I am using Drupal8, Commerce 2, Commerce Recurring and Commerce Licence. 

I am selling yearly subscription.

Issue and Steps

  • User can add subscription product into cart
  • User redirecting to cart page
  • User hits back button and visit product page(subscription page) again.
  • User again add subscription product into cart. 
  • Then user end up with 2 same subscription products into cart. Need to restrict user to add multiple product into cart.

Possible solution:

I want to clear cart whenever new product is added. So every time there will be only one product in the cart. 

Note: I have tried to restrict one quantity for the product. As I am not displaying the cart page to the user, this solution is not working. 

Comments

mindhunter75’s picture

I'm trying to achieve the same. No luck so far.

arthur_drupal’s picture

Hi,

You can subscribe this event :

  public static function getSubscribedEvents() {
    $events = [
      CartEvents::CART_ORDER_ITEM_ADD => ['onCartItemAdd', 100],
    ];
    return $events;
  }

  public function onCartItemAdd(CartOrderItemAddEvent $event) {
    $order_item = $event->getOrderItem();
    $cart = $event->getCart();

    // Remove all other items
    $items = $cart->getItems();
    if (count($items) > 1) {
      $cartManager = \Drupal::service('commerce_cart.cart_manager');
      foreach ($items as $item) {
        if($item->id() != $order_item->id()){
          $cartManager->removeOrderItem($cart, $item);
        }
      }
    }
  }

mellowtothemax’s picture

This worked for me thank you arthur_drupal.

In a custom module

  • create a /custom_module_name.services.yml file with
services:
  custom_module_name.customname_subscriber:
    class: Drupal\custom_module_name\EventSubscriber\CustomNameEventSubscriber
    tags:
      - { name: event_subscriber }
  • create a /src/EventSubscriber/Custom_NameEventSubscriber.php file with 
<?php

namespace Drupal\custom_module_name\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\commerce_cart\Event\CartEvents;
use Drupal\commerce_cart\Event\CartOrderItemAddEvent;


class CustomNameEventSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
 public static function getSubscribedEvents() {
    $events = [CartEvents::CART_ORDER_ITEM_ADD => ['onCartItemAdd', 100],];
    return $events;
  }

  public function onCartItemAdd(CartOrderItemAddEvent $event) {
    $order_item = $event->getOrderItem();
    $cart = $event->getCart();

    // Remove all other items
    $items = $cart->getItems();
    if (count($items) > 1) {
      $cartManager = \Drupal::service('commerce_cart.cart_manager');
      foreach ($items as $item) {
        if($item->id() != $order_item->id()){
          $cartManager->removeOrderItem($cart, $item);
        }
      }
    }
  }

}
emircanerkul’s picture

<?php

namespace Drupal\xxx\EventSubscriber;

use Drupal\commerce_cart\CartManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\commerce_cart\Event\CartEvents;
use Drupal\commerce_cart\Event\CartOrderItemAddEvent;

class CartLimiterEventSubscriber implements EventSubscriberInterface {

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

  /**
   * Constructs an CartLimiterEventSubscriber object.
   *
   * @param \Drupal\commerce_cart\CartManagerInterface $cart_manager
   *   The cart manager.
   */
  public function __construct(CartManagerInterface $cart_manager) {
    $this->cartManager = $cart_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [CartEvents::CART_ORDER_ITEM_ADD => ['onCartItemAdd', 100]];
    return $events;
  }

  public function onCartItemAdd(CartOrderItemAddEvent $event) {
    $order_item = $event->getOrderItem();
    $cart = $event->getCart();

    // Remove all other items
    $items = $cart->getItems();
    if (count($items) > 1) {
      foreach ($items as $item) {
        if ($item->id() != $order_item->id()) {
          $this->cartManager->removeOrderItem($cart, $item);
        }
      }
    }
  }

}
services:
  xxx.cart_limiter_event_subscriber:
    class: Drupal\xxx\EventSubscriber\CartLimiterEventSubscriber
    arguments: ['@commerce_cart.cart_manager']
    tags:
      - { name: event_subscriber }