I've several product type with corresponding product displays. The scenario is a case where there is only a product type per time on the cart. That is, you cannot have multiple product types at the same on the cart. Hence, each time a new item is to be added, check if it is of the same type as the existing items in the cart. If it is add, if not clear cart before adding the new item.

What rule setting can help me in achieving this. Thank you.

1 Answer 1


Found answer to my own question. I discovered the conditional rules module and came up with this rule component for each product type:

{ "rules_empty_cart__consumer_g" : {
    "LABEL" : "Empty cart - consumer g",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional", "commerce_cart", "commerce_order" ],
    "ON" : { "commerce_cart_product_prepare" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "commerce-product:type" ], "value" : "store_item" } }
    "DO" : [
      { "CONDITIONAL" : [
            "IF" : { "commerce_order_contains_product_type" : {
                "commerce_order" : [ "commerce_order" ],
                "product_type" : { "value" : {
                    "product" : "product",
                    "book" : "book",
                    "food" : "food"
                "operator" : "\u003E=",
                "value" : "1"
            "DO" : [
              { "commerce_cart_empty" : { "commerce_order" : [ "commerce_order" ] } },
              { "drupal_message" : { "message" : "cart cleared!" } }

What this does is it every time an item is to be added to cart it checks if there at least one item in the cart that is not of the same product-type as the product that is about to be added. If there is, the cart is cleared before the item is added, if not the cart remains unchanged and the new item added to the existing list on the cart.

For this check to happen irrespective of the type, a similar rule is created per product type.

  • Hey @Ojchris How can I use the above code? Do I have to create a module? I trying to achieve like this, whenever user add any product, cart should be empty. I am using Drupal commerce to sell subscription plan. Commented Aug 20, 2019 at 13:00
  • 1
    @FirozSabaliya just install the conditional rules module it will automatically sync into the rule components. Then create a rule based on the event 'before adding a product to the cart', next add a condition that checks the cart is empty. The final action is for the product to be added
    – Ojchris
    Commented Aug 20, 2019 at 16:15
  • Hello @Ojchris, Appreciate your quick reply. But I am using Drupal8. Conditional rules module is only available for D7. Any alternate solution for this? Commented Aug 21, 2019 at 5:28
  • Oh I see. Partly why I'm yet to get into D8, lots of missing module. This previous discussion may help you find alternative: drupal.stackexchange.com/questions/245049/…
    – Ojchris
    Commented Aug 22, 2019 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.