2015-04-27 46 views
0

遇到Shopify中調整「以前的產品」和「下一個產品」功能的問題。循環瀏覽集合中的所有產品| SHOPIFY

我想設置它,以便'上一個'和'下一個'循環遍歷所有產品,傳送帶樣式。

一旦你在上一個產品,'下一個'將帶你回到第一個。目前,一旦你達到最後,你只能去到以前的產品,並通過列表後退。

有什麼想法?

這裏是我的基本代碼:

{% if collection.previous_product %} 

    {{ '«' | link_to: collection.previous_product }} 

    {% endif %} 

    {% if collection.next_product %} 

    {% if collection.previous_product %} | {% endif %} 

    {{ '»' | link_to: collection.next_product }} 

    {% endif %} 

回答

0

我不是超級熟悉Shopify,但我看了看他們的文檔,看到了收藏對象包括colection.products_countcollection.products。如果collection.products包含的產品陣列,他們的方法綁定到這些,你可以做這樣的事情:

{% if collection.previous_product %} 

    {{ '«' | link_to: collection.previous_product }} 

{% else %} 

    {{ '«' | link_to: collection.products[collection.products_count - 1].url }} 

{% endif %} 

{% if collection.next_product %} 

    {{ '»' | link_to: collection.next_product }} 

{% else %} 

    {{ '»' | link_to: collection.products[0].url }} 

{% endif %} 

我似乎無法找到一個shopify沙盒測試中,但假設collection.products回報一個數組,這應該工作。

2

嘗試這樣:

{% if collection.previous_product %} 
    {{ '«' | link_to: collection.previous_product }} 
{% else %} 
    {% assign last_product_url = collection.products.last.url | within:collection %} 
    {{ '«' | link_to: last_product_url }} 
{% endif %} 

{% if collection.next_product %} 
    {{ '»' | link_to: collection.next_product }} 
{% else %} 
    {% assign first_product_url = collection.products.first.url | within:collection %} 
    {{ '»' | link_to: first_product_url }} 
{% endif %} 

如果你有超過50個產品您的收藏中,你應該注意的是collection.products is paginated

See here有關within過濾器的信息。

相關問題