Following this documentation I have created a custom event subscriber for add, update and remove items from cart. It works good and events are fired in every case you add,update or remove items from the cart on the cart page or from the shop view.
I have installed Commerce Cart Fly Out, that uses Commerce Cart Api and a fly-out window to show the cart, and It uses REST to update and remove. If you remove an item, then my remove event is fired. But if you update the quantity, update event is not fired. I have followed, with xdebug, the execution progress until class CartUpdateItemsResource and its patch method, that save the order item. The line is updated well but my update event is not fired. I don't know the reason.

This is my custom Event subscriber:

 * Cart Event Subscriber.
class MyCartEventSubscriber implements EventSubscriberInterface {

  protected $database;

  protected $messenger;

  protected $cartManager;

  public function __construct(MessengerInterface $messenger, CartManagerInterface $cart_manager) {
    $this->messenger = $messenger;
    $this->cartManager = $cart_manager;
    $this->database = \Drupal::database();

  public static function getSubscribedEvents() {
    return [
      CartEvents::CART_ENTITY_ADD => [['addToCart', 100]],
      CartEvents::CART_ORDER_ITEM_REMOVE => [['removeFromCart', 100]],
      CartEvents::CART_ORDER_ITEM_UPDATE => [['updateCart', 100]],

  public function addToCart(CartEntityAddEvent $event) {
    \Drupal::logger('CartEvent')->notice('add to cart');
    // ... some code

  public function removeFromCart(CartOrderItemRemoveEvent $event) {
    \Drupal::logger('CartEvent')->notice('remove from cart');
    // ... some code

  public function updateCart(CartOrderItemUpdateEvent $event) {
    \Drupal::logger('CartEvent')->notice('update cart item');
    // ... some code
  • 1
    Since the question is about code you wrote, it should at least show the code you wrote. – kiamlaluno Dec 1 '20 at 8:29
  • Sorry. I thought It was not necessary because my code works good and the problem seems to be commerce cart api. I have added the code. Thanks for your help. – briast Dec 1 '20 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.