Ad

Custom Add To Cart Button Shopify

- 1 answer

I am just trying to create an add to cart button to use on template and sections on my Shopify theme. Very simple.

Here is my code:

<form method="post" action="/cart/">
  <input name="id" value="1402808270917" type="hidden" />
  <input name="add" value="ORDER NOW" type="submit" />
</form>

Note: the value is the product id which I need to be able to hard code.

I'm not getting any errors, it's just going to the cart page stating cart is empty. The weird thing is, if I have something in the cart already, it will add a duplicate of the product.

Ad

Answer

You form need to action /cart/add

And you can't add to cart a product with the product id, you can only achieve this with a variant id.

If your product have only the default variant you can use this :

<form method="post" action="/cart/add">
  <input name="id" value="{{ product.variants.first.id }}" type="hidden" />
  <input name="add" value="ORDER NOW" type="submit" />
</form>
Ad
source: stackoverflow.com
Ad