0

With Drupal 8 / Drupal Commerce 2, I can't find a way to disable product combining in cart (same products in cart appears on the same line with quantity > 1). In my use case I never want this to happen.

I found that in

/modules/contrib/commerce/modules/cart/src/Form/AddToCartForm.php

...we have a submitForm method which calls

$this->cartManager->addOrderItem($cart, $order_item, $form_state->get(['settings', 'combine']);

So it seems like my form needs to have a combine variable set to FALSE.

How could I set this variable or override

public function addOrderItem(OrderInterface $cart, OrderItemInterface $order_item, $combine = TRUE, $save_cart = TRUE);

...in

/modules/contrib/commerce/modules/cart/src/CartManagerInterface.php

...to define the default value of $combine to FALSE ?

Thanks

2 Answers 2

1

I could achieve this by implementing hook_form_alter on add to cart form (in a custom module) and setting combine like this :

$form_state->set(['settings', 'combine'], FALSE);
1

Edge-case, but if you're adding items to the cart programmatically from a custom form, here's what I found to have worked.

$cart_manager = \Drupal::service('commerce_cart.cart_manager');

$line_item = $cart_manager->addEntity($cart, $variationobj, $quantity, FALSE, TRUE);

This will call addEntity() and set Combined to FALSE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.