如何通过shopify-api-node基于销售渠道获取Shopify商品?
基于销售渠道筛选Shopify商品(使用shopify-api-node)
Shopify的商品本身不直接绑定销售渠道,而是通过销售渠道的可见性设置控制商品是否在该渠道展示。要筛选指定渠道关联的商品,需要分两步操作:
1. 获取目标销售渠道的ID
首先调用销售渠道列表接口,找到名称为test的渠道ID:
const Shopify = require('shopify-api-node'); const shopify = new Shopify({ shopName: '你的店铺域名.myshopify.com', apiKey: '你的API密钥', password: '你的API密码' }); async function getTestChannelId() { const channels = await shopify.salesChannel.list(); const testChannel = channels.find(channel => channel.name === 'test'); return testChannel ? testChannel.id : null; }
2. 按渠道ID筛选商品
借助商品API的published_status参数,指定仅获取在目标渠道已发布的商品:
async function getProductsForTestChannel() { const channelId = await getTestChannelId(); if (!channelId) { console.log('未找到名为test的销售渠道'); return []; } // 筛选test渠道可见的商品 const products = await shopify.product.list({ published_status: `published:${channelId}` }); return products; } // 调用示例 getProductsForTestChannel().then(products => { console.log('test渠道关联的商品:', products); }).catch(err => { console.error('获取商品失败:', err); });
额外说明
- 确保你的API权限包含
read_products和read_sales_channels,否则会触发权限错误。 - 若要获取未在test渠道发布的商品,可将
published_status设为unpublished:${channelId}。 - 商品若在多个渠道发布,该筛选只会返回指定渠道下已发布的商品,不影响其他渠道的状态判断。
内容的提问来源于stack exchange,提问作者Rezaul karim
相关产品推荐
相关产品推荐

