简体   繁体   English

如何从产品页面将第二个产品添加到 Shopify 中的购物车

[英]How to add second product to cart in Shopify from product page

I'm using Shopify and am trying to add the options to add gift wrap and a carry case to products being bought.我正在使用 Shopify 并尝试添加选项以向所购买的产品添加礼品包装和手提箱。

I can use the item.properties to ask the user for the choice and show that in the cart.我可以使用item.properties来询问用户的选择并将其显示在购物车中。

I now want to add an additional product to the cart if item.properties is set to "gift wrap" or "carry case".如果item.properties设置为“礼品包装”或“手提箱”,我现在想向购物车添加额外的产品。

This code is placed in the product-template.liquid but does not work:此代码放在 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 %}

Doesn't seem the code you have was intended to be used on the product page.您的代码似乎不是打算在产品页面上使用的。 It looks like it should be placed on the cart page within the {% for item in cart.items %}... {% endfor %} loop.看起来它应该放在购物车页面的{% for item in cart.items %}... {% endfor %}循环内。

Also, this code will add only 1 wrap product, even if customers add 2+ wrapped items.此外,即使客户添加了 2 个以上的包装商品,此代码也只会添加 1 个包装产品。 I would change the code to something like the below:我会将代码更改为如下所示:

{%- 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>

I hope the above makes sense.我希望以上是有道理的。

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

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