I am using Drupal Commerce in that user can add a product to cart. If user add same product for five times, cart contains same product for five times. What I need is when user adds same product again and again it should reset to 1. I have include the following lines still I am getting the same.

function dt_admin_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {

   $form_state['line_item']->data['context']['add_to_cart_combine'] = TRUE;

   return $form;


enter image description here


Here you can find the solution for restricting duplicated items to the cart.

Solution is

  1. Create rule, name it; Event: before adding a product to the cart
  2. You can add there conditions if you wish. I am chcecking here is product is digital or not.
  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 (variable brought from the main rule)
  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.
  10. 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.
  11. You are done. Chcek it, it should work
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy