You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在BigCommerce商品详情页通过SKU获取并展示其他商品数据?

Hey there! Let’s walk through how to pull product data by SKU and display those related items on your BigCommerce product detail page. I’ve got a few practical approaches depending on your theme setup and technical comfort level:

1. Client-Side Fetch with BigCommerce Storefront API

This is great if you want to dynamically load related products without server-side changes:

  • First, confirm your store has the Storefront API enabled (it’s turned on by default for most plans). For public product data, you won’t even need an API token.
  • Open your product detail template (usually product.html in your theme’s files) and add a script block to fetch the product by SKU. Here’s a working example:
// Replace 'TARGET_SKU' with your specific SKU value
const targetSku = 'TARGET_SKU';
// Fetch product data matching the SKU
fetch(`/api/storefront/products?filter=sku:${targetSku}`)
  .then(response => response.json())
  .then(data => {
    if (data.length > 0) {
      const relatedProduct = data[0];
      // Build HTML for your related product display
      const productHtml = `
        <div class="related-product-card">
          <a href="/${relatedProduct.url}" class="product-link">
            <img src="${relatedProduct.images[0].url}" alt="${relatedProduct.name}" class="product-image">
            <h4 class="product-name">${relatedProduct.name}</h4>
            <span class="product-price">${relatedProduct.price.formatted}</span>
          </a>
        </div>
      `;
      // Insert the HTML into your page (update the selector to match your container)
      document.querySelector('.related-products-container').innerHTML = productHtml;
    }
  })
  .catch(error => console.error('Oops, error fetching related product:', error));
  • Add CSS for the .related-product-card class in your theme’s stylesheet to match your store’s design.
2. Server-Side Template Rendering (Stencil Themes)

If you prefer rendering directly in your theme template (no client-side JS):

  • Stencil doesn’t have a direct "get product by SKU" helper, but you can use the product ID (linked to your target SKU) with the getProductById helper. First, grab the product ID from your BigCommerce admin (find the product, check its URL for the ID).
  • Add this code to your product.html template:
{{#assign 'targetProductId' = 12345}} <!-- Replace with your product's ID -->
{{#with getProductById(targetProductId)}}
  <div class="related-product-card">
    <a href="{{url}}">
      <img src="{{image.url}}" alt="{{name}}">
      <h4>{{name}}</h4>
      <span>{{price.formatted}}</span>
    </a>
  </div>
{{/with}}
  • Note: This will only work if the product is published and visible to your store’s visitors.
3. Backend Integration with BigCommerce REST API

For more advanced use cases (like bulk related products or custom filtering):

  • Create an API account in your BigCommerce admin with the products_read scope.
  • Make a GET request to fetch products by SKU:
GET /v3/catalog/products?sku=TARGET_SKU
  • Parse the response, extract the product data, and pass it to your frontend template. This is ideal if you need to handle multiple SKUs at once (use filter=sku:in('SKU1','SKU2') to fetch multiple products in one call).
Key Tips to Keep in Mind
  • CORS Check: Most BigCommerce stores are configured to allow client-side fetch requests, but if you run into errors, double-check your store’s CORS settings in the admin.
  • Performance: If you’re loading multiple related products, batch your API calls to avoid slowing down the page.
  • Caching: Consider caching product data (either server-side or with browser cache) to reduce repeated API requests.

内容的提问来源于stack exchange,提问作者Priya Dinesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:49:23