The recommendations object returns the product recommendations stored in the shop for a given product.
The recommendations object only returns products when it is used in a theme section rendered through an HTTP request to <base_url>?section_id=<section_id>&product_id=<product_id>. section_id is the ID of the section that uses the recommendations object, and product_id is the ID of the product you want recommendations for. To get the base_url, use the routes.product_recommendations_url attribute. Using the routes object instead of a hard-coded URL makes sure the recommendations load in the right locale.
The recommendations object has the following attributes:
recommendations.performed
Returns true if the recommendations object is referenced inside a theme section that is rendered through the recommendations endpoint with valid parameters:
Parameters for recommendations.performed
| Parameters | Description | Required |
|---|---|---|
| product_id | ID of the product you want to show recommended products for | yes |
| section_id | ID of the section where the recommendations object is used |
yes |
recommendations.products_count
Returns the number of product recommendations, or 0 if recommendations.performed is false.
recommendations.products
Returns the product recommendations. These objects are products. No products are returned if recommendations.performed is false.
For example, this Liquid snippet sets up the display of product recommendations:
{% if recommendations.performed %}
{% if recommendations.products_count > 0 %}
{% for product in recommendations.products %}
{{ product.title | link_to: product.url }}
{% endfor %}
{% endif %}
{% else %}
<div class="placeholder">
Placeholder animation
</div>
{% endif %}
It creates this output when the enclosing section is rendered synchronously:
Placeholder animation
When the enclosing section is rendered from the endpoint /recommendations/products?section_id=<section_id>&product_id=<product_id>, the output is:
Product title
Another product title
More properties
VersaCommerce also provides these properties on the recommendations object:
| Properties | Returns |
|---|---|
intent, performed? |
Selected recommendation intent and completion predicate |