2013-10-24 30 views
2

的相關商品我已經構建了一個液體模板,用於比較當前產品的標籤和商店中所有其他產品的標籤,並在頁面底部顯示四個相關產品。Shopify

它可行,但我認爲我做得相當低效。有沒有辦法讓這項工作更好一點?

{% if settings.products_per_row == "4" %} 
{% assign number_of_related_products_to_show = 4 %} 
{% elsif settings.products_per_row == "3" %} 
{% assign number_of_related_products_to_show = 3 %} 
{% else %} 
{% assign number_of_related_products_to_show = 2 %} 
{% endif %} 

{% assign number_of_related_products_to_fetch = number_of_related_products_to_show | plus: 1 %} 
{% assign current_product_tags = product.tags %} 

{% for c in collections %} 
    {% if c.handle == 'all' %} 
     {% assign collection_all = c %} 
    {% endif %} 
{% endfor %} 

{% assign found_first_match = false %} 
{% assign found_second_match = false %} 
{% paginate collection_all.products by 1000 %} 
{% for product in collection_all.products %} 
{% for tag in product.tags %} 
    {% if current_product_tags contains tag and found_first_match == false and tag != 'Made in USA' %} 
     {% assign found_first_match = true %} 
     {% assign first_match = tag %} 
    {% endif %} 
    {% if current_product_tags contains tag and found_first_match == true and tag != first_match and tag != 'Made in USA' %} 
     {% assign found_second_match = true %} 
     {% assign second_match = tag %} 
    {% endif %} 
{% endfor %} 
{% endfor %} 
{% endpaginate %} 

{% assign matches_found = false %} 
{% assign current_product = product %} 
{% assign current_product_found = false %} 

{% paginate collection_all.products by 1000 %} 
{% for product in collection_all.products %} 
    {% if product.handle == current_product.handle %} 
    {% assign current_product_found = true %} 
    {% else %} 
    {% if product.tags contains first_match %} 
     {% unless current_product_found == false and forloop.last %} 
     {% assign matches_found = true %} 
     {% endunless %} 
    {% endif %} 
    {% if product.tags contains second_match and matches_found == false %} 
     {% unless current_product_found == false and forloop.last %} 
     {% assign matches_found = true %} 
     {% endunless %} 
    {% endif %} 
    {% endif %} 
{% endfor %} 
{% endpaginate %} 

{% if matches_found == true %} 

<div class="row"> 
    <div class="span12"> 
    <h3 class="collection-title">Related products</h3> 
    </div> 
</div> 

<div class="row products"> 

{% paginate collection_all.products by 1000 %} 
{% for product in collection_all.products %} 
    {% if product.handle == current_product.handle %} 
    {% assign current_product_found = true %} 
    {% else %} 
    {% if product.tags contains first_match %} 
     {% unless current_product_found == false and forloop.last %} 
     {% include 'related-product-loop' with collection.handle %} 
     {% assign matched_product = product.title %} 
     {% endunless %} 
    {% endif %} 
    {% if product.tags contains second_match %} 
     {% unless current_product_found == false and forloop.last or matched_product == product.title %} 
     {% include 'related-product-loop' with collection.handle %} 
     {% endunless %} 
    {% endif %} 
    {% endif %} 
{% endfor %} 
{% endpaginate %} 

</div> 

{% endif %} 

{{ 'jquery.pick.js' | asset_url | script_tag }} 
<script type="text/javascript" charset="utf-8"> 
//<![CDATA[ 
var howMany = {{ number_of_related_products_to_show }}; 
jQuery(function() { 
    jQuery('.products .product').pick(howMany); 
}); 
//]]> 
</script> 

我正在使用jquery.pick.js隨機顯示四個產品。

想法?

+0

實際上,僅基於標籤顯示相關產品並不是一個好主意。您應該使用過去的訂單數據,標籤,集合和許多其他因素來選擇您的客戶感興趣的相關產品。不可能(由於性能問題)實施此類高級算法以通過使用液體模板來選擇相關產品發動機。我爲Shopify開發了Recomify相關產品應用程序(https://apps.shopify.com/recomify),使所有這些工作變得簡單,快速和絕對自動化。 –

+0

@FeridMovsumov停止發送垃圾郵件併爲您的應用做廣告! –

回答

1

我建議看一看Shopify wiki上的這篇文章:Related Products

也許第3. Finding a relevant collection部分中使用的方法可能是實施相關產品的更簡潔方法。但是,如果您需要使用產品標籤,則在4. Using product tags部分中也有解釋如何執行此操作。

編輯:也許你的代碼可以簡化一些這樣的事情。它與上面的內容非常相似,只是將其縮減爲一個循環而不是三個。

{% if settings.products_per_row == "3" or settings.products_per_row == "4" %} 
    {% assign number_of_related_products_to_show = settings.products_per_row | times: 1 %} 
{% else %} 
    {% assign number_of_related_products_to_show = 2 %} 
{% endif %} 

{% assign current_product = product %} 
{% assign current_product_tags = product.tags %} 
{% assign found_first_match = false %} 
{% assign found_second_match = false %} 
{% assign first_related_product = true %} 

{% paginate collections.all.products by 1000 %} 
{% for product in collections.all.products %} 
    {% unless product.handle == current_product.handle %} 
    {% for tag in product.tags %} 
     {% if current_product_tags contains tag and tag != 'Made in USA' %} 
     {% if found_first_match == false %} 
      {% assign found_first_match = true %} 
      {% assign first_match = tag %} 
     {% elsif found_second_match == false %} 
      {% assign found_second_match = true %} 
      {% assign second_match = tag %} 
     {% endif %} 
     {% endif %} 
    {% endfor %} 

    {% if found_first_match == true %} 
     {% if first_related_product == true %} 
     {% assign first_related_product == false %} 

     <div class="row"> 
      <div class="span12"> 
      <h3 class="collection-title">Related products</h3> 
      </div> 
     </div> 
     <div class="row products"> 

     {% endif %} 
     {% if product.tags contains first_match or product.tags contains second_match %} 
     {% include 'related-product-loop' with collection.handle %} 
     {% endif %} 
    {% endif %} 
    {% endunless %} 
{% endfor %} 
{% if first_related_product == false %} </div> {% endif %} 
{% endpaginate %} 

{{ 'jquery.pick.js' | asset_url | script_tag }} 
<script type="text/javascript" charset="utf-8"> 
//<![CDATA[ 
    var howMany = {{ number_of_related_products_to_show }}; 
    jQuery(function() { 
    jQuery('.products .product').pick(howMany); 
    }); 
//]]> 
</script> 

此代碼的要訣here。我包含了2個文件,第二個選項在產品中使用了2個循環,但可能更具可讀性。 (我無法在兩者之間做出決定,因此我將兩者都包括在內)。

+1

我很欣賞這種迴應。我實際上看到了這兩個(這個代碼實際上是基於相關的收集解決方案),但客戶希望它使用他們已經建立的預先存在的標籤系統,因此該文檔中的標籤解決方案被證明是無法使用的。 :/ – JohnBuck

+0

@JohnBuck我編輯了我的答案......也許你所需要做的就是整理你有一點點,如果它適合你的情況。我玩弄了三個循環,通過一個產品減少。 –

+0

太棒了。感謝那。這看起來太棒了。 :) – JohnBuck