2017-07-26 6 views
0

私の商品ページには「関連商品」のセクションがあります。今は "コレクション"に関連する商品を表示しています。同じタグを持つ関連商品を表示することは可能ですか?関連する商品をタグ(Shopify)で表示

これは私の関連製品です。コードです。

ありがとうございました。

{% 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 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

関連する問題