Introduction to Drupal Commerce Cart Events
shopping cart

Introduction to Drupal Commerce Cart Events

Recently I have used the commerce module of Drupal in my current Drupal website.The 2.x version of this module brings a numbers of features which we expect in modern paradigm in e-commerce websites. In previous version of commerce module there were multiple hooks for different events. Now a number of hooks are reduced and we find different events which we can subscribe with the Drupal event subscribe mechanism.

This article is about different cart related events and their usage. A final class is defined in commerce cart module to define these events.

commerce/modules/cart/src/Event/CartEvents.php

In above file we see the cart events are defined as constant.


  1. CART_EMPTY = 'commerce_cart.cart.empty';
  2. CART_ENTITY_ADD = 'commerce_cart.entity.add';
  3. CART_ORDER_ITEM_ADD = 'commerce_cart.order_item.add';
  4. CART_ORDER_ITEM_UPDATE = 'commerce_cart.order_item.update';
  5. CART_ORDER_ITEM_REMOVE = 'commerce_cart.order_item.remove';
  6. ORDER_ITEM_COMPARISON_FIELDS = 'commerce_cart.order_item.comparison_fields';


As their name implies , each event is triggered on a specific condition. We can subscribe these events and can run our custom code.


Now we will take an example where we have a specific category of few SKUs which can't be added twice in the cart. If some user want to add that we have to show a error message that this product can not be added twice to the cart. Below are the steps to achieve this.

Create a my_module.services.yml file in your custom module

services:
  my_module.event_subscriber:
    class: Drupal\my_module\EventSubscriber\CartEventSubscriber
    arguments: ['@messenger', '@commerce_cart.cart_manager']
    tags:
      - { name: event_subscriber }        

2. Create CartEventSubscriber.php file in my_module/src/EventSubscriber/CartEventSubscriber.php

<?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\commerce_cart\CartManagerInterface;
use Drupal\commerce_cart\Event\CartEntityAddEvent;
use Drupal\commerce_cart\Event\CartEvents;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
 * Cart Event Subscriber.
 */
class CartEventSubscriber implements EventSubscriberInterface {

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The cart manager.
   *
   * @var \Drupal\commerce_cart\CartManagerInterface
   */
  protected $cartManager;

  /**
   * Constructs event subscriber.
   *
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(MessengerInterface $messenger, CartManagerInterface $cart_manager) {
    $this->messenger = $messenger;
    $this->cartManager = $cart_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      CartEvents::CART_ENTITY_ADD => [['addToCart', 100]]
    ];
  }

  /**
   * Restrict a specific sku
   *
   * @param \Drupal\commerce_cart\Event\CartEntityAddEvent $event
   *   The cart add event.
   *
   * @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
   */
  public function addToCart(CartEntityAddEvent $event) {
    /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $product_variation */
    $product_variation = $event->getEntity();
    if ($product_variation->getSku() === 'some_sku') {
      $this->messenger->addError($this->t('This SKU can not be added twice in the cart.'));
    }
  }

}        

3. In above example we are comparing sku in a static way but it can be configured from the backend variation configuration.


So this is a basic example of subscribing the cart events and we can run our custom code on these events trigger. Hope this article helps to understand the basics of cart events.

Saurabh Kumar Singh

Full Stack Developer | Drupal | Salesforce | HTML, JS, PHP, React, MySql, AWS

9 个月

Thanks for sharing this information!

要查看或添加评论,请登录

Deepesh Arora的更多文章

社区洞察

其他会员也浏览了