我们是否可以使用Shopify Storefront GraphQL API(非管理员)按SKU获取产品? [英] Can we get product by SKU using Shopify Storefront GraphQL API(NOT ADMIN)?

查看:49
本文介绍了我们是否可以使用Shopify Storefront GraphQL API(非管理员)按SKU获取产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在这件事上已经两天多了。我找不到任何关于这方面的资料。有很多解决方案通过使用管理API,但我没有管理员访问权限。我试图解决的问题是:我只能访问Product的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,而不是管理API。谢谢!

推荐答案

无法使用Storefront接口按单品单元查询产品。

店面产品可用的查询参数为:

  • 可供销售
  • 创建时间
  • PRODUCT_TYPE
  • 标签
  • 标题
  • 已更新_at
  • 变体。价格
  • 供应商

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

这篇关于我们是否可以使用Shopify Storefront GraphQL API(非管理员)按SKU获取产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆