使用Python的ShopifyAPI添加商品元字段遇问题:返回metafield(None)
Shopify API创建商品后添加元字段失败问题解决
我用Shopify API创建商品没问题,但执行product.save()后添加元字段时遇到困难。商品能在Shopify后台显示,但元字段返回metafield(None)。我的代码如下:
import shopify # Configure store details shop_url = 'store.myshopify.com' admin_api_key = 'shpat_' api_version = '2022-07' # Create and activate a new session session = shopify.Session(shop_url, api_version, admin_api_key) shopify.ShopifyResource.activate_session(session) # Create a new product Object, set the title product = shopify.Product() product.handle = "new-product" product.title = "New product" product.body_html = "Test description" product.product_type = "Type" product.vendor = "Vendor" product.product_type = "Category" product.tags = "Tag" product.price = "9.99" product.weight = "1.0" product.weight_unit = 'kg' variant = shopify.Variant({'sku': 'P1', 'price': 9.99, 'requires_shipping': True}) product.variants = [variant] # Save the product object to Shopify product.save() # Add the metafield to the saved product object "product.metafields.custom.maker" product.add_metafield(shopify.Metafield({ 'namespace':"custom", 'key':"maker", 'value':"Maker", 'type':"string" })) # Save the updated product object to Shopify product.save()
问题原因
直接在已保存的product对象上调用add_metafield后再执行save(),这种方式在Shopify Python API中无法确保元字段正确同步到服务器——元字段属于独立的API资源,不能通过修改商品对象的方式间接保存。另外代码中product.price的设置是无效的,商品价格只能通过Variant对象配置。
解决方案
有两种可靠的实现方式:
方式一:创建商品时直接包含元字段
初始化Product对象时就嵌入元字段,一次性完成保存操作:
import shopify # 配置商店信息 shop_url = 'store.myshopify.com' admin_api_key = 'shpat_' api_version = '2022-07' # 创建并激活会话 session = shopify.Session(shop_url, api_version, admin_api_key) shopify.ShopifyResource.activate_session(session) # 初始化商品并设置属性 product = shopify.Product() product.handle = "new-product" product.title = "New product" product.body_html = "Test description" product.product_type = "Category" product.vendor = "Vendor" product.tags = "Tag" product.weight = "1.0" product.weight_unit = 'kg' # 创建商品变体 variant = shopify.Variant({'sku': 'P1', 'price': 9.99, 'requires_shipping': True}) product.variants = [variant] # 直接添加元字段到商品对象 metafield = shopify.Metafield({ 'namespace':"custom", 'key':"maker", 'value':"Maker", 'type':"string" }) product.metafields = [metafield] # 一次性保存商品及关联元字段 product.save()
方式二:单独创建元字段并关联已保存商品
如果必须先保存商品再添加元字段,需单独创建Metafield对象并指定关联的商品ID:
import shopify # 配置商店信息 shop_url = 'store.myshopify.com' admin_api_key = 'shpat_' api_version = '2022-07' # 创建并激活会话 session = shopify.Session(shop_url, api_version, admin_api_key) shopify.ShopifyResource.activate_session(session) # 创建并保存商品 product = shopify.Product() product.handle = "new-product" product.title = "New product" product.body_html = "Test description" product.product_type = "Category" product.vendor = "Vendor" product.tags = "Tag" product.weight = "1.0" product.weight_unit = 'kg' variant = shopify.Variant({'sku': 'P1', 'price': 9.99, 'requires_shipping': True}) product.variants = [variant] product.save() # 单独创建元字段并关联到目标商品 metafield = shopify.Metafield({ 'namespace':"custom", 'key':"maker", 'value':"Maker", 'type':"string", 'owner_resource': 'product', 'owner_id': product.id }) metafield.save()
内容的提问来源于stack exchange,提问作者Logan S
相关产品推荐
相关产品推荐

