CoreShop Cart Processor
The Cart Processor takes care about refreshing the state and prices of carts for you. Everytime the persistCart
function of the Cart-Manager is called
on a Cart, it gets triggered and re-calculates the cart.
Following Processors are implemented by default:
- Cart Adjustment Clearer
- Item Processor
- Item Tax Processor
- Cart Price Rule Voucher Processor
- Cart Rule Auto Processor
- Cart Shipping Processor
- Cart Tax Processor
These Processors calculate all necessary prices of the cart. If you need to extend the Cart and change calculations, you need to create a new Processor.
Creating a Cart Processor
A Cart Processor needs to implement the Interface CoreShop\Component\Order\Processor\CartProcessorInterface
and registered
into the container with the tag coreshop.cart_processor
and a priority
attribute.
Example of a Cart Processor
For example, we create a Cart Processor, which calculates a custom field in our Cart.
<?php
namespace AppBundle\CoreShop\Order\Cart\Processor;
use CoreShop\Component\Order\Model\OrderInterface;
use CoreShop\Component\Order\Processor\CartProcessorInterface;
final class CustomCartProcessor implements CartProcessorInterface
{
public function process(OrderInterface $cart): void
{
$cart->setCustomField(uniqid());
}
}
We now only need to register the class:
app.coreshop.cart.processor.custom:
class: AppBundle\CoreShop\Order\Cart\Processor\CustomCartProcessor
tags:
- { name: coreshop.cart_processor, priority: 200 }
On every Cart Update, our service now gets called a the custom field gets a new unique id.