Hi everyone! I’m just getting started with Shopify/Liquid and I’ve come across an issue that I have been trying to solve for days.
I’m making a product recommendation section that recommends products based off the current product’s tags. So for instance, if a customer is viewing mugs-xyz then the code gets the mugs-xyz’s tags and displays products that have the same tags. That works fine but since looping through all the products was making the page very slow, I only loop through the products that belong in mugs-xyz*'*s collection. Which is causing some products to be displayed twice since some products belong in more than one collection.
I’m saving the product title of all the products that are being displayed and using the unless statement to check whether the product I’m currently looping through has already been displayed or not (by checking if its title exists in the product_title variable). But the contains (by the *** sign) is not working. It is printing out the products even though the titles of those products are already present in product_title.
{%- assign current_product = product -%}
{%- assign product_title = current_product.title -%}
{%- for collection in current_product.collections -%}
{%- for product in collection.products -%}
{%- if product.tags contains current_product_tag -%}
{%- unless product.handle == current_product.handle -%}
{%- unless product.title contains product_title -%} ***
{%- assign product_title = product_title | append: ', ' | append: product.title -%}
{%- render 'product-item', product: product, grid_classes: '1/4--lap 1/5--desk 1/6--wide', show_add_to_cart: section.settings.show_quick_buy -%}
{%- endunless -%}
{%- endunless -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
Any help would be greatly appreciated.