Show product recommendations on the product page
Caution
Vintage themes aren't available in the Theme Store. Vintage themes don't have the features included in Shopify's Online Store 2.0 themes , and Shopify free vintage themes don't receive updates except security fixes.
Note
This customization is for vintage Shopify themes, and doesn't apply to Online Store 2.0 themes.
This tutorial describes how to add product recommendations to your product page in the Debut theme. To learn more about how product recommendations work, see Showing product recommendations on product pages .
Step 1: Create a product-recommendations.liquid
section
Desktop
From your Shopify admin, go to Online Store > Themes .
Find the theme you want to edit, click the … button to open the actions menu, and then click Edit code .
In the Sections directory, click Add a new section .
Name the new section product-recommendations
and click Create section .
Replace all of the content with the code below:
{% assign limit = 4 %}
<div class="page-width product-recommendations" data-base-url="{{ routes . product_recommendations_url }} " data-product-id="{{ product . id }} " data-limit="{{ limit }} " data-section-id="{{ section . id }} " data-section-type="product-recommendations" data-intent="related">
{% if recommendations . performed %}
{% if recommendations . products_count > 0 %}
<div class="section-header text-center">
{% if recommendations . intent == 'related' %}
<h2> You may also like</h2>
{% elsif recommendations . intent == 'complementary' %}
<h2>Pair it with</h2>
{% endif %}
</div>
<ul class="grid grid--uniform grid--view-items">
{% for product in recommendations . products %}
<li class="grid__item small--one-half medium-up--one-quarter">
{% include 'product-card-grid' , max_height : 250 %}
</li>
{% endfor %}
</ul>
{% endif %}
{% else %}
<div class="product-recommendations__loading-dots">
<div class="product-recommendations__loading-dot"></div>
<div class="product-recommendations__loading-dot"></div>
<div class="product-recommendations__loading-dot"></div>
</div>
{% endif %}
</div>
Click Save .
When the section is rendered with the product page, recommendations.performed
will be false
and so the generated HTML will show a loading animation:
<div class= "page-width product-recommendations" data-base-url= "/recommendations/products" data-product-id= "123" data-limit= "4" data-section-id= "product-recommendations" data-section-type= "product-recommendations" data-intent= "related" >
<div class= "product-recommendations__loading-dots" >
<div class= "product-recommendations__loading-dot" ></div>
<div class= "product-recommendations__loading-dot" ></div>
<div class= "product-recommendations__loading-dot" ></div>
</div>
</div>
If you don't want to show a loading animation, use this code instead:
{% assign limit = 4 %}
<div class="page-width product-recommendations" data-base-url="{{ routes . product_recommendations_url }} " data-product-id="{{ product . id }} " data-limit="{{ limit }} " data-section-id="{{ section . id }} " data-section-type="product-recommendations" data-intent="related">
{% if recommendations . products_count > 0 %}
<div class="section-header text-center">
{% if recommendations . intent == 'related' %}
<h2> You may also like</h2>
{% elsif recommendations . intent == 'complementary' %}
<h2>Pair it with</h2>
{% endif %}
</div>
<ul class="grid grid--uniform grid--view-items">
{% for product in recommendations . products %}
<li class="grid__item small--one-half medium-up--one-quarter">
{% include 'product-card-grid' , max_height : 250 %}
</li>
{% endfor %}
</ul>
{% endif %}
</div>
When the above section is rendered with your product page, the generated HTML will be a div
element with no content:
<div class= "page-width product-recommendations" data-base-url= "/recommendations/products" data-product-id= "123" data-limit= "4" data-section-id= "product-recommendations" data-section-type= "product-recommendations" data-intent= "related" >
If the user is using an alternate locale, then the locale is included in the div's data-base-url
. For example, /fr/recommendations/products
.
iPhone
From the Shopify app , tap the … button.
In the Sales channels section, tap Online Store .
Tap Manage themes .
Find the theme you want to edit, click the … button to open the actions menu, and then click Edit code .
In the Sections directory, click Add a new section .
Name the new section product-recommendations
and click Create section .
Replace all of the content with the code below:
{% assign limit = 4 %}
<div class="page-width product-recommendations" data-base-url="{{ routes . product_recommendations_url }} " data-product-id="{{ product . id }} " data-limit="{{ limit }} " data-section-id="{{ section . id }} " data-section-type="product-recommendations" data-intent="related">
{% if recommendations . performed %}
{% if recommendations . products_count > 0 %}
<div class="section-header text-center">
{% if recommendations . intent == 'related' %}
<h2> You may also like</h2>
{% elsif recommendations . intent == 'complementary' %}
<h2>Pair it with</h2>
{% endif %}
</div>
<ul class="grid grid--uniform grid--view-items">
{% for product in recommendations . products %}
<li class="grid__item small--one-half medium-up--one-quarter">
{% include 'product-card-grid' , max_height : 250 %}
</li>
{% endfor %}
</ul>
{% endif %}
{% else %}
<div class="product-recommendations__loading-dots">
<div class="product-recommendations__loading-dot"></div>
<div class="product-recommendations__loading-dot"></div>
<div class="product-recommendations__loading-dot"></div>
</div>
{% endif %}
</div>
Click Save .
When the section is rendered with the product page, recommendations.performed
will be false
and so the generated HTML will show a loading animation:
<div class= "page-width product-recommendations" data-base-url= "/recommendations/products" data-product-id= "123" data-limit= "4" data-section-id= "product-recommendations" data-section-type= "product-recommendations" data-intent= "related" >
<div class= "product-recommendations__loading-dots" >
<div class= "product-recommendations__loading-dot" ></div>
<div class= "product-recommendations__loading-dot" ></div>
<div class= "product-recommendations__loading-dot" ></div>
</div>
</div>
If you don't want to show a loading animation, use this code instead:
{% assign limit = 4 %}
<div class="page-width product-recommendations" data-base-url="{{ routes . product_recommendations_url }} " data-product-id="{{ product . id }} " data-limit="{{ limit }} " data-section-id="{{ section . id }} " data-section-type="product-recommendations" data-intent="related">
{% if recommendations . products_count > 0 %}
<div class="section-header text-center">
{% if recommendations . intent == 'related' %}
<h2> You may also like</h2>
{% elsif recommendations . intent == 'complementary' %}
<h2>Pair it with</h2>
{% endif %}
</div>
<ul class="grid grid--uniform grid--view-items">
{% for product in recommendations . products %}
<li class="grid__item small--one-half medium-up--one-quarter">
{% include 'product-card-grid' , max_height : 250 %}
</li>
{% endfor %}
</ul>
{% endif %}
</div>
When the above section is rendered with your product page, the generated HTML will be a div
element with no content:
<div class= "page-width product-recommendations" data-base-url= "/recommendations/products" data-product-id= "123" data-limit= "4" data-section-id= "product-recommendations" data-section-type= "product-recommendations" data-intent= "related" >
If the user is using an alternate locale, then the locale is included in the div's data-base-url
. For example, /fr/recommendations/products
.
Android
From the Shopify app , tap the … button.
In the Sales channels section, tap Online Store .
Tap Manage themes .
Find the theme you want to edit, click the … button to open the actions menu, and then click Edit code .
In the Sections directory, click Add a new section .
Name the new section product-recommendations
and click Create section .
Replace all of the content with the code below:
{% assign limit = 4 %}
<div class="page-width product-recommendations" data-base-url="{{ routes . product_recommendations_url }} " data-product-id="{{ product . id }} " data-limit="{{ limit }} " data-section-id="{{ section . id }} " data-section-type="product-recommendations" data-intent="related">
{% if recommendations . performed %}
{% if recommendations . products_count > 0 %}
<div class="section-header text-center">
{% if recommendations . intent == 'related' %}
<h2> You may also like</h2>
{% elsif recommendations . intent == 'complementary' %}
<h2>Pair it with</h2>
{% endif %}
</div>
<ul class="grid grid--uniform grid--view-items">
{% for product in recommendations . products %}
<li class="grid__item small--one-half medium-up--one-quarter">
{% include 'product-card-grid' , max_height : 250 %}
</li>
{% endfor %}
</ul>
{% endif %}
{% else %}
<div class="product-recommendations__loading-dots">
<div class="product-recommendations__loading-dot"></div>
<div class="product-recommendations__loading-dot"></div>
<div class="product-recommendations__loading-dot"></div>
</div>
{% endif %}
</div>
Click Save .
When the section is rendered with the product page, recommendations.performed
will be false
and so the generated HTML will show a loading animation:
<div class= "page-width product-recommendations" data-base-url= "/recommendations/products" data-product-id= "123" data-limit= "4" data-section-id= "product-recommendations" data-section-type= "product-recommendations" data-intent= "related" >
<div class= "product-recommendations__loading-dots" >
<div class= "product-recommendations__loading-dot" ></div>
<div class= "product-recommendations__loading-dot" ></div>
<div class= "product-recommendations__loading-dot" ></div>
</div>
</div>
If you don't want to show a loading animation, use this code instead:
{% assign limit = 4 %}
<div class="page-width product-recommendations" data-base-url="{{ routes . product_recommendations_url }} " data-product-id="{{ product . id }} " data-limit="{{ limit }} " data-section-id="{{ section . id }} " data-section-type="product-recommendations" data-intent="related">
{% if recommendations . products_count > 0 %}
<div class="section-header text-center">
{% if recommendations . intent == 'related' %}
<h2> You may also like</h2>
{% elsif recommendations . intent == 'complementary' %}
<h2>Pair it with</h2>
{% endif %}
</div>
<ul class="grid grid--uniform grid--view-items">
{% for product in recommendations . products %}
<li class="grid__item small--one-half medium-up--one-quarter">
{% include 'product-card-grid' , max_height : 250 %}
</li>
{% endfor %}
</ul>
{% endif %}
</div>
When the above section is rendered with your product page, the generated HTML will be a div
element with no content:
<div class= "page-width product-recommendations" data-base-url= "/recommendations/products" data-product-id= "123" data-limit= "4" data-section-id= "product-recommendations" data-section-type= "product-recommendations" data-intent= "related" >
If the user is using an alternate locale, then the locale is included in the div's data-base-url
. For example, /fr/recommendations/products
.
Step 2: Include the section in your product.liquid
template To display product recommendations at the bottom of your product page, include the section at the bottom of your templates/product.liquid
file:
In the Templates directory, open the product.liquid file.
Add the following code at the bottom of the file:
{% section 'product-recommendations' %}
Click Save .
Step 3: Edit your theme.js
file to load the recommendations asynchronously You need to load recommendations into the empty container that the section produced on the product page. Use JavaScript to make an HTTP GET request to <base_url>?section_id=<section_id>&product_id=<product_id>
.
In the Assets directory, open the theme.js file.
Find this line of code:
sections . register ( ' hero-section ' , theme . HeroSection );
Below that line, add this code:
sections . register ( ' product-recommendations ' , theme . ProductRecommendations );
Add the following code at the bottom of the file:
theme . ProductRecommendations = ( function () {
function ProductRecommendations ( container ) {
var $container = ( this . $container = $ ( container ));
var baseUrl = $container . data ( ' baseUrl ' );
var productId = $container . data ( ' productId ' );
var limit = $container . data ( ' limit ' );
var intent = $container . data ( ' intent ' );
var productRecommendationsUrlAndContainerClass = baseUrl + ' ?section_id=product-recommendations&limit= ' + limit +
' &product_id= ' + productId + ' &intent= ' + intent +
' .product-recommendations ' ;
$container . parent (). load ( productRecommendationsUrlAndContainerClass );
}
return ProductRecommendations ;
})();
Click Save .
Step 4: Edit your theme.scss.liquid
file to create the loading animation (optional) If you used the snippet that shows a loading animation inside your product recommendations section, add the following code at the bottom of your assets/theme.scss.liquid
file:
In the Assets directory, open the theme.scss.liquid file.
At the bottom of the file, add this code:
.product-recommendations {
padding-top : $section-spacing-small ;
padding-bottom : $section-spacing-small ;
@include media-query ( $medium-up ) {
padding-top : $section-spacing ;
padding-bottom : $section-spacing ;
}
}
.product-recommendations__loading-dots {
height : 350px ;
display : flex ;
align-items : center ;
justify-content : center ;
}
.product-recommendations__loading-dot {
animation : dot-keyframes 1 .5s infinite ease-in-out ;
background-color : $color-text ;
border-radius : 10px ;
display : inline-block ;
height : 10px ;
width : 10px ;
margin : 0 3px ;
& :nth-child ( 2 ) {
animation-delay : 0 .5s ;
}
& :nth-child ( 3 ) {
animation-delay : 1s ;
}
}
@keyframes dot-keyframes {
0 % {
opacity : 0 .4 ;
transform : scale ( 1 , 1 );
}
50 % {
opacity : 1 ;
transform : scale ( 1 .2 , 1 .2 );
}
100 % {
opacity : 0 .4 ;
transform : scale ( 1 , 1 );
}
}
Click Save .