Avatar of quikwallace
quikwallace
 asked on

Drupal 7 Commerce Rules - Stop Duplicate item

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?
DrupalE-Commerce

Avatar of undefined
Last Comment
Aaron Feledy

8/22/2022 - Mon
Gary

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

ASKER
HI Gary

That plugin is for uberCart, not commerce.
Gary

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
quikwallace

ASKER
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

quikwallace

ASKER
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.
quikwallace

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Aaron Feledy

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.
quikwallace

ASKER
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 Feledy

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
quikwallace

ASKER
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.
ASKER CERTIFIED SOLUTION
Aaron Feledy

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
quikwallace

ASKER
DUDE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
quikwallace

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
quikwallace

ASKER
Exceeding excellent and provided full example.  Wish I could go in and increase the points.
Aaron Feledy

Glad to help :)