Reputation: 658
I'm getting the error:
Parameter Missing or Invalid: Required parameter missing or invalid: id
When trying to add a product to the basket from the product page, don't understand why this is happening?
<form action="/cart/add" method="post" enctype="multipart/form-data">
{% if product.options.size > 1 %}
<fieldset class="group">
<ul class="checkbox">
{% for variant in product.variants %}
{% if variant.available == true %}
<li>
<label>
<input type="radio" value="{{variant.id}}" name="id" />
{{ variant.title }} for {{ product.price | minus:variant.price | money_with_currency }}
</label>
</li>
{% else %}
{% endif %}
{% endfor %}
</ul>
</fieldset>
{% endif %}
<input type="submit" name="add" id="add" class="inpost-buy w-button" value="Add to Bag →"></input>
</form>
Any help to get this sorted would be brilliant!
EDIT - Updated Code
<form action="/cart/add" method="post">
{% if product.options.size > 1 %}
<fieldset class="group">
<ul class="checkbox">
{% for variant in product.variants %}
{% if variant.available == true %}
<li>
<input type="radio" value="{{variant.id}}" name="id" {%if variant.id == product.selected_or_first_available_variant.id %} checked{% endif %} >
<label>{{ variant.title }} for {{ product.price | minus:variant.price | money_with_currency }}</label>
</li>
{% else %}
{% endif %}
{% endfor %}
</ul>
</fieldset>
{% else %}
<input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}">
{% endif %}
<input type='hidden' name='quantity' value='1'>
<input type="submit" name="add" id="add" class="inpost-buy w-button" value="Add to Bag →"></input>
</form>
Upvotes: 0
Views: 3663
Reputation: 15402
You need to make sure you are at least sending an id. I think you need a quantity too (you do if you use the cart api) but the straight form post may currently assume 1 if it's not given. I'd add a quantity. If you don't want it to show just use type=hidden
.
Your second problem is that your code doesn't have a fallback. If a product has options but not available variant you'll be stuck. Generally I use a variable to track whether or not I had a purchasable variant. You can do this a number of ways. I've added a test for that using the product.first_available_variant:
{% if product.first_available_variant == true %}
<form ...>
{% if product.options.size > 1 %}
...
<input type="radio" value="{{variant.id}}" name="id" {%if variant.id == product.selected_or_first_available_variant.id %} checked{% endif %} >
<label>{{ variant.title }} for {{ product.price | minus:variant.price | money_with_currency }}</label>
...
{% else %}
<input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}">
{% endif %}
<input type='hidden' name='quantity' value='1'>
</form>
{% else %}
<p>{{ 'no_product_available' | t }}</p> // or just some text if no locale support.
{% endif %}
Upvotes: 2