Add cart collector/processor

Overview

In order to change the cart at runtime, you can use a custom collector or a custom processor.
Their main purpose is explained in their respective section.

Collector class

A collector can and should be used to retrieve additional data for the cart, e.g. by querying the database, hence the name "collector". This could also be querying an API endpoint, querying the database or fetching data in any other way you can think of.
Very often a collector is used to fetch data necessary for a processor.
It has to implement the interface Shopware\Core\Checkout\Cart\CartDataCollectorInterface and therefore implement a collect method.
But let's have a look at an example collector class.
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Core\Checkout\Cart;
4
5
use Shopware\Core\Checkout\Cart\Cart;
6
use Shopware\Core\Checkout\Cart\CartBehavior;
7
use Shopware\Core\Checkout\Cart\CartDataCollectorInterface;
8
use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
9
use Shopware\Core\System\SalesChannel\SalesChannelContext;
10
11
class CustomCartCollector implements CartDataCollectorInterface
12
{
13
public function collect(CartDataCollection $data, Cart $original, SalesChannelContext $context, CartBehavior $behavior): void
14
{
15
// Do your stuff in order to collect data, this is just an example method call
16
$newData = $this->collectData();
17
18
$data->set('uniqueKey', $newData);
19
}
20
}
Copied!
The collect method's parameters are the following:
  • CartDataCollection: Use this object to save your new cart data. You'll most likely use the set method here, which expects a unique key and its value. This object will be available in all processors.
  • Cart: The current cart and its line items.
  • SalesChannelContext: The current sales channel context, containing information about the currency, the country, etc.
  • CartBehavior: It contains a cart state, which describes which actions are allowed. E.g. in the product processor, there's a permission to check if the product stock validation should be skipped.
Your collector has to be defined in the service container using the tag shopware.cart.collector.

Processor class

A processor is the class that will actually process the cart and is supposed to apply changes to the cart. It will most likely use data, that was previously fetched by a collector.
Do not query data in the process method, since it may be executed a lot of times. Always use the collect method of a collector for this case!
Your processor class has to implement the interface Shopware\Core\Checkout\Cart\CartProcessorInterface and its process method.
Let's have a look at an example processor.
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Core\Checkout\Cart;
4
5
use Shopware\Core\Checkout\Cart\Cart;
6
use Shopware\Core\Checkout\Cart\CartBehavior;
7
use Shopware\Core\Checkout\Cart\CartProcessorInterface;
8
use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
9
use Shopware\Core\System\SalesChannel\SalesChannelContext;
10
11
class CustomCartProcessor implements CartProcessorInterface
12
{
13
public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
14
{
15
$newData = $data->get('uniqueKey');
16
17
// Do stuff to the `$toCalculate` cart with your new data
18
foreach ($toCalculate->getLineItems()->getFlat() as $lineItem) {
19
$lineItem->setPayload($newData['stuff']);
20
}
21
}
22
}
Copied!
The process method contains the same parameters as the collect method, but there's one main difference:
Next to the $original Cart, you've got another Cart parameter being called $toCalculate here. Make sure to do all the changes on the $toCalculate instance, since this is the cart that's going to be considered in the end.
Your processor has to be defined in the service container using the tag shopware.cart.processor.

Next steps

If you want to see a better example on what can be done with a collector and a processor, you might want to have a look at our guide regarding Changing the price of an item in the cart.
Last modified 17d ago