skip to Main Content

On my product page in my Shopify webshop I have a small liquid script that looks for other products in the same color and recommends these – output on the page.

So if a product carries the tag mor – it will look through the ‘all’ collection in Shopify for all other products with the tag mor and show them as alternative, matching colour-options.

This works well for all my existing products.

However, today I created to new products with _mor- as tag, and these products are not included in the alternative products when viewing other similar products.

I checked the ‘all’ collection – and the new products ARE part of this collection, but for somehow they are not shown on the product page.

Debug shows that the script does not even evaluate the products – it’s like the products are not part of the fetch of the all collection the script does.

I can’t figure it out. I hoped it was some sort of delay but now it’s lasted for hours – and the two new products are simply not included.

The new products DO show other alternative products, but they are not included when other products try to fetch them.

Below is the code of the script – and here is a couple of products for explaining.

Old product:
https://puretime.dk/en/products/oddjob-ipadholder-mobilholder-i-trae-morkt-egetrae

  • shows all other brown products but not the two new, brown products

Two new brown products:
https://puretime.dk/en/collections/kaffefilterholdere/products/gislev-kaffefilterholder-i-trae-flere-varianter-morkt-egetrae

https://puretime.dk/en/collections/toiletrulleholdere/products/hvidkilde-toiletrulleholder-i-trae-morkt-egetrae-toiletpapirholder

They do show other brown products – but none of them are included in other brown products alternative listings.

Both new products carry both mor and crosslinking tags.

Debug in console is available.


Script:

{% if product.tags contains 'crosslinking' %}
  {%- assign tag_alt = "BLANK" -%}
  
  {%- for tag in product.tags -%}
    {%- assign tag_prefix = tag | slice: 0, 5 -%}
    
    {%- if tag_prefix == "_lys_" or tag_prefix == "_mor_" -%}
      {%- assign tag_alt = tag | strip | downcase -%}
      {%- break -%}
    {%- endif -%}
  {%- endfor -%}
  
  {%- unless tag_alt == "BLANK" -%}
    {%- assign all_collection_handle = 'all' -%}
    {%- if collections[all_collection_handle].products.size > 0 -%}
      {%- assign alt_products_total = 0 -%}
      {%- assign alt_products = collections[all_collection_handle].products -%}
      
      <!-- Add header before the alt products -->
      <p class="crosslinking-p">
        <span style="font-weight: 600 !important; font-size: .8rem;">
          {% if shop.locale == 'da' %}Produkter i samme farve:{% else %}Also available in:{% endif %}
        </span>
      </p>
      
      {%- assign sorted_alt_products = alt_products | sort: 'price' -%}  <!-- Sort products by price -->
      
      {%- for alt_product in sorted_alt_products -%}
        
        {%- if alt_product.id != product.id -%}  <!-- Exclude current product -->
          
          {% capture debug_msg %}Product ID: {{ alt_product.id }}, Title: {{ alt_product.title }}{% endcapture %}
          <script>console.debug("Checking alternative product - {{ debug_msg }}");</script>
          
          {%- assign matched = false -%}
          {%- for tag in alt_product.tags -%}
            {%- assign normalized_tag = tag | strip | downcase -%}
            
            {% if normalized_tag == tag_alt %}
              <script>console.debug("  - Tag check: Tag {{ normalized_tag }} matches tag_alt {{ tag_alt }}");</script>
              {%- assign matched = true -%}
              {%- assign alt_products_total = alt_products_total | plus: 1 -%}
              <a href="{{ alt_product.url }}" title="{{ alt_product.title }}">
                <img src="{{ alt_product.featured_image | img_url: '50x50', crop: 'center', format: 'pjpg' }}" alt="{{ alt_product.title }}">
              </a>
              <script>console.info("  -> Product matched: {{ alt_product.title }}");</script>
              {%- break -%}
            {% else %}
              <script>console.debug("  - Tag check: Tag {{ normalized_tag }} does not match tag_alt {{ tag_alt }}");</script>
            {% endif %}
          {%- endfor -%}
          
          {%- unless matched -%}
            <script>console.warn("  -> Product not matched: {{ alt_product.title }}");</script>
          {%- endunless -%}
        
        {%- endif -%}
        
      {%- endfor -%}

      {%- if alt_products_total == 0 -%}
        <p>No matching products found in the collection</p>
        <script>console.info("No matching products found in the collection");</script>
      {%- endif -%}
    {%- else -%}
      <p>No products in 'all' collection</p>
      <script>console.error("No products available in the 'all' collection");</script>
    {%- endif -%}
  {%- else -%}
    <p>No matching tag found</p>
    <script>console.error("No matching tag found in product tags");</script>
  {%- endunless -%}
{% endif %}

2

Answers


  1. Chosen as BEST ANSWER

    For some reason it did not fetch all 66 products from the all collection - but only apparently the first 48.

    Solution was in this case to create a collection with only the needed products (which is less than 48).

    However, if anyone would know how to change the script to make it more solid and work with 48+ products, suggestions are welcome.


  2. Filter Products Using Tags or Increase Pagination Size

    I understand that you are making a product association using a condition to extract those containing specific tags and then using it as a recommendation engine (product relationships)

    What I would do:

    1. If I already have the tagged products, I simply use the section rendering API to achieve this purpose. Example: /collectiones/all/mor?section_id={your_section_liquid_file_without_liquid_extention}

    Section rendering API: https://shopify.dev/docs/api/ajax/section-rendering

    1. You can use pagination to access more products than you are currently accessing, the object is paginate and increase the page_size to be greater than 50 which is the default pagination
    {% paginate array by page_size %}
      {% for item in array %}
        // custom logic to extract tags
      {% endfor %}
    {% endpaginate %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search