'Issues with a custom related products snippet in Shopify

I'm attempting to get a customised related products section on my Shopify product pages. It's a functionality I've not been able to get from any of the Apps I've tried in the app store.

My store has nearly 2,000 products, and with the type of products stocked the logic for matching related products comes from the product title.

For example we have six products with these titles:

  • All That Jazz Greetings Card

  • All That Jazz Coaster

  • All That Jazz Tablemat

  • Grasmere Coaster

  • Grasmere Dawn Postcard

  • Boats on Grasmere

The text to match on in these examples is 'All That Jazz' and 'Grasmere'.

Apps have been matching products on tags, collections and AI which has resulted in illogical relations and virtually no clicks. For example the 'Grasmere Coaster' will have other coasters in the related products block, such as the 'All that Jazz' coaster. Customers will want to see other 'Grasmere' or 'All That Jazz' products on those pages.

Because we use all the other fields in the products for other purposes (particularly the tags field), I felt the approach to this was to set up a Metafield containing the text to match each product to.

So I've been able to get a snippet coded which works partially, but I'm at the point where I don't think it's going to work properly.

Here's my snippet code:

{%- assign collection = collections.all -%}
  {%- assign relationship = product.metafields.product.related -%}
  {%- assign url = product.url -%}
  
  <div class="grid">
    {% paginate collection.products by 1000 %}
      {% for product in collection.products %}
        {% if product.metafields.product.related == relationship %}
          {% unless product.url == url %}
            <div class="grid-item">
              {% include 'product-card' product: product %}
            </div>
          {% endunless %}
        {% endif %}
      {% endfor %}
    {% endpaginate %}
  </div>

Just to explain what's happening, relationship is the text from the Metafield. And I'm using an if statement to match the results to the main product being displayed. I've assigned the url in order to filter out the main product in the related products block. Paginate is there to circumvent the Shopify limit of 50 products to loop through.

First issue is that I'm unable to limit the results properly. If I use limit anywhere it only loops through that number of products. If I don't limit the number of results though, there's a danger that there would be too many related products loading on a single page.

Second issue is that the paginate limit is apparently 1,000 products. I really need to be able to access all products to get accurate results. I could make collection dynamic to match the main product's range which is generated through the Vendor field (there are collections to match each Vendor), but not all the results we want would then be displayed. Grasmere products, for example, straddle 2 or 3 different vendors/collections.

Perhaps I've gone about this wrong. Maybe I missed an App that would perform this functionality. Or possibly I need a custom App? Any guidance or feedback would be most appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source