We help IT Professionals succeed at work.

Drupal 7 Commerce Rules - Stop Duplicate item

quikwallace
quikwallace asked
on
1,883 Views
Last Modified: 2013-12-30
Not sure if there are a lot of drupal experts here, but i'll ask, will give me some sense of accomplishment, maybe.

So I have a commerce site, need to prevent users from adding the same item to their cart.  They should only be able to purchase 1 of any sku type.

Sooo, using rules,  I just want to say:
before adding this item to cart
look in cart
if there is another item with the same sku
if so, do not add this item, display message saying dup prevented
continue shopping experience.

I've tried so many things and read so many comments - but nothing is working.  Most rules examples are really basic and the more complex ones, either don't relate close enough to my need or the instructions are just.....  

I had a rule that would check for quantity after i had added the product to the cart, this was ok, except the added to cart message appears along with the duplicate removed message I created.  Obviously I can get rid of the add to cart msg, so I need to address this before the item is added.

Is anyone out there proficient with Drupal 7 Commerce Rules and have any idea on how to resolve this?
Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Have you tried this plugin
https://drupal.org/project/uc_restrict_qty

Author

Commented:
HI Gary

That plugin is for uberCart, not commerce.
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Ahh this should work
http://www.drupalcommerce.org/node/1053

{ "rules_restrict_quantity_1" : {
    "LABEL" : "Restrict quantity to 1",
    "PLUGIN" : "reaction rule",
    "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" ] } }
    ]
  }
}

Open in new window

Author

Commented:
This is nice and is somewhat similar to what I had originally.  I did have a couple of more steps, well several to be honest :).  this is much cleaner - but now here in lies the problem.

I need to add a message to the user saying, duplicates not accepted.  That is easy enough, but the problem comes in when this rule runs, the item is still added to the cart, thus you still get the item successfully added to cart message, along with your message.  I can't change the default add to cart message because of course, it is needed for the normal shopping.  

See example, is there any way around this?Drupal Commerce Duplicate Msg Issue
I just added the drupal_message line:
{ "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 allowrf.  This item has been removed from \u003Ca href=\u0022\/cart\u0022\u003Eyour cart?\u003E",
          "type" : "warning"
        }
      }
    ]
  }
}

Open in new window

Author

Commented:
I have racked my brain, because I seems like I should be able to do this very thing, but do it on "Before Saving Item to cart", that would resolve the default message issue.  But I can't figure out a way to do this.

Author

Commented:
There does not appear to be a means to turn off the add to cart message for my particular circumstance, so I really need these steps, as mentioned in the original post, so the duplicate item is not added to teh cart, thus never displaying the add to cart message

Sooo, using rules,  I just want to say:
before adding this item to cart
look in cart
if there is another item with the same sku
if so, do not add this item, display message saying dup prevented
continue shopping experience.
Aaron FeledyDrupal Developer and Consultant

Commented:
You can make a product unpurchasable by setting its price to NULL. You could try doing that before adding the item to the cart. You would use the set a data value action to set the price:amount to nothing by leaving the box empty.

Author

Commented:
Hummm, I won't be able to do that because all products will have a price and i will have no way to determine when a duplicate product is being added to someone's cart.
Aaron FeledyDrupal Developer and Consultant

Commented:
If this is firing after the item is added to the cart and you have the php filter enabled, you could create a php action to execute the drupal_get_messages() function which will clear any pending messages. This should remove the add to cart message assuming the rule runs after the message is generated.

Author

Commented:
Hummm, I don't want to resort to adding code.  Just seems I should be able to accomplish this on, showhow on the  "before adding this item to cart" event, so the message would not be triggered.
Drupal Developer and Consultant
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
DUDE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Author

Commented:
Sorry, I was so excited I accidentally hit submit above.  I had not even thought of touching the default message.  I didn't want to disrupt anything related to the normal flow of the cart.

I have been working on this for days.  I have so many rules, components of rules - geeeesh, thank you!


THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

And for those who come along after, be sure to read his instruction in the 2nd paragraph [To add this check...] I missed that and initially didn't think it worked.

Author

Commented:
Exceeding excellent and provided full example.  Wish I could go in and increase the points.
Aaron FeledyDrupal Developer and Consultant

Commented:
Glad to help :)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.