简体   繁体   English

检查产品是否也存在于 Shopify 主题的不同集合中

[英]Checking whether product also exists in a different collection in a Shopify Theme

I have a collection.list.liquid file.我有一个 collection.list.liquid 文件。

This loops through ALL the products in the store.这会遍历商店中的所有产品。

{% for product in collection.products %}

I have created another collection of products that are in a collection cleverly titled 'Special Offers Products', with the url and handle '/special-offers-products'.我创建了另一个产品系列,这些产品系列巧妙地命名为“特价产品”,带有 url 并处理“/特价产品”。

Within my FOR loop I want to make an IF statement that simply asks each product within the FOR loop if it is also within this side collection 'Special Offers Products'.在我的 FOR 循环中,我想创建一个 IF 语句,它只是询问 FOR 循环中的每个产品是否也在这个侧面集合“特价产品”中。 If this is TRUE display a little block of code that is essentially a tag that says 'Special Offer.'.如果为真,则显示一小段代码,本质上是一个标有“特价商品”的标签。

Childishly I have tried (and failed):我幼稚地尝试过(但失败了):

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

I am now struggling to think of another way I could run this IF.我现在正在努力想另一种方式来运行这个 IF。

Here is my full product FOR loop:这是我的完整产品 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 %}

How can I to correctly check if the product that is currently being looped over is also placed within another collection?如何正确检查当前循环的产品是否也放置在另一个集合中?

Edit编辑

I do not want to know how to create a separate collection of products using the 'Special Offers Products' collection, this needs to be done within the same ALL products collection loop.我不想知道如何使用“特价商品”集合创建单独的产品集合,这需要在同一个 ALL 产品集合循环中完成。

You were actually very close.你实际上非常接近。

What's confusing you is that product.collections returns the collections object and not the collection handle.让您感到困惑的是product.collections返回 collections object 而不是收集句柄。

So you can do this instead.所以你可以这样做。

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

This should work with your current logic then.这应该适用于您当前的逻辑。

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

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