shopify在产品页面上显示产品推荐步骤
在产品页面上显示产品推荐注意这属于高级教程,Shopify 不提供相关支持。此教程要求您具有 HTML、CSS、JavaScript 和 Liquid 等 Web 设计语言的知识。如果您需要帮助,那么
目录
- 答案
- 文章目录
- shopify在产品页面上显示产品推荐步骤详细回答
shopify在产品页面上显示产品推荐步骤详细回答
在产品页面上显示产品推荐
注意
这属于高级教程,Shopify 不提供相关支持。此教程要求您具有 HTML、CSS、JavaScript 和 Liquid 等 Web 设计语言的知识。如果您需要帮助,那么您可以雇用 Shopify 专家。
备注
此自定义用于复古 Shopify 模板,不适用于“Online Store 2.0”模板。
找到您的模板架构版本
探索“Online Store 2.0”模板自定义
本教程介绍如何在 Debut 模板中向产品页面添加产品推荐。若要详细了解产品推荐的工作方式,请参阅在产品页面上显示产品推荐。
备注:默认情况下,以下 Shopify 模板的最新版本中包含产品推荐:
Boundless
Brooklyn
Debut
Minimal
Narrative
Simple
Venture
如果您使用的是上方某个模板的旧版本,则您可以在更新模板后显示产品推荐,而无需自定义模板代码。
本页相关主题
步骤 1:创建
product-recommendations.liquid
分区步骤 2:在
product.liquid
模板中包含该分区步骤 3:编辑您的
theme.js
文件以便以异步方式加载推荐步骤 4:编辑
theme.scss.liquid
文件以创建加载动画(可选)
步骤 1:创建 product-recommendations.liquid
分区
PC:
在 Shopify 后台中,转到在线商店 > 模板。
找到要编辑的模板,然后点击操作 > 编辑代码。
在 Sections 目录中,点击添加新分区。
将新分区命名为
product-recommendations
,然后点击创建分区。将所有内容替换为下方代码:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.performed %} {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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> <div></div> <div></div> <div></div> </div> {% endif %} </div>
点击保存。
当在产品页面中呈现该分区时,recommendations.performed
将为 false
,因此生成的 HTML 将显示加载动画:
<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"> <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>
备注:推荐的产品需要使用 JavaScript 进行异步加载。您将在步骤 3:编辑您的 theme.js
文件以便以异步方式加载推荐中加载推荐的产品。
如果您不想显示加载动画,请改为使用此代码:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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>
当在产品页面中呈现上述分区时,生成的 HTML 将为不包含任何内容的 div
元素:
<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">
如果用户使用的是备用区域设置,则区域设置将包含在 div 的 data-base-url
中。例如,/fr/recommendations/products
。
苹果系统:
在 Shopify 应用中,轻触商店。
在销售渠道部分中,轻触在线商店。
轻触 Manage themes(管理模板)。
找到要编辑的模板,然后点击操作 > 编辑代码。
在 Sections 目录中,点击添加新分区。
将新分区命名为
product-recommendations
,然后点击创建分区。将所有内容替换为下方代码:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.performed %} {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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> <div></div> <div></div> <div></div> </div> {% endif %} </div>
点击保存。
当在产品页面中呈现该分区时,recommendations.performed
将为 false
,因此生成的 HTML 将显示加载动画:
<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"> <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>
备注:推荐的产品需要使用 JavaScript 进行异步加载。您将在步骤 3:编辑您的 theme.js
文件以便以异步方式加载推荐中加载推荐的产品。
如果您不想显示加载动画,请改为使用此代码:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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>
当在产品页面中呈现上述分区时,生成的 HTML 将为不包含任何内容的 div
元素:
<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">
如果用户使用的是备用区域设置,则区域设置将包含在 div 的 data-base-url
中。例如,/fr/recommendations/products
。
安卓系统:
在 Shopify 应用中,轻触商店。
在销售渠道部分中,轻触在线商店。
轻触 Manage themes(管理模板)。
找到要编辑的模板,然后点击操作 > 编辑代码。
在 Sections 目录中,点击添加新分区。
将新分区命名为
product-recommendations
,然后点击创建分区。将所有内容替换为下方代码:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.performed %} {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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> <div></div> <div></div> <div></div> </div> {% endif %} </div>
点击保存。
当在产品页面中呈现该分区时,recommendations.performed
将为 false
,因此生成的 HTML 将显示加载动画:
<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"> <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>
备注:推荐的产品需要使用 JavaScript 进行异步加载。您将在步骤 3:编辑您的 theme.js
文件以便以异步方式加载推荐中加载推荐的产品。
如果您不想显示加载动画,请改为使用此代码:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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>
当在产品页面中呈现上述分区时,生成的 HTML 将为不包含任何内容的 div
元素:
<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">
如果用户使用的是备用区域设置,则区域设置将包含在 div 的 data-base-url
中。例如,/fr/recommendations/products
。
步骤 2:在 product.liquid
模板中包含该分区
若要在产品页面底部显示产品推荐,请在 templates/product.liquid
文件底部包含该分区:
在 Templates 目录中,打开 product.liquid 文件。
在文件底部添加以下代码:
{% section 'product-recommendations' %}
点击保存。
步骤 3:编辑您的 theme.js
文件以便以异步方式加载推荐
您需要将推荐加载到该分区在产品页面上生成的空容器中。使用 JavaScript 向 <base_url>?section_id=<section_id>&product_id=<product_id>
发出 HTTP GET 请求。
在 Assets 目录中,打开 theme.js 文件。
查找此代码行:
sections.register('hero-section', theme.HeroSection);
在此行下方添加以下代码:
sections.register('product-recommendations', theme.ProductRecommendations);
在文件底部添加以下代码:
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 productRecommendationsUrlAndContainerClass = baseUrl + '?section_id=product-recommendations&limit=' + limit + '&product_id=' +productId + ' .product-recommendations'; $container.parent().load(productRecommendationsUrlAndContainerClass); } return ProductRecommendations; })();
点击保存。
步骤 4:编辑 theme.scss.liquid
文件以创建加载动画(可选)
如果您使用了可在产品推荐分区内显示加载动画的代码片段,请在 assets/theme.scss.liquid
文件底部添加以下代码:
在 Assets 目录中,打开 theme.scss.liquid 文件。
在文件底部,添加此代码:
.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); } }
点击保存。
Shopify商户官网原文详情:
Show product recommendations on the product page
Caution
This is an advanced tutorial and is not supported by Shopify. Knowledge of web design languages such as HTML, CSS, JavaScript, and Liquid is required. If you need help, then you can hire a Shopify expert.
Note
This customization is for vintage Shopify themes, and doesn't apply to Online Store 2.0 themes.
Find out your theme's architecture version
Explore Online Store 2.0 theme customizations
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.
Note
The latest versions of the following Shopify themes include product recommendations by default:
Boundless
Brooklyn
Debut
Minimal
Narrative
Simple
Venture
If you're using an older version of one of these themes, then you can show product recommendations after you update your theme instead of customizing its code.
On this page
Step 1: Create a
product-recommendations.liquid
sectionStep 2: Include the section in your
product.liquid
templateStep 3: Edit your
theme.js
file to load the recommendations asynchronouslyStep 4: Edit your
theme.scss.liquid
file to create the loading animation (optional)Step 1: Create a
product-recommendations.liquid
sectionPC:
From your Shopify admin, go to Online Store > Themes.
Find the theme you want to edit, and then click Actions > 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 heading = 'You may also like' %} {% 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"> {% if recommendations.performed %} {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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> <div></div> <div></div> <div></div> </div> {% endif %} </div>Click Save.
When the section is rendered with the product page,
recommendations.performed
will befalse
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"> <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>
Note
The recommended products need to be loaded asynchronously using JavaScript. You will load them in Step 3: Edit your
theme.js
file to load the recommendations asynchronously.
If you don't want to show a loading animation, use this code instead:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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">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 Store.
In the Sales channels section, tap Online Store.
Tap Manage themes.
Find the theme you want to edit, and then click Actions > 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 heading = 'You may also like' %} {% 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"> {% if recommendations.performed %} {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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> <div></div> <div></div> <div></div> </div> {% endif %} </div>Click Save.
When the section is rendered with the product page,
recommendations.performed
will befalse
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"> <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>
Note
The recommended products need to be loaded asynchronously using JavaScript. You will load them in Step 3: Edit your
theme.js
file to load the recommendations asynchronously.
If you don't want to show a loading animation, use this code instead:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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">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 Store.
In the Sales channels section, tap Online Store.
Tap Manage themes.
Find the theme you want to edit, and then click Actions > 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 heading = 'You may also like' %} {% 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"> {% if recommendations.performed %} {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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> <div></div> <div></div> <div></div> </div> {% endif %} </div>Click Save.
When the section is rendered with the product page,
recommendations.performed
will befalse
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"> <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>
Note
The recommended products need to be loaded asynchronously using JavaScript. You will load them in Step 3: Edit your
theme.js
file to load the recommendations asynchronously.
If you don't want to show a loading animation, use this code instead:
{% assign heading = 'You may also like' %} {% 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"> {% if recommendations.products_count > 0 %} <div class="section-header text-center"> <h2>{{ heading }}</h2> </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">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
templateTo 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 asynchronouslyYou 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 productRecommendationsUrlAndContainerClass = baseUrl + '?section_id=product-recommendations&limit=' + limit + '&product_id=' +productId + ' .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.
文章内容来源:Shopify商户官方网站