How to only show products in Dynamic Recommendations that are in a collection?

Highlighted
New Member
8 0 0

I use the Simple theme on a general store. I was wondering if I can code the dynamic product recommendations in the product page so that it shows only products that are in the first collection of the main product being shown.

For example, if someone is looking at a dog toy which is part of a collection called "dog products", with the "dynamic recommendations" feature,  I want to recommend other 'dog products' and not some other random product like hiking gear since I do run a general store.

Also to avoid complications since most of my products are also in a collection called "recently added" for new arrivals, I just want it to use the first collection of the stack of collections that the product is in.

Note: This was also posted in the Shopify Script edit section but I have no idea how to delete posts.

0 Likes
Explorer
52 6 20

Hey so I was unable to test the related products code on my test site since I don't have enough products on my store, likely also takes time to come into effect. I wrote some code that should give you the desired results. Just make sure to copy your old code and save it somewhere just in case it doesn't work as expected. The code you will want to edit is sections > product-recommendations.liquid. The code I wrote is basically the same as the original code the only difference is that it compares the products first collection to the first collections of all the related products. If they match then the product will be displayed. Paste this code over your code in the product-recommendations.liquid section:

{%-assign first_collection = product.collections.first.handle -%}
{%- if section.settings.show_product_recommendations -%}
  {%- if recommendations.performed -%}
    {%- if recommendations.products_count > 0 -%}
      <hr>
      {%- if section.settings.heading != blank -%}
        <h2 class="section-header__title h3">{{ section.settings.heading | escape }}</h2>
      {%- endif -%}
      <div class="grid grid--uniform">
        {%- assign grid_item_width = 'medium-up--one-quarter small--one-half' -%}
        
        {%- for product in recommendations.products -%}
        	{%if product.collections.first.handle == first_collection -%}
        		{% include 'product-grid-item' %}
        	{%-endif-%}
        {%- endfor -%}
      </div>
    {%- endif -%}
  {%- else  -%}
    <div data-product-id="{{ product.id }}" data-section-id="{{ section.id }}" data-section-type="product-recommendations"></div>
  {%- endif -%}
{%- endif -%}
{% schema %}
{
  "name": {
    "da": "Produktanbefalinger",
    "de": "Produktempfehlungen",
    "en": "Product recommendations",
    "es": "Recomendaciones de productos",
    "fi": "Tuotesuositukset",
    "fr": "Recommandations de produits",
    "hi": "उत्पाद की अनुशंसाएं",
    "it": "Raccomandazioni sui prodotti",
    "ja": "商品の推奨",
    "ko": "제품 권장 사항",
    "ms": "Cadangan produk",
    "nb": "Produktanbefalinger",
    "nl": "Productaanbevelingen",
    "pt-BR": "Recomendações de produtos",
    "pt-PT": "Recomendações de produtos",
    "sv": "Produktrekommendationer",
    "th": "คำแนะนำสินค้า",
    "zh-CN": "产品推荐",
    "zh-TW": "商品推薦"
  },
  "settings": [
    {
      "type": "checkbox",
      "id": "show_product_recommendations",
      "label": {
        "da": "Vis dynamiske anbefalinger",
        "de": "Dynamische Empfehlungen anzeigen",
        "en": "Show dynamic recommendations",
        "es": "Mostrar recomendaciones dinámicas",
        "fi": "Näytä dynaamiset suositukset",
        "fr": "Afficher les recommandations dynamiques",
        "hi": "डायनेमिक सुझाव दिखाएं",
        "it": "Mostra raccomandazioni dinamiche",
        "ja": "動的推奨を表示する",
        "ko": "동적 권장 사항 표시",
        "ms": "Tunjukkan cadangan dinamik",
        "nb": "Vis dynamiske anbefalinger",
        "nl": "Dynamische aanbevelingen weergeven",
        "pt-BR": "Mostrar recomendações dinâmicas",
        "pt-PT": "Mostrar recomendações dinâmicas",
        "sv": "Visa dynamiska rekommendationer",
        "th": "แสดงคำแนะนำแบบไดนามิก",
        "zh-CN": "显示动态推荐",
        "zh-TW": "顯示動態推薦"
      },
      "info": {
        "da": "Dynamiske anbefalinger ændres og forbedres med tiden. [Få mere at vide](https://help.shopify.com/en/themes/development/recommended-products)",
        "de": "Dynamische Empfehlungen werden im Laufe der Zeit angepasst und verbessert. [Mehr Informationen](https://help.shopify.com/en/themes/development/recommended-products)",
        "en": "Dynamic recommendations change and improve with time. [Learn more](https://help.shopify.com/en/themes/development/recommended-products)",
        "es": "Las recomendaciones dinámicas cambian y mejoran con el tiempo. [Más información](https://help.shopify.com/en/themes/development/recommended-products)",
        "fi": "Dynaamiset suositukset muuttuvat ja paranevat ajan myötä. [Lisätietoja](https://help.shopify.com/en/themes/development/recommended-products)",
        "fr": "Les recommandations dynamiques changent et s'améliorent avec le temps. [En savoir plus](https://help.shopify.com/en/themes/development/recommended-products)",
        "hi": "समय के साथ डायनेमिक सुझाव परिवर्तन और सुधार. [अधिक जानें](https://help.shopify.com/en/themes/development/recommended-products)",
        "it": "Le raccomandazioni dinamiche cambiano e migliorano nel tempo. [Maggiori informazioni](https://help.shopify.com/en/themes/development/recommended-products)",
        "ja": "動的推奨は時間とともに変化し改善します。[詳細情報](https://help.shopify.com/en/themes/development/recommended-products)",
        "ko": "동적 권장 사항은 시간이 지나면서 변하고 개선됩니다. [자세히 알아보기](https://help.shopify.com/en/themes/development/recommended-products)",
        "ms": "Cadangan dinamik bertukar dan bertambah baik dari masa. [Ketahui lebih lanjut](https://help.shopify.com/en/themes/development/recommended-products)",
        "nb": "Dynamiske anbefalinger endrer seg og forbedres med tiden. [Finn ut mer](https://help.shopify.com/en/themes/development/recommended-products)",
        "nl": "Dynamische aanbevelingen veranderen en verbeteren mettertijd. [Meer informatie](https://help.shopify.com/en/themes/development/recommended-products)",
        "pt-BR": "As recomendações dinâmicas mudam e melhoram com o tempo. [Saiba mais](https://help.shopify.com/en/themes/development/recommended-products)",
        "pt-PT": "As recomendações dinâmicas mudam e melhoram com o tempo. [Saiba mais](https://help.shopify.com/en/themes/development/recommended-products)",
        "sv": "Dynamiska rekommendationer ändras och förbättras med tiden. [Läs mer](https://help.shopify.com/en/themes/development/recommended-products)",
        "th": "คำแนะนำแบบไดนามิกนั้นเปลี่ยนแปลงและถูกปรับปรุงให้ดีขึ้นตลอดเวลา [เรียนรู้เพิ่มเติม](https://help.shopify.com/en/themes/development/recommended-products)",
        "zh-CN": "动态推荐会随着时间而变化和改进。[了解详情](https://help.shopify.com/en/themes/development/recommended-products)",
        "zh-TW": "動態推薦會隨著時間改變與改進。[深入瞭解](https://help.shopify.com/en/themes/development/recommended-products)"
      },
      "default": true
    },
    {
      "type": "text",
      "id": "heading",
      "label": {
        "da": "Overskrift",
        "de": "Überschrift",
        "en": "Heading",
        "es": "Título",
        "fi": "Otsake",
        "fr": "En-tête",
        "hi": "शीर्षक",
        "it": "Heading",
        "ja": "見出し",
        "ko": "제목",
        "ms": "Tajuk",
        "nb": "Overskrift",
        "nl": "Kop",
        "pt-BR": "Título",
        "pt-PT": "Título",
        "sv": "Rubrik",
        "th": "ส่วนหัว",
        "zh-CN": "标题",
        "zh-TW": "標題"
      },
      "default": {
        "da": "Du vil muligvis også synes om",
        "de": "Das könnte Ihnen auch gefallen",
        "en": "You may also like",
        "es": "También te puede interesar",
        "fi": "Saatat pitää myös näistä",
        "fr": "Vous pourriez aimer également",
        "hi": "आप शायद इसे भी पसंद करें",
        "it": "Potrebbero interessarti anche",
        "ja": "あなたへのおすすめ",
        "ko": "회원님도 좋아할 것입니다.",
        "ms": "Anda juga mungkin sukakan",
        "nb": "Kanskje du også liker",
        "nl": "Wellicht vindt je  dit ook leuk",
        "pt-BR": "Talvez você também goste de",
        "pt-PT": "Também poderá gostar de",
        "sv": "Du kanske också gillar",
        "th": "คุณอาจจะชอบ",
        "zh-CN": "您可能还喜欢",
        "zh-TW": "您也可能喜歡"
      }
    },
    {
      "type": "checkbox",
      "id": "vendor_show",
      "label": {
        "da": "Vis produktleverandør",
        "de": "Produktverkäufer anzeigen",
        "en": "Show product vendor",
        "es": "Mostrar proveedor del producto",
        "fi": "Näytä tuotteen myyjä",
        "fr": "Afficher le distributeur du produit",
        "hi": "उत्पाद विक्रेता दिखाएं",
        "it": "Mostra fornitore prodotto",
        "ja": "商品の販売元を表示する",
        "ko": "제품 공급 업체 표시",
        "ms": "Tunjukkan vendor produk",
        "nb": "Vis produktleverandør",
        "nl": "Productleverancier weergeven",
        "pt-BR": "Exiba o fornecedor do produto",
        "pt-PT": "Mostrar o fornecedor do produto",
        "sv": "Visa produktsäljare",
        "th": "แสดงผู้ขายสินค้า",
        "zh-CN": "显示产品厂商",
        "zh-TW": "顯示產品廠商"
      },
      "default": false
    }
  ]
}
{% endschema %}

Let me know if there are any issues hope this helps!

0 Likes
Explorer
52 6 20

Hey been having issues posting a reply to this so I am going to explain the code here and post it in the next post. Basically I wasn't able to get the product recommendations working on my test site because I don't have enough products and I imagine it takes some time for the dynamic recommendations to show up. I wrote some code that should have the effect you are looking for. It works by looking at the first collection of the product and compares that to the first collections of the related products. You will want to paste the code over the contents found in sections > product-recommendations.liquid, just make sure to copy the contents of that code just in case it doesn't work how you want it too.

0 Likes
Explorer
52 6 20

Here is the code:

{%-assign first_collection = product.collections.first.handle -%}
{%- if section.settings.show_product_recommendations -%}
  {%- if recommendations.performed -%}
    {%- if recommendations.products_count > 0 -%}
      <hr>
      {%- if section.settings.heading != blank -%}
        <h2 class="section-header__title h3">{{ section.settings.heading | escape }}</h2>
      {%- endif -%}
      <div class="grid grid--uniform">
        {%- assign grid_item_width = 'medium-up--one-quarter small--one-half' -%}
        
        {%- for product in recommendations.products -%}
        	{%if product.collections.first.handle == first_collection -%}
        		{% include 'product-grid-item' %}
        	{%-endif-%}
        {%- endfor -%}
      </div>
    {%- endif -%}
  {%- else  -%}
    <div data-product-id="{{ product.id }}" data-section-id="{{ section.id }}" data-section-type="product-recommendations"></div>
  {%- endif -%}
{%- endif -%}
{% schema %}
{
  "name": {
    "da": "Produktanbefalinger",
    "de": "Produktempfehlungen",
    "en": "Product recommendations",
    "es": "Recomendaciones de productos",
    "fi": "Tuotesuositukset",
    "fr": "Recommandations de produits",
    "hi": "उत्पाद की अनुशंसाएं",
    "it": "Raccomandazioni sui prodotti",
    "ja": "商品の推奨",
    "ko": "제품 권장 사항",
    "ms": "Cadangan produk",
    "nb": "Produktanbefalinger",
    "nl": "Productaanbevelingen",
    "pt-BR": "Recomendações de produtos",
    "pt-PT": "Recomendações de produtos",
    "sv": "Produktrekommendationer",
    "th": "คำแนะนำสินค้า",
    "zh-CN": "产品推荐",
    "zh-TW": "商品推薦"
  },
  "settings": [
    {
      "type": "checkbox",
      "id": "show_product_recommendations",
      "label": {
        "da": "Vis dynamiske anbefalinger",
        "de": "Dynamische Empfehlungen anzeigen",
        "en": "Show dynamic recommendations",
        "es": "Mostrar recomendaciones dinámicas",
        "fi": "Näytä dynaamiset suositukset",
        "fr": "Afficher les recommandations dynamiques",
        "hi": "डायनेमिक सुझाव दिखाएं",
        "it": "Mostra raccomandazioni dinamiche",
        "ja": "動的推奨を表示する",
        "ko": "동적 권장 사항 표시",
        "ms": "Tunjukkan cadangan dinamik",
        "nb": "Vis dynamiske anbefalinger",
        "nl": "Dynamische aanbevelingen weergeven",
        "pt-BR": "Mostrar recomendações dinâmicas",
        "pt-PT": "Mostrar recomendações dinâmicas",
        "sv": "Visa dynamiska rekommendationer",
        "th": "แสดงคำแนะนำแบบไดนามิก",
        "zh-CN": "显示动态推荐",
        "zh-TW": "顯示動態推薦"
      },
      "info": {
        "da": "Dynamiske anbefalinger ændres og forbedres med tiden. [Få mere at vide](https://help.shopify.com/en/themes/development/recommended-products)",
        "de": "Dynamische Empfehlungen werden im Laufe der Zeit angepasst und verbessert. [Mehr Informationen](https://help.shopify.com/en/themes/development/recommended-products)",
        "en": "Dynamic recommendations change and improve with time. [Learn more](https://help.shopify.com/en/themes/development/recommended-products)",
        "es": "Las recomendaciones dinámicas cambian y mejoran con el tiempo. [Más información](https://help.shopify.com/en/themes/development/recommended-products)",
        "fi": "Dynaamiset suositukset muuttuvat ja paranevat ajan myötä. [Lisätietoja](https://help.shopify.com/en/themes/development/recommended-products)",
        "fr": "Les recommandations dynamiques changent et s'améliorent avec le temps. [En savoir plus](https://help.shopify.com/en/themes/development/recommended-products)",
        "hi": "समय के साथ डायनेमिक सुझाव परिवर्तन और सुधार. [अधिक जानें](https://help.shopify.com/en/themes/development/recommended-products)",
        "it": "Le raccomandazioni dinamiche cambiano e migliorano nel tempo. [Maggiori informazioni](https://help.shopify.com/en/themes/development/recommended-products)",
        "ja": "動的推奨は時間とともに変化し改善します。[詳細情報](https://help.shopify.com/en/themes/development/recommended-products)",
        "ko": "동적 권장 사항은 시간이 지나면서 변하고 개선됩니다. [자세히 알아보기](https://help.shopify.com/en/themes/development/recommended-products)",
        "ms": "Cadangan dinamik bertukar dan bertambah baik dari masa. [Ketahui lebih lanjut](https://help.shopify.com/en/themes/development/recommended-products)",
        "nb": "Dynamiske anbefalinger endrer seg og forbedres med tiden. [Finn ut mer](https://help.shopify.com/en/themes/development/recommended-products)",
        "nl": "Dynamische aanbevelingen veranderen en verbeteren mettertijd. [Meer informatie](https://help.shopify.com/en/themes/development/recommended-products)",
        "pt-BR": "As recomendações dinâmicas mudam e melhoram com o tempo. [Saiba mais](https://help.shopify.com/en/themes/development/recommended-products)",
        "pt-PT": "As recomendações dinâmicas mudam e melhoram com o tempo. [Saiba mais](https://help.shopify.com/en/themes/development/recommended-products)",
        "sv": "Dynamiska rekommendationer ändras och förbättras med tiden. [Läs mer](https://help.shopify.com/en/themes/development/recommended-products)",
        "th": "คำแนะนำแบบไดนามิกนั้นเปลี่ยนแปลงและถูกปรับปรุงให้ดีขึ้นตลอดเวลา [เรียนรู้เพิ่มเติม](https://help.shopify.com/en/themes/development/recommended-products)",
        "zh-CN": "动态推荐会随着时间而变化和改进。[了解详情](https://help.shopify.com/en/themes/development/recommended-products)",
        "zh-TW": "動態推薦會隨著時間改變與改進。[深入瞭解](https://help.shopify.com/en/themes/development/recommended-products)"
      },
      "default": true
    },
    {
      "type": "text",
      "id": "heading",
      "label": {
        "da": "Overskrift",
        "de": "Überschrift",
        "en": "Heading",
        "es": "Título",
        "fi": "Otsake",
        "fr": "En-tête",
        "hi": "शीर्षक",
        "it": "Heading",
        "ja": "見出し",
        "ko": "제목",
        "ms": "Tajuk",
        "nb": "Overskrift",
        "nl": "Kop",
        "pt-BR": "Título",
        "pt-PT": "Título",
        "sv": "Rubrik",
        "th": "ส่วนหัว",
        "zh-CN": "标题",
        "zh-TW": "標題"
      },
      "default": {
        "da": "Du vil muligvis også synes om",
        "de": "Das könnte Ihnen auch gefallen",
        "en": "You may also like",
        "es": "También te puede interesar",
        "fi": "Saatat pitää myös näistä",
        "fr": "Vous pourriez aimer également",
        "hi": "आप शायद इसे भी पसंद करें",
        "it": "Potrebbero interessarti anche",
        "ja": "あなたへのおすすめ",
        "ko": "회원님도 좋아할 것입니다.",
        "ms": "Anda juga mungkin sukakan",
        "nb": "Kanskje du også liker",
        "nl": "Wellicht vindt je  dit ook leuk",
        "pt-BR": "Talvez você também goste de",
        "pt-PT": "Também poderá gostar de",
        "sv": "Du kanske också gillar",
        "th": "คุณอาจจะชอบ",
        "zh-CN": "您可能还喜欢",
        "zh-TW": "您也可能喜歡"
      }
    },
    {
      "type": "checkbox",
      "id": "vendor_show",
      "label": {
        "da": "Vis produktleverandør",
        "de": "Produktverkäufer anzeigen",
        "en": "Show product vendor",
        "es": "Mostrar proveedor del producto",
        "fi": "Näytä tuotteen myyjä",
        "fr": "Afficher le distributeur du produit",
        "hi": "उत्पाद विक्रेता दिखाएं",
        "it": "Mostra fornitore prodotto",
        "ja": "商品の販売元を表示する",
        "ko": "제품 공급 업체 표시",
        "ms": "Tunjukkan vendor produk",
        "nb": "Vis produktleverandør",
        "nl": "Productleverancier weergeven",
        "pt-BR": "Exiba o fornecedor do produto",
        "pt-PT": "Mostrar o fornecedor do produto",
        "sv": "Visa produktsäljare",
        "th": "แสดงผู้ขายสินค้า",
        "zh-CN": "显示产品厂商",
        "zh-TW": "顯示產品廠商"
      },
      "default": false
    }
  ]
}
{% endschema %}
0 Likes
Explorer
52 6 20

It looks like I am not able to paste the entire contents of that code so you will want to paste this section of code over the first section of your code:

{%-assign first_collection = product.collections.first.handle -%}
<script type='text/javascript'>console.log({{first_collection|json}});</script>
{%- if section.settings.show_product_recommendations -%}
  {%- if recommendations.performed -%}

    {%- if recommendations.products_count > 0 -%}
      <hr>
      {%- if section.settings.heading != blank -%}
        <h2 class="section-header__title h3">{{ section.settings.heading | escape }}</h2>
      {%- endif -%}
      <div class="grid grid--uniform">
        {%- assign grid_item_width = 'medium-up--one-quarter small--one-half' -%}
        
        {%- for product in recommendations.products -%}
        	{%if product.collections.first.handle == first_collection -%}
        		{% include 'product-grid-item' %}
        	{%-endif-%}
        {%- endfor -%}
      </div>
    {%- endif -%}
  {%- else  -%}
    <div data-product-id="{{ product.id }}" data-section-id="{{ section.id }}" data-section-type="product-recommendations"></div>
  {%- endif -%}
{%- endif -%}

You should paste the above over this section of code:

{%- if section.settings.show_product_recommendations -%}
  {%- if recommendations.performed -%}
    {%- if recommendations.products_count > 0 -%}
      <hr>
      {%- if section.settings.heading != blank -%}
        <h2 class="section-header__title h3">{{ section.settings.heading | escape }}</h2>
      {%- endif -%}
      <div class="grid grid--uniform">
        {%- assign grid_item_width = 'medium-up--one-quarter small--one-half' -%}
        
        {%- for product in recommendations.products -%}
        	{% include 'product-grid-item' %}
        {%- endfor -%}
      </div>
    {%- endif -%}
  {%- else  -%}
    <div data-product-id="{{ product.id }}" data-section-id="{{ section.id }}" data-section-type="product-recommendations"></div>
  {%- endif -%}
{%- endif -%}

Let me know if you have any issues! If you want to full code DM me and I can email it to you.

0 Likes
New Member
8 0 0

Thanks Michael, I'll give it a try.

0 Likes
New Member
8 0 0

Hey Michael, It's not displaying any products. It only displays the header.Prod recommendations error.pngerror

0 Likes
New Member
8 0 0

Hey Michael, It's not displaying any products. It only displays the header.Prod recommendations error.pngerrorerror

0 Likes
Explorer
52 6 20

Ok sorry that didn't work just took a look at my test site and it looks like some product recommendations are now taking effect, guess it just takes time for it to work. I can confirm on my site it is also not displaying the products I will look into it right now and should have an answer to you today. It will be way easier to figure out now that I can test it on my own site.

0 Likes
Astronaut
884 106 198

There's a new feature available for recommended products you can use:

 

https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#recommended-products

 

It's not guaranteed to ALWAYS be the same thing that the customer is looking at, but "Products are recommended based on an algorithm that predicts the most relevant products based on the product a customer is interacting with."

 

We have it implemented on our site and all results seem relevant. 

If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes