不幸的是,正如您所指出的,由于 Shopify API 的结构方式,我认为没有一种有效的方法可以做到这一点。它不允许从产品中查询集合,而只允许从集合中查询产品。也就是说,看不到一个产品属于哪些系列,但可以看到哪些产品属于一个系列。
ShopifyAPI::Collect 或 ShopifyAPI::Collection REST 资源不返回产品变体信息,这是根据要求获取价格信息所必需的。此外,ShopifyAPI::Collect 仅限于自定义集合,不适用于ShopifyAPI::SmartCollection 中的产品。出于这个原因,我建议使用 GraphQL 而不是 REST 来获取所需的信息。
query ($collectionCursor: String, $productCursor: String){
collections(first: 1, after: $collectionCursor) {
edges {
cursor
node {
id
handle
products(first: 8, after: $productCursor){
edges{
cursor
node{
id
title
variants(first: 100){
edges{
node{
price
}
}
}
}
}
}
}
}
}
}
{
"collectionCursor": null,
"productCursor": null
}
$productCursor 变量可用于迭代集合中的所有产品,$collectionCursor 可用于迭代所有集合。请注意,只需查询前 100 个变体,因为 Shopify 对每个产品有 100 个变体的硬性限制。
可以使用相同的查询来遍历ShopifyAPI::SmartCollection。
另外,使用 REST API 的相同查询在 Ruby 中看起来像这样。
collections = ShopifyAPI::Collection.all # paginate
collection.each do |collection|
collection.products.each do |product|
product.title
# note the extra call the Product API to get varint info
ShopifyAPI::Product.find(product.id).variants.each do |varaint|
variant.price
end
end
end
我看不出有任何方法可以解决 REST 查询的低效问题,但您可以使用 Shopify 的 GraphQL Bulk Operations 改进 GraphQL 查询。