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?
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 -%}
{% 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 %}