1

I finally got a duplicate cart rule to work in my Drupal Commerce site. My "After item is added to cart" rule determines if there are duplicate sku's, if so, resets the quantity of that item to one.

When that rule is run, it posts a system message to the user saying the duplicate has been removed from the cart, but the original "Item has been added" message still appears, so the green, "item added to cart" msg appears and just under it, my "duplicate item has been removed" msg appears.

I want to turn off/not display that first "item has been add" message, when a duplicate has been detected. Obviously the message would appear as normal when no duplication is found.

What is the simplest means of accomplishing this? Nothing I've tried thus far has worked.

default add to cart message:

{ "commerce_cart_add_to_cart_message" : {
    "LABEL" : "Display an Add to Cart message",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "Commerce Cart" ],
    "REQUIRES" : [ "commerce_cart" ],
    "ON" : { "commerce_cart_product_add" : [] },
    "DO" : [
      { "commerce_cart_add_to_cart_message" : { "commerce_product" : [ "commerce-product" ] } }
    ]
  }
} 

my stop duplicate rule

{ "rules_restrict_quantity_1" : {
    "LABEL" : "Restrict quantity to 1",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_cart" ],
    "ON" : { "commerce_cart_product_add" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "commerce-line-item:quantity" ],
          "op" : "\u003E",
          "value" : "1"
        }
      }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "commerce-line-item:quantity" ], "value" : "1" } },
      { "entity_save" : { "data" : [ "commerce-line-item:order" ] } },
      { "drupal_message" : {
          "message" : "Duplicates items are not allow.  This item has been removed from \u003Ca href=\u0022\/cart\u0022\u003Eyour cart?\u003E",
          "type" : "warning"
        }
      }
    ]
  }
}
1

You can't remove a message from the queue once it's been added via Rules, so you just need to reproduce the conditions in inverse from one rule to the next. In other words, you already configured the conditions in your custom rule to prevent the duplicate, so now put the opposite conditions on the rule that shows the normal Add to Cart message.

  • i'm sorry, i just don't understand how to achieve this. I have the rule for the add to cart and my restrict qty rule. How should I "do the opposite" in the default add to cart msg? – user2969739 Dec 29 '13 at 19:50
  • Invert the logic. In your case, since you just have a single condition, add it to your "Add to Cart message" rule and check the Negate box at the bottom of the configuration form. – Ryan Szrama Dec 30 '13 at 20:56
0

Whether or not you choose to follow Ryan's advice to disable the message, be aware that your Action:

{ "data_set" : { "data" : [ "commerce-line-item:quantity" ], "value" : "1" } },

will only change the line-item quantity, not its total.

Unless you also add a second Action such as:

{ "data_set" : { "data" : [ "commerce-line-item:commerce-total" ], "value" : [ "commerce-line-item:commerce-unit-price" ] }

then the total amount for the line-item will increment each time the customer tries (and fails, because of your rule) to add more than one of the product. Your Cart will show a quantity of 1 but a total of n x unit price.

Your Answer

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

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