The recommendations object returns products only if it's used in a theme section that's rendered through an HTTP request to <base_url>?section_id=<section_id>&product_id=<product_id>. section_id is the ID of the section where the recommendations object is being used, and product_id is the ID of the product you want to show recommended products for. To determine the base_url, use the routes.product_recommendations_url attribute. Using the routes object rather than a hardcoded URL ensures that the product recommendations load in the correct locale.
This tutorial describes how to create a section to display recommendations and render it inside the product template of a theme.
Step 1: Create a product-recommendations.liquid section
When the section is rendered with the page, recommendations.products_count is 0 and so the generated HTML is an empty div element:
If the user is using an alternate locale, the locale is included in the div's data-base-url:
Recommendations are also loaded using the alternate locale. For example, /fr/recommendations/products?section_id=product-recommendations&limit=4&product_id=123.
Step 2: Include the section in your product.liquid template
To display product recommendations at the bottom of the product page, include the section at the bottom of your templates/product.liquid file: