在產品頁面上顯示產品推薦

此教學課程說明如何在 Debut 佈景主題中,將產品推薦新增至產品頁面。若要深入瞭解產品推薦如何運作,請參閱在產品頁面上顯示產品推薦

步驟 1:建立 product-recommendations.liquid 區段

步驟 2:在 product.liquid 範本中包含此區段

若要在產品頁面最下方顯示產品推薦,請在 templates/product.liquid 檔案的最下方包含此區段:

  1. 在「範本」目錄中,開啟 product.liquid 檔案。
  2. 將下列程式碼新增至檔案最下方:
{% section 'product-recommendations' %}
  1. 點擊「儲存」。

步驟 3:編輯 theme.js 檔案,並以非同步方式載入產品建議

您必須將推薦產品載入至該區段在產品頁面上產生的空容器。使用 JavaScript 提出 HTTP GET 要求至 <base_url>?section_id=<section_id>&product_id=<product_id>

  1. 在「資產」目錄中,開啟 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. 在「資產」目錄中,開啟 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 銷售商品了嗎?

免費試用