【问题标题】:How to add second product to cart in Shopify from product page如何从产品页面将第二个产品添加到 Shopify 中的购物车
【发布时间】:2020-05-04 18:36:31
【问题描述】:

我正在使用 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 %}

【问题讨论】:

    标签: shopify shopify-api


    【解决方案1】:

    您的代码似乎不是要在产品页面上使用的。看起来它应该放在 {% for item in cart.items %} ... {% endfor %} 循环内的购物车页面上。

    此外,即使客户添加了 2 个以上的包装商品,此代码也只会添加 1 个包装商品。我会将代码更改为如下所示:

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

    我希望以上内容有意义。

    【讨论】:

    • 谢谢你。不幸的是,这仍然不起作用。似乎更新购物车的脚本是不起作用的部分。我把它放在你提到的购物车页面上。难道是因为我处于预览模式?
    • 你能留下一个链接到你的网站/预览来检查吗?你用什么主题?
    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    相关资源
    最近更新 更多