根据条件重置液体循环计数

问题描述 投票:0回答:1

我正在尝试从 Shopify 的推荐产品部分中消除停产的产品。使用 Liquid,我检查产品是否包含“停产”标签并跳过它。

我面临的问题是for循环的限制是4,所以当它遇到带标签的产品时,它会跳过,但当达到4时,它会停止,导致前面的产品数组不完整结束。

因此,我需要找到一种方法,当发现停产产品时将循环计数增加 1。

{%- for product in related_collection.products limit: number_of_products -%}
            {% comment %} On smaller screen sizes, 39vw is used for grid items in the CSS {% endcomment %}
            {% if product.tags contains 'discontinued' %}
              {% increment number_of_products %}
            {% else %}
                {%- render 'product-grid-item',
                    product: product,
                    per_row: section.settings.products_per_row,
                    quick_shop_enable: settings.quick_shop_enable,
                    fallback: '39vw',
                -%}
            {% endif %}
          {%- endfor -%}

上面的代码是我用来过滤产品的代码,但增量循环计数不起作用。

shopify liquid
1个回答
0
投票

设法找到解决方案:

{%- if related_collection.products_count > 0 -%}
      <div class="product-recommendations page-width">
        <div class="product-grid grid--uniform" data-aos="overflow__animation">
          {%- for product in related_collection.products -%}
            {% comment %} On smaller screen sizes, 39vw is used for grid items in the CSS {% endcomment %}
            {% unless product.tags contains 'discontinued' %}
                {%- render 'product-grid-item',
                    product: product,
                    per_row: section.settings.products_per_row,
                    quick_shop_enable: settings.quick_shop_enable,
                    fallback: '39vw',
                -%}
                {%- assign rendered_products = rendered_products | plus: 1 -%}
            {% endunless %}
            {% if rendered_products == number_of_products %}
              {% break %}
            {% endif %}
          {%- endfor -%}
        </div>
      </div>
    {%- endif -%}
© www.soinside.com 2019 - 2024. All rights reserved.