1

I am learning/working with RULES in my Drupal7 Commerce store. I thought this would be a fairly simple rule to create, but I can't find a work around for my need. I have a commerce site, that has our team uniforms. Each member is entitled to one and only one patch. If they add a second patch to their cart, I want a message to appear telling them that they can only have one patch in there cart and the add to cart process stopped.

So here is the scenario.

  • Click the Add to Cart button for first patch, order/item/patch goes into cart
  • Click the Add to Cart button for a second patch, message appears "you can't do that" and item does not go into cart

Where I am stuck is, what to select in the RULES UI, so it will check to see what is in the cart, look at the sku or item number [both of these are unique to the item], determine if the item that is to be added a duplicate, if so, pop up warning message, do not continue the process of adding that item.

Any help would be greatly appreciated.

1 Answer 1

1

You're looking for the action 'commerce-order: commerce-line-items' in Rules as a way of inspecting and testing against what the user already has in their carts. The recipe outlined here goes through how to add this rule in and do some preliminary testing, I've tried to modify it for your use case:

  1. Create rule, name it; Event: before adding a product to the cart
  2. Add your conditions, ex. check if content is a patch
  3. In Actions add Loop through commerce-order:commerce-line-items
  4. Create component, single rule, name it
  5. Create following variables of the component and name them as you wish:
    • Commerce line item (line item of the order from the loop of the main rule)
    • Commerce product (product added to the cart)
  6. Add condition to the component: Entity has field, Entity: [line-item-in-order], Field: commerce_product. this will bring us the product from actual line item in current order (shopping cart).
  7. Add condition to the component: Data comparison. Data to compare: line-item-in-order:commerce-product with product-added-to-cart -- This would be where you would determine if it is also a patch

  8. Add action to the component: Remove an item from a list. List: [order:commerce-line-items], Item to remove: [line-item-in-order]. This will completely remove duplicated product from the shopping cart and from the order as well.

  9. Eventually add another actions. At least Display some message to the user to inform him/her that the item has been removed sounds sensible.

Come back to your main rule created in step 1. Add action to the loop and select the component you have just created in steps 4. - 9. and set the variables.

5
  • I was actually just reading this. what is a component? I didn't understand that part mentioned first in step 4. Commented Dec 2, 2013 at 20:30
  • Found the component, of course, just after I asked the question. Commented Dec 3, 2013 at 13:58
  • So did this answer solve your issue?
    – schnippy
    Commented Dec 3, 2013 at 17:25
  • Unfortunately it did not. I am still wrestling with this one. Certain things are confusing me. For example I will be using the "product sku" instead of "patch" to do my data compare on, well product sku does not appear in my selector listing, though I see it in the actual product type. Also, I don't see for example, line-item-in-order [I don't see the "in", so assuming a typo] and #7, I don't follow at all, what variable from the main rule, which I assume is #1 Commented Dec 4, 2013 at 5:09
  • fyi steps 1-5 are clear. Steps 6-9 are not. Building it yourself and then posting the exports might be a good way to ensure accurate steps above. I couldn't get it to work.
    – Anon Ymous
    Commented Feb 13, 2017 at 1:22

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.