Shopify Admin API:如何获取折扣订单的正确价格?
问题:通过Shopify Admin API获取与UI一致的订单行项价格
我正在用GraphQL调用Shopify Admin API搭建Shopify与ERP的集成,其余功能均正常,唯独获取订单准确价格这块存在问题。
根据Shopify文档说明,discountedTotalSet字段应返回折扣应用后的行项总价,但实际返回的是原价。我需要获取与订单UI完全一致的逐行价格,请问如何通过API实现?
当前使用的查询语句
{ node(id: "gid://shopify/Order/4866288156908") { id ...on Order { name lineItems (first: 50) { edges { node { id quantity sku discountedTotalSet { shopMoney { currencyCode amount } } } } } } } }
查询结果
返回的amount为599.0,但这与订单UI显示的价格不符:
{ "data": { "node": { "id": "gid://shopify/Order/4866288156908", "name": "AK-1003", "lineItems": { "edges": [ { "node": { "id": "gid://shopify/LineItem/12356850286828", "quantity": 1, "sku": "AK-A1081", "discountedTotalSet": { "shopMoney": { "currencyCode": "AUD", "amount": "599.0" } } } } ] } } } }
解决方案
要获取与UI完全一致的行项价格,需根据折扣类型调整查询字段:
1. 区分折扣类型
- 行项专属折扣:折扣仅针对单个商品设置,此时
discountedTotalSet应返回正确的折扣后总价,若仍异常,可补充查询discountedUnitPriceSet(折扣后单价),乘以数量验证结果。 - 订单级折扣:如全局优惠券、满减等,这类折扣不会直接体现在
discountedTotalSet中,需通过discountAllocations字段获取行项分摊的折扣金额。
2. 调整后的查询语句
{ node(id: "gid://shopify/Order/4866288156908") { id ...on Order { name lineItems (first: 50) { edges { node { id quantity sku # 行项原价总价 totalSet { shopMoney { currencyCode amount } } # 行项自身折扣后的总价 discountedTotalSet { shopMoney { currencyCode amount } } # 订单级折扣的分摊金额 discountAllocations { allocatedAmountSet { shopMoney { currencyCode amount } } } # 折扣后的单价(包含所有折扣类型) discountedUnitPriceSet { shopMoney { currencyCode amount } } } } } } } }
3. 计算最终总价
若存在订单级折扣,最终行项总价 = totalSet.amount - 所有discountAllocations.allocatedAmountSet.amount之和。
另外,discountedUnitPriceSet返回的是单个商品最终折扣后的单价,乘以quantity也能得到与UI一致的行项总价。
内容的提问来源于stack exchange,提问作者tgflash
相关产品推荐
相关产品推荐

