Exclude some collections from related-products

Hello,

I am using the minimal theme (an old version) and am looking to customise the related-products feature to ignore some of my collections. I want to ‘ignore’ the collections that are at the parent level of my category tree, only ‘including’ child collections e.g. ignore ‘for special occasions’ parent collection, include ‘christmas’ child collection.

Can anyone help with the code that I need to add to achieve this?

Thanks!

{% assign number_of_products = 4 %}
{% assign number_of_products_to_fetch = number_of_products | plus: 1 %}

{% if collection == null or collection.handle == ‘frontpage’ or collection.handle == ‘all’ %}
{% assign found_a_collection = false %}
{% for c in product.collections %}
{% if found_a_collection == false and c.handle != ‘frontpage’ and c.handle != ‘all’ and c.all_products_count > 1 %}
{% assign found_a_collection = true %}
{% assign collection = c %}
{% endif %}
{% endfor %}
{% endif %}

{% if collection and collection.products_count > 1 %}


{{ 'products.general.related_products_title' | t }}

{% assign current_product = product %} {% assign current_product_found = false %} {% for product in collection.products limit: number_of_products_to_fetch %} {%- if product.metafields.inventory.ShappifyHidden == 'true' -%}{%- continue -%}{%- endif -%} {%- include 'bold-product' with product, hide_action: 'skip' -%} {% if product.handle == current_product.handle %} {% assign current_product_found = true %} {% else %} {% unless current_product_found == false and forloop.last %} {% assign grid_item_width = 'post-large--one-quarter medium--one-quarter small--one-half' %} {%- assign product_width = 300 -%} {% assign featured = product %}
{% include 'product-grid-item' %}
{% endunless %} {% endif %} {% endfor %}
{% endif %}
1 Like

@avmhb

sorry for this issue also sorry again Shopify related product doesn’t allow to customization its dynamic code

https://www.shopify.in/partners/blog/related-products

but if you have static collection all product it can be done some custom code

1 Like

Hi Ketan,

Oh brilliant, so using the article you sent I have been able to add the ‘dynamic recommendations’ module and I will now turn off the old ‘related products’ module.

One final question! At the moment the recommendations are appearing 1 product / line, even on desktop. How do I tweak the code so that there are 4 products / line on desktop (as for the old related-products module)

Thank you so much for your help!

Alice

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

{%- if recommendations.products_count > 0 -%}

{{ section.settings.heading }}

{%- endif -%}
{%- endif -%}

{% schema %}
{
“name”: “Product recommendations”,
“settings”: [
{
“type”: “checkbox”,
“id”: “show_product_recommendations”,
“label”: “Turn on product recommendations”,
“default”: false
},
{
“type”: “text”,
“id”: “heading”,
“label”: “Heading”,
“default”: “You might also like”
}
]
}
{% endschema %}

{% javascript %}
var loadProductRecommendationsIntoSection = function() {
// Look for an element with class ‘product-recommendations’
var productRecommendationsSection = document.querySelector(“.product-recommendations”);
if (productRecommendationsSection === null) { return; }
// Read product id from data attribute
var productId = productRecommendationsSection.dataset.productId;
// Read limit from data attribute
var limit = productRecommendationsSection.dataset.limit;
// Build request URL
var requestUrl = “/recommendations/products?section_id=product-recommendations&limit=”+limit+“&product_id=”+productId;
// Create request and submit it using Ajax
var request = new XMLHttpRequest();
request.open(“GET”, requestUrl);
request.onload = function() {
if (request.status >= 200 && request.status < 300) {
var container = document.createElement(“div”);
container.innerHTML = request.response;
productRecommendationsSection.parentElement.innerHTML = container.querySelector(“.product-recommendations”).innerHTML;
}
};
request.send();
};
// If your section has theme settings, the theme editor
// reloads the section as you edit those settings. When that happens, the
// recommendations need to be fetched again.
// See https://help.shopify.com/en/themes/development/sections/integration-with-theme-editor
document.addEventListener(“shopify:section:load”, function(event) {
if (event.detail.sectionId === “product-recommendations”) {
loadProductRecommendationsIntoSection();
}
});
// Fetching the recommendations on page load
loadProductRecommendationsIntoSection();
{% endjavascript %}

1 Like

@avmhb

can you please share store url so i will check and update