New Shopify Certification now available: Liquid Storefronts for Theme Developers

"Add to Cart" button on my collection pages

RLyndon
Visitor
3 0 0

I want to add the add to cart option on my collection pages, but when I do it wont actually let me add the item to my cart. I have had some minor custom coding done to my site but this issue started more recently. Does anyone know how to fix this? www.gildedhoney.ca - currently editing in a duplicate theme fyi

 

Error message is:

Something went wrong.

What happened?

Parameter Missing or Invalid: Required parameter missing or invalid: id
 
Reply 1 (1)
RobDukarski
Shopify Partner
88 15 20

@RLyndon 

That error occurs when the product's variant ID is not supplied with the request to add the product to the cart. If you're adding the add to cart button to the product grid item in the collection you will want to do something like this:

{% if product.available and product.has_only_default_variant %}
  {% form 'product', product -%}
    <input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}">
    <button type="submit" name="add" class="btn secondary-button" data-cart-action="drawer">Add to Cart</button>
  {% endform %}
{% endif %}

 

Which checks if the product is available and does not have more than one variant then should display the add to cart button for the product to be added to the cart. I am not sure how your theme is set up but that may work for you.

If you already had a solution you may simply need to ensure the variant ID is part of the form submission or Cart AJAX API submission.

Hope that helps!

- Rob Dukarski
Helping to make commerce better for everyone!