1

I am trying (and failing miserably) to send multipule products to my cart using MultiAdd

I can see the data getting sent - it looks ok? I get a 200 response back, so I know its working, but I'm always getting this error:

{"error":["No items?"]}

Here is what my form data looks like:

action:multiAdd/multiAdd
redirect:commerce/cart
qty:1
CSRF:gQBSJxqkMIQChG3r6TJ4Dz81dgtZuh4yrv_ZUDTp|ecc547c00a45b29a506b9f4c7904e62add62d37a
myProductOne:174
myProductTwo:64
myProductThree:189

My product template looks like this:

<form>
<input type="hidden" name="action" value="multiAdd/multiAdd">
...

<select name="myProductOne">
    <option value="174">Value</option>
</select>
...
<select name="myProductTwo">
    <option value="64">Value</option>
</select>
...
<select name="myProductTwo">
    <option value="189">Value</option>
</select>

I am using jquery validator to validate my form; I'm handling the submit like this:

...
 onFormSubmit(function (response) {
     console.log(response);
 });


var onFormFormSubmit = function (callback) {
    var data = $('#campRegistrationForm').serialize();
    data['CSRF'] = $('input[name="CSRF"]').val();

    $.ajax({
        'type': 'post',
        'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
        'cache': false,
        'data': data,
        'url': '/actions/' + $('input[name="action"]').val(),
        'dataType': 'json',
        'timeout': 50000
    }).done(function (response) {
        callback(response);
    }).fail(function (error) {
        // Total fail.
    });
};

I see on the plugin's page, I need to have markup like this:

<select name="items[{{ loop.index }}][options][color]">

I believe it all boils down to the fact I'm not doing this correctly:

{% for product in craft.commerce.products.find() %}
...
{% endfor %}

Since I am selling 3-5 different products on any given page, I can't loop through all of my products as I only need specific ones at any given time. I'm not sure how to set that up correctly.

EDIT

Thank you so much!

To help others, here is what each of my select input(s) look like:

<select id="myProductOne" name="items[1][purchasableId]">
<input type="hidden" name="items[1][qty]" value="1">
...
<select id="myProductTwo" name="items[2][purchasableId]">
<input type="hidden" name="items[2][qty]" value="1">

...
2

Ok you're getting there, but your form syntax is a bit off. MultiAdd needs you to post a form with an array named items.

Here's an example of precisely the sort of thing you need to post:

action    
multiAdd/multiAdd

items[1][purchasableId]    
854
items[1][qty]    
0

items[4][purchasableId]    
857
items[4][qty]    
1

items[5][purchasableId]    
858
items[5][qty]    
0

items[6][purchasableId]    
859
items[6][qty]    
1

redirect    
/cart

So you're posting items, each keyed by an index value to keep the attributes together. This can be anything really. You just have to send for each the purchasableId and a qty (if you send 0 the item will just be ignored).

items[whatever][purchasableId]
items[whatever][qty]

Just send as many of those pairs as you need, exactly like that (i.e. it must be called items, and the word whatever you can replace with an arbitrary key). Each pair must have a value for purchasableId and qty (so for the first example the purchasableId is 854 and the qty is 0, so this one won't be added to the cart).

(You don't need to use the options syntax to add products as such, that's only required if you're using options with those products. Maybe get your form working without options first, so just adding products) and then we can add them if you need them!)

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.