shopify - 我们可以使用 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。谢谢!
解决方案
您无法使用 StoreFront API 按 SKU 查询产品。
StoreFront 产品的可用查询参数是:
- available_for_sale
- created_at
- 产品类别
- 标签
- 标题
- 更新时间
- 变体.价格
- 小贩
因此,您不能仅使用 StoreFront API 执行此操作,因为 SKU 未公开(如 Admin API)。
推荐阅读
- sql - 如果字段不为空,则赋值
- node.js - 即使安装了 cors nom 软件包,也会出现 CORS 错误
- powershell - 从 Powershell 中的多个下拉列表中返回多个值
- google-apps-script - 查询日历事件列表中组织者电子邮件地址的文本
- reactjs - React 中的身份验证
- python - 在 python-docx 的同一行中添加两个图像
- excel - 运行时错误“91”对象变量或未设置块变量在早期工作
- angular - 第一次尝试时未显示数据
- uwp - 如何导航到 UWP 页面的实例?
- perl - 为什么官方的 perl docker 镜像有两个版本的 perl?