1

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!

1

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.

  1. You can use your hook hook_form_commerce_cart_add_to_cart_form_alter()
  2. Render form elements for all your product variations there by custom code.
  3. Replace form submit with your custom submit
  4. 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

  • Hey milkovsky, first off, sorry that I did not reply any sooner than this. I already made fair use of the form hook and therefore rather stuck with the product display than setting up an additional view. I did not know the commerce_cart_product_add function before (the _by_id alternative did not allow me to include additional line item fields), and neither that you can replace a submit handler (further googling told me how to do so). – so thanks a whole bunch for introducing me to these things! – Bird-Kid Mar 4 '14 at 14:49
  • the hidden input field was what I had in mind too, and indeed that allowed me to collect a user's entire selection and (on submit) pump it back in as a string via JS. however, i couldn't quite manage to make it work proper with just the commerce_cart_product_add function. (links were missing on the cart etc). so instead I ended up copying the original submit handler and looping through it (with some slight modifications each loop). – and voilà, it's doing a dream of a job! so, once again, thank you so so many times for guiding me to the right direction and sharing your solutions, milkovsky! – Bird-Kid Mar 4 '14 at 15:02
  • You are welcome. That's how open source community works :) – milkovsky Mar 4 '14 at 17:36
  • You can post your solution as new answer as well – milkovsky Mar 5 '14 at 14:01

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.