Shopify运费折扣函数故障排查:指定标签客户免运费未生效
Shopify Shipping Discount函数不生效:VIP标签客户免运费未触发
我正在为Shopify店铺开发一个shipping-discount类型的函数,目标是给带有「swell_vip_learned lounger」或「swell_vip_pj professional」标签的客户提供免运费服务。目前已完成自定义应用创建、扩展添加,应用安装成功且折扣已启用,但使用对应标签的客户账号结账时,免运费并未生效。
核心代码
run.graphql
query RunInput { cart { deliveryGroups{ id } buyerIdentity { customer { hasTags(tags: ["swell_vip_learned lounger", "swell_vip_pj professional"]) { hasTag tag } } } cost { subtotalAmount { amount } } } }
run.js
/** * @typedef {import("../generated/api").RunInput} RunInput * @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult */ /** * @type {FunctionRunResult} */ const EMPTY_DISCOUNT = { discounts: [], }; /** * @param {RunInput} input * @returns {FunctionRunResult} */ export function run(input) { console.log("Free Shipping Validation Function"); const headers = new Headers(); headers.append('Authorization', 'Bearer SOME-VALUE'); fetch('https://haroon-ins-dev.free.beeceptor.com', { method: 'POST', headers: headers, body: JSON.stringify(input) }) .then(response => response.text()) .then(data => { console.log(data); }) .catch(error => { console.error(error); }); const SWELL_TIER_2 = "swell_vip_learned lounger"; const SWELL_TIER_3 = "swell_vip_pj professional"; let customer = input.cart.buyerIdentity.customer; const deliveryGroupId = input.cart.deliveryGroups.id; if ( // customer has tags customer && ( customer?.hasTags.find((t) => t.tag === SWELL_TIER_2)?.hasTag === true || customer?.hasTags.find((t) => t.tag === SWELL_TIER_3)?.hasTag === true ) ) { // return free shipping const ShippingDiscount = { discounts: [ { message: "Free VIP Shipping", targets: { deliveryGroup: { id: deliveryGroupId, }, }, value: { percentage: 100, }, }, ], }; return ShippingDiscount; } else { // otherwise return empty discount (no free shipping) return EMPTY_DISCOUNT; } }
调试遇到的问题
- 添加
console.log后前端控制台无输出 - 向Beeceptor发送的POST请求未到达目标地址
问题排查与修复方案
1. 核心错误:配送组对象访问错误
input.cart.deliveryGroups返回的是数组(一个购物车可能包含多个配送组),直接访问input.cart.deliveryGroups.id会得到undefined,导致折扣无法关联到有效配送组,这是免运费不生效的主要原因。
2. 调试方式错误
Shopify函数的运行日志不会显示在前端浏览器控制台,需在Shopify后台 > 设置 > 函数 > 对应shipping-discount函数的日志面板查看。另外,函数运行环境对外部请求有严格权限限制,未授权的请求会被拦截,这也是Beeceptor请求未到达的原因。
3. 标签判断逻辑优化
hasTags查询返回的是所有目标标签的匹配结果数组,无需逐个匹配标签值,直接检查是否存在hasTag: true的项即可。
修正后的run.js代码
/** * @typedef {import("../generated/api").RunInput} RunInput * @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult */ const EMPTY_DISCOUNT = { discounts: [], }; export function run(input) { const customer = input.cart.buyerIdentity.customer; const deliveryGroups = input.cart.deliveryGroups; // 检查客户是否拥有目标VIP标签 const hasVipTag = customer?.hasTags.some(tagResult => tagResult.hasTag); if (hasVipTag && deliveryGroups.length > 0) { // 为所有配送组添加免运费折扣 const discounts = deliveryGroups.map(group => ({ message: "Free VIP Shipping", targets: { deliveryGroup: { id: group.id } }, value: { percentage: 100 } })); return { discounts }; } return EMPTY_DISCOUNT; }
额外调试建议
- 验证标签匹配:在run.graphql中添加
tags字段,直接返回客户所有标签,确认是否与目标标签完全一致(含大小写、空格):query RunInput { cart { deliveryGroups{ id } buyerIdentity { customer { tags # 新增:查看客户所有标签 hasTags(tags: ["swell_vip_learned lounger", "swell_vip_pj professional"]) { hasTag tag } } } } } - 外部请求权限:若需调用外部API,需在Shopify应用的权限设置中添加对应API访问权限,并确保请求地址在Shopify允许列表内。
内容的提问来源于stack exchange,提问作者Haroon Ahmed
相关产品推荐
相关产品推荐

