如何限制Shopify Function仅对已登录客户生效?
问题:Product Discount API 未认证用户输入客户邮箱即可享受标签折扣的解决方案
我通过Product Discount API实现了基于客户标签的自动折扣功能,使用的GraphQL输入如下:
query Input { cart { buyerIdentity { customer { id test: hasAnyTag(tags: ["test"]) } } lines { merchandise { __typename ...on ProductVariant { id product { ... } } } } } }
但发现未认证用户只要输入符合标签条件的客户邮箱,就能享受折扣(哪怕收货邮箱和客户邮箱不同,商品依然会打折)。客户对象没有isLoggedIn属性,之前在Script Editor中可以通过if customer判断用户是否登录,现在不想强制所有用户登录结账,尝试过Checkout Validation API也没能解决问题,求可行的解决方案。
附测试输入示例:
{ "buyerJourney": { "step": "CHECKOUT_INTERACTION" }, "cart": { "buyerIdentity": { "email": "alksdj@gmail.com", "customer": { "id": "gid://shopify/Customer/6936468160786", } }, "lines": [ {"quantity": 2} ] } }
查询Schema:
query Input { cart { buyerIdentity { email customer { id } } lines { quantity } } }
解决方案
加认证状态判断:在Product Discount的Input Query里加上
buyerIdentity.authenticationStatus字段,这个字段能返回用户的认证状态,有AUTHENTICATED(已登录)、UNAUTHENTICATED(未登录)、VERIFIED_EMAIL(仅验证邮箱)三种值。只有当状态是AUTHENTICATED时,才算真正登录的客户,再结合标签判断要不要给折扣。
改后的Input Query示例:query Input { cart { buyerIdentity { authenticationStatus customer { id test: hasAnyTag(tags: ["test"]) email } email } lines { merchandise { __typename ...on ProductVariant { id product { ... } } } } } }双重校验邮箱匹配:同时核对
buyerIdentity.email和customer.email是否完全一致,防止未登录用户随便填个符合标签的客户邮箱蹭折扣。调整折扣触发逻辑:在折扣规则里加三个判断条件:
buyerIdentity.authenticationStatus必须等于AUTHENTICATEDcustomer.test为true(也就是客户有目标标签)- 可选:
buyerIdentity.email和customer.email完全匹配
只有同时满足这些条件,才给自动折扣。
要注意的是,Product Discount API的规则得用Shopify的规则表达式来写这些判断,确保只有真正登录的标签客户能拿到折扣,同时不强制所有用户登录结账。
内容的提问来源于stack exchange,提问作者TwistedOwl
相关产品推荐
相关产品推荐

