在产品页面上显示产品推荐

本教程介绍如何在 Debut 模板中向产品页面添加产品推荐。若要详细了解产品推荐的工作方式,请参阅在产品页面上显示产品推荐

步骤 1:创建 product-recommendations.liquid 分区

步骤 2:在 product.liquid 模板中包含该分区

若要在产品页面底部显示产品推荐,请在 templates/product.liquid 文件底部包含该分区:

  1. Templates 目录中,打开 product.liquid 文件。
  2. 在文件底部添加以下代码:
{% section 'product-recommendations' %}
  1. 点击保存

步骤 3:编辑您的 theme.js 文件以便以异步方式加载推荐

您需要将推荐加载到该分区在产品页面上生成的空容器中。使用 JavaScript 向 <base_url>?section_id=<section_id>&product_id=<product_id> 发出 HTTP GET 请求。

  1. Assets 目录中,打开 theme.js 文件。
  2. 查找此代码行:
sections.register('hero-section', theme.HeroSection);
  1. 在此行下方添加以下代码:
sections.register('product-recommendations', theme.ProductRecommendations);
  1. 在文件底部添加以下代码:
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;
})();
  1. 点击保存

步骤 4:编辑 theme.scss.liquid 文件以创建加载动画(可选)

如果您使用了可在产品推荐分区内显示加载动画的代码片段,请在 assets/theme.scss.liquid 文件底部添加以下代码:

  1. Assets 目录中,打开 theme.scss.liquid 文件。
  2. 在文件底部,添加此代码:
.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);
  }
}
  1. 点击保存

准备好开始使用 Shopify 进行销售了吗?

免费试用