Solved

Drupal 7 Commerce Rules - Stop Duplicate item

Posted on 2013-12-29
15
1,256 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?
0
Comment
Question by:quikwallace
  • 9
  • 4
  • 2
15 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39744880
Have you tried this plugin
https://drupal.org/project/uc_restrict_qty
0
 

Author Comment

by:quikwallace
ID: 39744894
HI Gary

That plugin is for uberCart, not commerce.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39744915
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

0
 

Author Comment

by:quikwallace
ID: 39744938
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

0
 

Author Comment

by:quikwallace
ID: 39744950
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.
0
 

Author Comment

by:quikwallace
ID: 39745132
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.
0
 
LVL 13

Expert Comment

by:Arrow_1
ID: 39745505
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:quikwallace
ID: 39746611
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.
0
 
LVL 13

Expert Comment

by:Arrow_1
ID: 39746629
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.
0
 

Author Comment

by:quikwallace
ID: 39746911
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.
0
 
LVL 13

Accepted Solution

by:
Arrow_1 earned 500 total points
ID: 39747154
Alright.. here's the trick:

Use this rule to restrict the quantity:
{ "rules_restrict_quantity" : {
    "LABEL" : "Restrict Quantity",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "1",
    "OWNER" : "rules",
    "REQUIRES" : [ "commerce_order", "rules", "commerce_cart" ],
    "ON" : { "commerce_cart_product_add" : [] },
    "IF" : [
      { "commerce_order_contains_product" : {
          "commerce_order" : [ "commerce_order" ],
          "product_id" : [ "commerce-product:sku" ],
          "operator" : "\u003E",
          "value" : "1"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "Quantity is restricted to 1 per order.",
          "type" : "warning"
        }
      },
      { "data_set" : { "data" : [ "commerce-line-item:quantity" ], "value" : "1" } }
    ]
  }
}

Open in new window


Notice that the "weight" is 1. This makes it fire after the "Display an Add to Cart message" rule which has a weight of 0. Now, when you add an item to the cart, the message is generated, then if the quantity is 2, it will be reduced to 1 and a warning is displayed. The thing to notice here is that the quantity is 2, then it is reduced to 1 by our rule. So, we can add a condition to the "Display an Add to Cart message" rule that determines if the quantity is 1, meaning it's the first one added to the cart or 2 meaning it's an attempt to add a second one which will be corrected later by our restrict quantity rule.

To add this check, go to the "Display an Add to Cart message" and add "Order contains a particular product" condition. Then set the order to commerce_order, the sku to commerce-product:sku (be sure to switch to data selection rather than direct input), the operator to "=", and the quantity to 1.

Now, the message will only be displayed when the first item is added to the cart. Subsequent items will not trigger the message, but will trigger the rule to reduce the qty to 1 and display the warning.
0
 

Author Comment

by:quikwallace
ID: 39747222
DUDE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0
 

Author Comment

by:quikwallace
ID: 39747227
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.
0
 

Author Closing Comment

by:quikwallace
ID: 39747233
Exceeding excellent and provided full example.  Wish I could go in and increase the points.
0
 
LVL 13

Expert Comment

by:Arrow_1
ID: 39747509
Glad to help :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I recently read an article which suggested that 60% of businesses in the U.S. that process credit card details online in order to accept payment for goods or services were not Payment Card Industry security standards (PCI) compliant. This statement …
Are you using email marketing software? If not, you're missing out on effortless marketing and the reaching of desired conversion rates through email marketing software.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now