Item data when more than one item selected

I have a product (gift box) where a user can select 2 items from a list of 3 (this may increase). I have tried order_fields`, which works but displays on every item in the cart if more than one item and I have tried itemData, which again works and would be better as it is just one field. What happens though is that it will only pull in the last item selected, even if I create 3 different itemData fields, it will only pull in the last one if selected and ignore the other two. Ideally I would like one itemData field, beerChoice, which then pulls in all the selections.
This is the code for pulling in the choices

     {% if item.properties.beerChoice %}
        {{ item.properties.beerChoice }}
     {% endif %}

https://dev.adfdev.co.uk/shop/avid-gift-box.html

How can I have it, so it pulls through the selections? Is there a simpler way or am I doing it wrong.

Thanks

Hi Alastair!

It looks like you currently have the following HTML for selecting the beer options:

<input type="checkbox" id="Morphues" name="beerChoice" value="Morphues">
<label for="WhoopAss">WhoopAss</label>
<input type="checkbox" id="WhoopAss" name="beerChoice" value="WhoopAss">
<label for="Koru">Koru</label>
<input type="checkbox" id="Koru" name="beerChoice" value="Koru">

Because they all share the same input name (beerChoice), only one will ever be received by the server, even before Commerce has a chance to read it.

There’s two options I can think of.

  1. Send as an array by changing the name to beerChoice[]
<input type="checkbox" id="Morphues" name="beerChoice[]" value="Morphues">
<label for="WhoopAss">WhoopAss</label>
<input type="checkbox" id="WhoopAss" name="beerChoice[]" value="WhoopAss">
<label for="Koru">Koru</label>
<input type="checkbox" id="Koru" name="beerChoice[]" value="Koru">

Haven’t recently tested this myself but from a quick glance at the ItemData module code, I think that should work.

  1. Different field names per option.
<input type="checkbox" id="Morphues" name="beerChoice_morphues" value="Morphues">
<label for="WhoopAss">WhoopAss</label>
<input type="checkbox" id="WhoopAss" name="beerChoice_whoopass" value="WhoopAss">
<label for="Koru">Koru</label>
<input type="checkbox" id="Koru" name="beerChoice_koru" value="Koru">

This would need enabling the additional fields in the module and is also harder to render as you’ll need to do a check for each field.

My suggestion would be option 1.

Hi Mark, thanks for getting back to me. Option 1 didn’t work I’m afraid, I couldn’t get them to pull through at all. In the end, I used beerChoice in the itemdata module and used a select box for them to select their choices. May get complicated if more options are added in the future but for now it works well and pulls through to the basket and the order emails.

You can see it here- https://dev.adfdev.co.uk/shop/avid-gift-box.html

Thanks for your help though.