Shopify中如何让一件商品修改购物车信息?

How to get one product to modify information in the cart in Shopify?

如果客户单击我的 Shopify 商店中的某个按钮,我希望能够使用 Liquid(或任何需要的东西)将信息添加到我的购物车。此信息可以像字符串一样简单。客户不会看到此信息,但它将用于修改购物车页面的外观。如果这是我唯一的选择,我可以愉快地将它 it/handle 解析为字符串,但到目前为止我无法获得任何要发送的信息。我曾尝试使用 {% capture %} 块修改 cart.note,但最终并没有永久修改任何内容。我也不确定如何更改 line_item 属性,但这也可以。

您可以将名称为 properties[some-prop-name] 的输入字段添加到您的产品表单中,其中将包含所需的链接数据

例如,将以下输入添加到您的产品表单会在您添加产品时向您的产品添加一个行项目 属性:

<input type="checkbox" name="properties[_add_to_box]" value="{{ box_identifier }}" />

如果您想将行项目属性动态更新为 add/rearrange 框中的项目 post-hoc 您可以使用针对 AJAX 的请求来执行此操作=13=]端点

这是此类命令的示例,您将在用户更改适当的输入元素时 运行 使用该命令:

/* Assuming we have a variable named cart (containing the cart JSON) and the the 0-based index for the item we want to update */

var line = index + 1;  //Shopify uses 1-based indexing for line-items
var qty = cart.items[index].quantity;

jQuery.ajax({
  url:'/cart/change.js',
  type:'post',
  dataType:'json',
  data:{
   line: line, /* Line to edit: REQUIRED */
   quantity: qty, /* 'New' quantity - If omitted, the line will change to quantity 1! */
   properties:{
     /*
      This properties object will replace any existing properties object on the line-item, so be sure to include everything, not just what you're changing!
      Properties with keys that are prepended with an underscore (_) are hidden in Shopify's checkout page.  
     */
     _box_id: 'box01',
     'Congratulatory Message':'Hooray! You did it!'
   }
  },
  success:function(cart){
    console.log('Success!', cart)
  },
  error:function(err){
    alert('Something went wrong!', err)
  }
 
})

希望这可以帮助您使用您的功能!

您可以将该数据存储在行项目属性中。

Get customization information for products with line item properties

You can collect customization information for products using line item properties. Line item properties are custom form fields that you can add to the product page, allowing customers to make choices or add information about a product. For example, if you offer product engraving, then you can use line item properties to let customers enter the text that they want engraved on the product.

然后您可以从 liquid 模板访问订单项属性。