使用 Shopify 按句柄检索产品 Javascript 购买 SDK

Retrieve product by handle with Shopify Javascript Buy SDK

我正在将 Shopify 商店实施到 WordPress 主题中,我需要做的一件事是使用产品句柄而不是 ID 查询 Shopify 产品,如他们的文档所示:

http://shopify.github.io/js-buy-sdk/api/classes/ShopClient.html#method-fetchProduct

这样做的原因是我想要具有 SEO 友好 URL 的单一产品视图,精确地从产品句柄构建。

在 Shopify 的网站上,有人提到了一种未记录的方法来实现这一点,但我无法让它工作: https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/how-do-i-query-for-a-product-using-its-handle-322118#comment-346792

有没有人碰过这个?

据我所知,API 没有办法做到这一点。您可能最终不得不存储 ID 到句柄的映射,并通过 webhook 更新该映射。

我想 fetchQueryProducts() 就是您要找的。例如:

var shopClient = ShopifyBuy.buildClient({
  apiKey: 'my_key',
  myShopifyDomain: 'your_myshopify_domain',
  appId: 6
});

shopClient.fetchQueryProducts({handle: 'your_product_handle'}).then(function(products) {
  // Your product is at products[0] if successful
});

您可以使用它按句柄获取产品。

client.product.fetchByHandle(handle).then(
        product => { // Your product },
        error => { // error },
)