1

I have a site and i am using drupal commerce module. i want to set a rule which will prevent the user to add a product twice in the cart. suppose a user add a product A and after that if he add the same product in the same cart than the system should generate a message that the same product is already added in your cart you can add another product.

I tried to create a rule but it works on the total quantity of cart not on total quantity of same product in the cart. please help

Thanks

1

You can use unique field , autosku and tokens module

using that module set the name of sku using tokens While generating sku use tokens

the parameters which u know differentiates 2 products should be used in tokens

Like if quantity and size is the parameter which differentiates your 2 products and defines two products as unique then using tokens(for e.g.) [product:qty] and [product:size] set the sku title and using unique field set the sku as required and unique field

You can try the following function to edit the quantity : you can set cart quantity for a single item to be default value 1. hook_commerce_cart_line_item_refresh($line_item, $order_wrapper)

Allows modules to perform additional processing to refresh an individual line item on a shopping cart order.

Prior to this hook being invoked, product line items will have already had their sell prices refreshed via the creation of a new line item for the same product being passed through Rules for calculation.

Parameters

$line_item: A line item object that should be updated as necessary for the refresh.

$order_wrapper: An EntityMetadataWrapper for the order the line item is attached to.

  • HI Harshal. thanks for your reply. but i did not actually get the point. i want user add a product to cart . when again he/she click on add to cart button for same product then it should not be added in cart and print message that same product is already there in your cart – eweb Dec 6 '13 at 7:54
  • sorry I thought u wanted autosku ,as per your needs you can set cart quantity for a single item to be default value 1 – harshal Dec 6 '13 at 7:59
  • pls check the function it might help – harshal Dec 6 '13 at 8:08

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.