Verwandte Produkte Tag für Tag in Shopify

Habe ich gebaut aus einer flüssigen Vorlage zu vergleichen, die tags der aktuellen Produkte mit den tags von allen anderen Produkten im Shop-und display-vier zugehörigen Produkten am unteren Rand der Seite.

Es funktioniert, aber ich denke, ich habe getan, es ziemlich ineffizient. Gibt es eine Möglichkeit, diese Arbeit zu machen ein wenig besser?

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

Ich bin mit jquery.pick.js zufällig anzeigen vier der Produkte.

Gedanken?

  • Eigentlich ist es nicht eine gute Idee, um zu zeigen Verwandte Produkte nur auf Basis von tags. Sie verwenden sollten der Vergangenheit, um Daten, tags, Sammlungen und viele andere Faktoren zu wählen, Verwandte Produkte, die Ihre Kunden interessieren wird. Es ist nicht möglich (wegen der performance-Probleme) Implementierung von fortgeschrittenen algorithmen auswählen, Verwandte Produkte, die nur mit Flüssigkeit template-engine. Ich entwickelte Recomify Verwandte Produkte App ( apps.shopify.com/recomify ) für Shopify, um alle diese arbeiten einfach, schnell und absolut automatisch.
  • Hört auf zu Spammen und Werbung für Ihre app!
InformationsquelleAutor JohnBuck | 2013-10-24
Schreibe einen Kommentar