【问题标题】:How to exclude sold out products from related products section Shopify?如何从相关产品部分 Shopify 中排除已售罄的产品?
【发布时间】:2018-03-30 11:23:45
【问题描述】:

我试图防止售罄的产品显示在我的 Shopify 商店的相关产品部分。我尝试在代码开头使用{% if product.available %},但没有成功。

这是本节的代码 -

{% if collection and collection.all_products_count > 1 %}
  {% assign col = collection.handle %}
{% else %}
  {% assign col = product.collections.last.handle %}
{% endif %}

{% for tag in product.tags %}
  {% if tag contains 'meta-related-collection-' %}
    {% assign related_collection_handle = tag | remove: 'meta-related-collection-' %}
    {% if collections[related_collection_handle].all_products_count > 0 %}
      {% assign col = related_collection_handle %}
      {% assign collection = collections[col] %}
    {% endif %}
  {% endif %}
{% endfor %}

{% if col %}
  {% if collections[col].all_products_count != 1 or collections[col].products.first.id != product.id %}
    {% assign skip_product = product %}
    {% assign products = collections[col].products %}
    {% unless sidebar %} <div class="container"> {% endunless %}
      <div class="related-products__title {% unless section.settings.related_products_style == 'slider' %}{% if sidebar %}twelve columns{% else %}sixteen columns{% endif %}{% endunless %}">
        <h4 class="title center">{{ 'products.product.related_items' | t }}</h4>
        <div class="feature_divider"></div>
      </div>
      <div class="clear"></div>
    {% unless sidebar %} </div> {% endunless %}

    {% if section.settings.related_products_style == 'slider' %}
      {% assign limit = section.settings.related_products_limit %}
      <div class="related-products related-products--slider js-related-products-slider">
        {% if col and collections[col].all_products_count > 0 and product.available %}
          {% include 'product-slider', related_products: true %}
        {% endif %}
      </div>

    {% else %}
      {% assign limit = section.settings.related_products_limit | plus: 1 %}
      {% assign products_per_row = section.settings.products_per %}
      {% if col and collections[col].all_products_count > 0 and product.available %}
        {% unless sidebar %}<div class="container related-products--grid">{% endunless %}
          <div class="{% if sidebar %}twelve{% else %}sixteen{% endif %} columns">
            {% include 'product-loop', related_products: true %}
          </div>
        {% unless sidebar %}</div>{% endunless %}
      {% endif %}
    {% endif %}
  {% endif %}
{% endif %}

希望能得到一些帮助,谢谢!

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    您的相关集合分配看起来有点复杂,但应该可以解决问题。

    问题出现在第二部分。一旦你的集合被定义。这种代码应该可以工作:

    {% if col %}
        <!-- First, memorize your current product handle -->
        {% assign current_product_handle = product.handle %}
        <!-- Then open the loop to get products inside related collection -->
        {% for product in col.products %}
          <!-- Filter your results to avoid identic product and out of stock products -->
          {% unless product.handle == current_product_handle %}
            {% if product.available %}
              {{ product.title }} and other things you might want to display about related products.
            {% endif %}
          {% endunless %}
        {% endfor %}
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2013-08-11
      • 1970-01-01
      相关资源
      最近更新 更多