2017-07-26 81 views
0

我的產品頁面上有「相關產品」部分。現在它顯示了一個由「集合」相關的產品。是否有可能展示具有相同標籤的相關產品?按標籤顯示相關商品(Shopify)

這是我的related-products.liquid code。

謝謝。

{% assign number_of_products = 4 %} 
{% assign number_of_products_to_fetch = number_of_products | plus: 1 %} 

{% if collection == null or collection.handle == 'frontpage' or collection.handle == 'all' %} 
    {% assign found_a_collection = false %} 
    {% for c in product.collections %} 
     {% if found_a_collection == false and c.handle != 'frontpage' and c.handle != 'all' and c.all_products_count > 1 %} 
     {% assign found_a_collection = true %} 
     {% assign collection = c %} 
     {% endif %} 
    {% endfor %} 
{% endif %} 

{% if collection and collection.products_count > 1 %} 
    <div class="related"> 
     <h1>You Might Also Like</h1> 
     <div class="products clearfix"> 
     {% assign current_product = product %} 
     {% assign current_product_found = false %} 

     {% for product in collection.products limit: number_of_products_to_fetch %} 
      {% if product.handle == current_product.handle %} 
       {% assign current_product_found = true %} 
      {% else %} 
       {% unless current_product_found == false and forloop.last %} 
        <li> 
        <a href="{{ product.url | within: collection }}" class="product__image" title="{{ product.title | escape }}"> 
         <img src="{{ product.featured_image.src | img_url: '350x350' }}" alt="{{ product.featured_image.alt | escape }}"> 
        </a> 
        </li> 
       {% endunless %} 
      {% endif %} 
     {% endfor %} 
     </div> 
    </div> 
{% endif %} 
+0

您想在集合中顯示標記的產品嗎? –

回答

0

讓我們改變了你的for循環遍歷項目的集合,而忽略那些項目沒有標籤。

 {% for product in collection.products limit: number_of_products_to_fetch %} 
     {% if product.handle == current_product.handle %} 
      {% assign current_product_found = true %} 
     {% else %} 
      {% unless current_product_found == false and forloop.last %} 
      {% if current_product_found.tags contains 'best-tag-ever' %} 
       <li> 
       <a href="{{ product.url | within: collection }}" class="product__image" title="{{ product.title | escape }}"> 
        <img src="{{ product.featured_image.src | img_url: '350x350' }}" alt="{{ product.featured_image.alt | escape }}"> 
       </a> 
       </li> 
      {% endif %} 
      {% endunless %} 
     {% endif %} 
    {% endfor %} 

注意!這仍然只會遍歷所提供的集合。這意味着將出現的產品需要a)包含在此類集合中,並且b)標記爲'best-tag-ever'。如果您想展示整個商店中的產品而不是上述產品系列,則可以將該產品系列替換爲all系列,該系列包含您商店中的所有產品。

+0

我不想硬編碼標籤或集合,我只是想shopify顯示相關產品,該特定的產品。 – chrisbedoya