首页 > 解决方案 > 如何在 ajax-cart-template.liquid 中获取产品变体及其标签的库存?

问题描述

我需要在我的购物车的“备注”字段中自动填写“预购”一词,只要库存小于零并且产品具有“预购”标签。

我已经尝试过使用此代码,但是在选择其他产品时它不会自动更新,可能是因为购物车与 ajax 一起使用

{% endraw %}
{% assign product_qty = 0 %}
{% for variant in product.variants %}
{% if variant.inventory_quantity <= 0 %}
{% if variant.selected %}
{% assign product_qty = variant.inventory_quantity %}
<p style="display:none">{{product_qty}}</p>
{% endif%}

{% assign product_qty = product_qty | plus: variant.inventory_quantity %}
{% endif %}
{% endfor %}


{% assign isTagAvailable = false %}

{% for item in cart.items %}

{% if item.product.tags contains 'pre-order1' %}
   {% assign isTagAvailable = true %}
{% endif %}

{% endfor %}

{% if isTagAvailable == true and product_qty <= 0   %}
        <div class="spec-instructions" style="display:none">
    <label for="CartSpecialInstructions">Please leave special instructions below:</label>
    <textarea name="note" class="input-full" id="CartSpecialInstructions">Pre-order {{ note }} 
 </textarea>
    </div>
{% endif %}

 {% raw %}
    {% endraw %}{% if settings.cart_notes_enable %}{% raw %}

    {% endraw %}{% endif %}{% raw %}

标签: ajaxshopifyliquidcart

解决方案


推荐阅读