Add product(s) to cart from DOM string short version:

how can I add multiple items to a cart from a served string that provides the product ids and their quantities? I suppose a rule could handle this somehow?


long explanation:

hey guys,

I'm trying to make some bigger changes to the add-to-cart form and the way it functions, for my client and I have specific needs here.
Usually the form only let's you add one type of product variation at a time, but I want visitors to be able to set multiple product variations of the same product node, and the 'Submit' button submits all of them, instead of just the currently selected variation.

To put this into perspective: Let's say I have a product (display node) called


Brick


This Brick holds three variations:


Brick (Front) , Brick (Side) , Brick (Top)


Using a Custom Line Item, it gets these options added automatically (taxonomy term list reference):


Red , Green , Blue


Here, we want users to be able to (for example) click Side and select Green, then hit Front and choose Red, and when they hit Submit, both of these get added to their cart. By default however, of course only one - the last selection - is added.

Now, using a hook_form_commerce_cart_add_to_cart_form_alter() and custom JavaScript, I've gotten to a point where my JS automatically stores the entire selection inside a JS Object. But from hereon, I'll need your help guys.

I can easily print the JS Object into the DOM as a string with a format such as VariationID:LineItemOption:Quantity[_repeat].
For example the string could read 31:2:1_32:3:1 (which for humans would read Side:Green:1, Front:Red:1).

From hereon, though, I dont really know how to progress. My idea would be to perhaps create a custom rule that (replacing the action of the default Submit button) receives that string, filters it and adds matching items to the cart.

Really hope someone here has an idea on how I could progress. Any suggestion and recommendation would be highly appreciated!
If I understood you right. I had some similar task in the past. I had form of all product variations in the table with textbox inputs for quantity. And they must be added multiple to cart.

The main function is commerce_cart_product_add_by_id($product_id, $quantity);

What I did:
I created custom field formatter for "Product reference" field and rendered it as table of variations with text inputs for quantity.
And on form submit I loop all products in foreach and add them to cart.

But if you don't want to create additional formatters there is another solution. 


You can use your hook hook_form_commerce_cart_add_to_cart_form_alter()
Render form elements for all your product variations there by custom code.
Replace form submit with your custom submit
Add products to cart in custom submit (call commerce_cart_product_add_by_id() in foreach).


Also you can write your text VariationID:LineItemOption:Quantity[_repeat] into some hidden textfield element. After submit you'll read this text in $form_state