Exclude certain Products From Product Recommendations on Motion Theme

I need help with excluding certain products from my Product Recommendation Section.

I found the code, but not sure how to code it. I have tag all the items with vjt. Here is the code below. Any help will be appreciate it.

Thank you in advance.

{%- liquid
assign recommend_products = true

if recommendations.products and recommendations.products_count > 0
assign related_collection = recommendations
endif

for tag in product.tags
if tag contains ‘related’
assign include_collection_handle = tag | split: '
’ | last
assign include_collection = collections[include_collection_handle]
if include_collection != empty and include_collection.products_count > 0
assign related_collection = include_collection
assign recommend_products = false
break
endif
endif
endfor

assign number_of_products = section.settings.related_count
-%}

{%- if section.settings.show_product_recommendations -%}

{{ section.settings.product_recommendations_heading }}

{%- if recommend_products -%}
{% comment %} This content is visually hidden and replaced when recommended products show up {% endcomment %}
{%- render 'product-grid-item', product: product, per_row: number_of_products -%}
{%- endif -%} {%- if related_collection.products_count > 1 -%} {%- liquid case number_of_products when 1 assign grid_item_width = '' when 2 assign grid_item_width = 'medium-up--one-half' when 3 assign grid_item_width = 'small--one-half medium-up--one-third' when 4 assign grid_item_width = 'small--one-half medium-up--one-quarter' when 5 assign grid_item_width = 'small--one-half medium-up--one-fifth' when 6 assign grid_item_width = 'small--one-half medium-up--one-sixth' endcase -%}
{%- for product in related_collection.products limit: number_of_products -%} {%- render 'product-grid-item', product: product, grid_item_width: grid_item_width, per_row: number_of_products -%} {%- endfor -%}
{%- endif -%}
{%- endif -%}

{% schema %}
{
“name”: “t:sections.product-recommendations.name”,
“settings”: [
{
“type”: “checkbox”,
“id”: “show_product_recommendations”,
“label”: “t:sections.product-recommendations.settings.show_product_recommendations.label”,
“info”: “t:sections.product-recommendations.settings.show_product_recommendations.info”,
“default”: true
},
{
“type”: “text”,
“id”: “product_recommendations_heading”,
“label”: “t:sections.product-recommendations.settings.product_recommendations_heading.label”,
“default”: “You may also like”
},
{
“type”: “range”,
“id”: “related_count”,
“label”: “t:sections.product-recommendations.settings.related_count.label”,
“default”: 5,
“min”: 2,
“max”: 6,
“step”: 1
}
]
}
{% endschema %}

Hi @kim62 ,

Thank you for providing the code. My understanding is your products that you need to be hidden contains a tag “vjt”? if so, you can use the {% unless %} condition.

{% unless product.tags contains "vjt" %}
{%- render 'product-grid-item', product: product, grid_item_width: grid_item_width, per_row: number_of_products -%}
{% endunless %}

I made some edits. Please replace your code in your file with the code below.

{%- liquid
assign recommend_products = true

if recommendations.products and recommendations.products_count > 0
assign related_collection = recommendations
endif

for tag in product.tags
if tag contains '_related'
assign include_collection_handle = tag | split: '_' | last
assign include_collection = collections[include_collection_handle]
if include_collection != empty and include_collection.products_count > 0
assign related_collection = include_collection
assign recommend_products = false
break
endif
endif
endfor

assign number_of_products = section.settings.related_count
-%}

{%- if section.settings.show_product_recommendations -%}

{%- if recommend_products -%}

{% comment %}
This content is visually hidden and replaced when recommended products show up
{% endcomment %}

{%- render 'product-grid-item', product: product, per_row: number_of_products -%}

{%- endif -%}
{%- if related_collection.products_count > 1 -%}
{%- liquid
case number_of_products
when 1
assign grid_item_width = ''
when 2
assign grid_item_width = 'medium-up--one-half'
when 3
assign grid_item_width = 'small--one-half medium-up--one-third'
when 4
assign grid_item_width = 'small--one-half medium-up--one-quarter'
when 5
assign grid_item_width = 'small--one-half medium-up--one-fifth'
when 6
assign grid_item_width = 'small--one-half medium-up--one-sixth'
endcase
-%}

{%- for product in related_collection.products limit: number_of_products -%}
{% unless product.tags contains "vjt" %}
{%- render 'product-grid-item', product: product, grid_item_width: grid_item_width, per_row: number_of_products -%}
{% endunless %}
{%- endfor -%}

{%- endif -%}

{%- endif -%}

{% schema %}
{
"name": "t:sections.product-recommendations.name",
"settings": [
{
"type": "checkbox",
"id": "show_product_recommendations",
"label": "t:sections.product-recommendations.settings.show_product_recommendations.label",
"info": "t:sections.product-recommendations.settings.show_product_recommendations.info",
"default": true
},
{
"type": "text",
"id": "product_recommendations_heading",
"label": "t:sections.product-recommendations.settings.product_recommendations_heading.label",
"default": "You may also like"
},
{
"type": "range",
"id": "related_count",
"label": "t:sections.product-recommendations.settings.related_count.label",
"default": 5,
"min": 2,
"max": 6,
"step": 1
}
]
}
{% endschema %}

Thank you so much for your help and it works beautifully, the only thing I notice is it now leaves an empty spot. Grid is 5, but only shows 4, instead of populated to the next related product, but again thank you so much.

K

@kim62

You have to adjust your CSS