首页 > 解决方案 > 我们可以使用 Shopify Storefront GraphQL API(非管理员)按 SKU 获取产品吗?

问题描述

我已经坚持了两天多。我找不到这方面的任何资源。使用 ADMIN API 有很多解决方案,但我没有管理员访问权限。我要解决的问题是:我只能访问产品的 SKU。我需要使用 Storefront API 从 Shopify 获取所有其他信息(标题、价格、描述、特色图片等...)。这是获取产品的功能:

function loadProducts(items) {
    let products = [];

    items.forEach((item) => {
        const sku = item.id;
        if (sku !== "undefined") {
            /* TODO: Need to figure out this query*/
            const query = `{
         products(first: 1, query: "sku:<sku>") {
    edges {
      node {
        title
        id
        description
      }
    }
  }
       }`;

            const STOREFRONT_ACCESS_TOKEN = 'xxxxxxxxxxxxxxx';
            const GRAPHQL_URL = 'https://<my-store>.myshopify.com/api/2021-01/graphql.json';
            const GRAPHQL_BODY = {

                'method': 'POST',
                'headers': {
                    'X-Shopify-Storefront-Access-Token': STOREFRONT_ACCESS_TOKEN,
                    'Content-Type': 'application/json',
                },
                'body': JSON.stringify({ query })

            }
            products.push(getData(GRAPHQL_URL, GRAPHQL_BODY));
        }
    });
    return Promise.all(products);
}
function getData(url, body) {
    return new Promise((resolve, reject) => {
        fetch(url, body)
            .then(res => res.json())
            .then(data => {
                resolve(data);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

如果您能将我重定向到正确的方向,我将不胜感激。请不要:我只假设使用 Storefront API,而不是 ADMIN API。谢谢!

标签: shopifyshopify-apishopify-storefront-api

解决方案


您无法使用 StoreFront API 按 SKU 查询产品。

StoreFront 产品的可用查询参数是:

  • available_for_sale
  • created_at
  • 产品类别
  • 标签
  • 标题
  • 更新时间
  • 变体.价格
  • 小贩

因此,您不能仅使用 StoreFront API 执行此操作,因为 SKU 未公开(如 Admin API)。


推荐阅读