Shopify主题开发|如果在集合内FOR循环|检查产品是否还存在于其他集合中

时间:2019-10-21 14:22:24

标签: for-loop if-statement collections shopify

我有一个困扰我的问题,我无法在其他地方获得有价值的资源;真的很感激任何可以帮助我的人。

我有一个collection.list.liquid文件。

这将遍历商店中的所有产品。

{% for product in collection.products %}

我创建了另一个产品集合,这些集合巧妙地命名为“特价商品”,带有url和句柄“ / special-offers-products”。

在我的FOR循环中,我想做一个IF语句,简单地询问FOR循环中的每个产品是否也包含在此“特殊优惠产品”附加产品中。如果为TRUE,则显示一小段代码,其本质上是一个标有“ Special Offer!”的标签。

我幼稚地尝试过(但失败了):

{% if product.collections contains 'special-offers-products' %}     
    <div class="special-offer-banner">
        Special<br/>
        Offer!
    </div>
{% endif %}

我现在正在努力想办法运行该IF。

这是我完整的FOR循环产品:

{% for product in collection.products %}
    <div class="single-product">
        <a href="{{ product.url | within: collection }}" class="box-link"></a>
        <div class="product-image" style="background-image: url({{ product.featured_image.src | img_url: 'large' }});">
        </div>
        {% if product.collections contains 'special-offers-products' %}     
            <div class="special-offer-banner">
                Special<br/>
                Offer!
            </div>
        {% endif %}
        <div class="product-information">
            <p class="product-title">{{ product.title }}</p>
            <p class="product-vendor">{{ product.vendor }}</p>
            <p class="product-price">{{ product.price | money }}</p>
            {% unless product.available %}
                <br><strong>sold out</strong>
            {% endunless %}
            <div class="product-buttons">
                {% include 'view-button' %}
                {% comment %}{% include 'add-to-cart-button' %}{% endcomment %}
            </div>
        </div>
    </div>
{% else %}
    <p>no matches</p>
{% endfor %}

请问有人可以建议我如何正确检查当前正在循环播放的产品是否也放置在另一个收藏夹中吗?

任何帮助将不胜感激...

编辑:我不想知道如何使用“特惠产品”集合创建单独的产品集合,而这需要在同一“所有产品”集合循环中完成。 >

谢谢杰森。

1 个答案:

答案 0 :(得分:0)

你实际上很亲近。

让您感到困惑的是,product.collections返回的是collections对象,而不是collection句柄。

所以您可以改为这样做。

{%- assign collection_handles = product.collections | map: 'handle' -%}
{% if collection_handles contains 'special-offers-products' %}     
    <div class="special-offer-banner">
        Special<br/>
        Offer!
    </div>
{% endif %}

那么这应该与您当前的逻辑一起工作。