Tags/topics: 
2
Answers
Vote up!
0
Vote down!

How can I split quantities in the Add to Cart process into different line items?

I need to have the cart not combine items of the same type as I need each product to have its own line item ID. I have disabled all of the combine like items but its still combining in cart?

Any ideas? I'm running the latest Commerce Kickstart on D7.

Asked by: brandercreative
on March 25, 2013

2 Answers

Vote up!
1
Vote down!

The issue here is that "combine like items" only comes into affect with each additional press of the "Add to Cart" button. If you set the quantity to 10 and click "Add to Cart", it's adding a single line item to the cart with a quantity of 10. If you did it again with a quantity of 1, then you'd see a second line item with a quantity of 1. However, it does not break out a quantity of 10 form submission and add them as 10 individual line items.

If you must support a quantity textfield, my suggestion would be to setup a Rule (or quick custom module code) that reacted to adding a product to the cart, looked at the quantity, and if it was over 1 created an additional number of line items as necessary and set the quantity of the original one back to 1. If you're down with more code, you could also just alter the Add to Cart form to use a different submit handler that looped over the quantity entered and added the products individually.

Ryan Szrama
Answer by: Ryan Szrama
Posted: Mar 26, 2013

Comments

Hi Ryan,

Will have a crack at doing it with rules in the morning. Appreciate your response and will post back.

Thanks,
Ben

- brandercreative on March 26, 2013
Vote up!
0
Vote down!

This is what I have so far-

{ "rules_split_quantity" : {
"LABEL" : "Split Quantity",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules", "commerce_cart" ],
"ON" : [ "commerce_cart_product_add" ],
"IF" : [
{ "entity_is_of_type" : { "entity" : [ "commerce-line-item" ], "type" : "commerce_line_item" } },
{ "entity_is_of_type" : { "entity" : [ "commerce-order" ], "type" : "commerce_order" } }
],
"DO" : [
{ "data_calc" : {
"USING" : { "input_1" : [ "quantity" ], "op" : "-", "input_2" : "1" },
"PROVIDE" : { "result" : { "result" : "Calculation result" } }
}
},
{ "commerce_cart_product_add_by_sku" : {
"USING" : {
"user" : [ "site:current-user" ],
"sku" : "[commerce-product:sku]",
"quantity" : [ "result" ],
"combine" : 0
},
"PROVIDE" : { "product_add_line_item" : { "product_add_line_item" : "Added product line item" } }
}
}
]
}
}

Its adding products back to the cart with one less in the quantity but its not making multiple lines just one with multiple quantities?

Any ideas?

Thanks,
Ben

Answer by: brandercreative
Posted: Mar 26, 2013

Comments

Did you ever solve this? I am in need of this as well. I need to keep the Qty selection on the Product Display so someone can add more than one to the cart, but need them to truly be separate line items. The "Combine Liked Products" option appears to split them, but it doesn't appear to actually work as intended.

- Travis on August 12, 2015

Just to add it makes sense that it doesn't work, since the "Add Product" action is looking for a qty (which we need it to be a 1), but in order for it to keep adding the others, we should be able to run a "add product loop" X amount of times. X being the remaining number when taking the QTY - 1 (so to not include the original product in the list).

- Travis on August 17, 2015