简体   繁体   English

自定义添加到购物车按钮Shopify

[英]Custom add to cart button Shopify

I am just trying to create an add to cart button to use on template and sections on my Shopify theme. 我只是想创建一个“添加到购物车”按钮以在Shopify主题的模板和部分上使用。 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. 注意:该value是我需要能够硬编码的产品ID。

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. 奇怪的是,如果我已经在购物车中放东西了,它将添加该产品的副本。

You form need to action /cart/add 您需要采取行动/cart/add

And you can't add to cart a product with the product id, you can only achieve this with a variant id. 您不能将带有产品ID的产品添加到购物车中,而只能通过变体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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM