我正在使用Shopify,并试图添加选项来添加礼品包装和一个手提箱的产品购买。
我可以使用 item.properties
要求用户进行选择,并显示在购物车中。
现在,我想添加一个额外的产品到购物车,如果 item.properties
被设置为 "礼品包装 "或 "手提箱"。
这个代码放在product-template.liquid中,但无法使用。
{% for property in item.properties %}
{% if property.last == "Gift Wrap" %}
<p>This would add gift wrap.</p>
<script>
jQuery.post('/cart/update.js', {
updates: {
32005672697928: 1
}
});
</script>
{% endif %}
{% if property.last == "Carry Strap" %}
<p>This would add carry strap.</p>
{% endif %}
{% endfor %}
{% endunless %}
你的代码似乎并不打算用在产品页面上。它看起来像是应该放在购物车页面中的 {% for item in cart.items %} ... {% endfor %}
循环。
另外,这段代码只会添加1个包邮产品,即使客户添加了2个以上的包邮产品。我会把代码改成下面这样。
{%- assign numWrappedItems = 0 -%}
{%- for item in cart.items -%}
{%- for property in item.properties -%}
{%- if property.last == "Gift Wrap" -%}
{%- assign numWrappedItems = numWrappedItems | plus: item.quantity -%}
{%- break -%}
{%- endif -%}
{%- endfor -%}
...
{%- endfor -%}
{%- if numWrappedItems > 0 -%}
<script>
jQuery.post('/cart/update.js', {
updates: {
32005672697928: {{ numWrappedItems }}
}
});
</script>
我希望上面的代码有意义