如何创建Shopify功能为具有特定标签的客户提供免费送货

问题描述 投票:0回答:1

我正在尝试向我们的商店添加 Shopify 功能,其功能应该是为至少拥有以下两个标签之一的客户提供免费送货:“swell_vip_learned Lounger”、“swell_vip_pj professional”。

我创建了一个自定义应用程序并添加了“运费折扣”类型的扩展。该应用程序已成功安装,并且也显示折扣已应用。但当我以具有这些标签的客户身份打开结帐时,它不适用免费送货。我为 run.graphql 和 run.js 提供代码,因为它们只是扩展中的两个主要文件。请帮助我这里可能出了什么问题?

运行.grpahql

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");
  // fetch("https://haroon-ins-dev.free.beeceptor.com/", {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify(input) } )

  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 发送发布请求,但请求也未到达那里。

shopify shopify-app shopify-api
1个回答
0
投票

您提供的代码似乎走在正确的轨道上,但有一些问题可能会阻止免费送货的正确应用。让我们来分解一下:

1。记录问题:

  • 前端控制台: Shopify Functions 在后端运行,不直接与前端交互。因此,该函数内的控制台日志不会出现在浏览器的开发者控制台中。
  • Beeceptor 请求: 仔细检查 Beeceptor URL。这可能是一个拼写错误,或者需要在基本 URL 后添加特定路径。

2。免运费逻辑:

检查标签并应用折扣的逻辑看起来不错。但是,请考虑以下几点:

  • 区分大小写:确保
    customer?.hasTags.find
    中的标签比较考虑区分大小写。 Shopify 标签可能区分大小写。无论大小写,您都可以使用
    toLowerCase()
    确保匹配。
  • OR 条件: 您当前的逻辑使用 OR 条件来检查任一标签。这是可行的,但如果您想要求两个标签都免费送货,您可以更改逻辑以检查
    hasTag
    函数中的两个
    find
    属性是否为 true。

3.调试技巧:

  • 使用 Shopify 开发者工具 (https://www.youtube.com/watch?v=bWWmxULp8oM) 检查函数执行情况。您可以查看该函数接收到的输入数据以及记录的任何错误。
  • 暂时删除 Beeceptor 调用并专注于在函数中记录
    customer.hasTags
    ,以验证它是否正确检索客户标签。

这是

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: Input", input); // Log input data for debugging

  const SWELL_TIER_2_LOWER = "swell_vip_learned lounger".toLowerCase();
  const SWELL_TIER_3_LOWER = "swell_vip_pj professional".toLowerCase();

  let customer = input.cart.buyerIdentity.customer;
  const deliveryGroupId = input.cart.deliveryGroups.id;

  if (
    customer &&
    customer.hasTags.some((t) =>
      t.tag.toLowerCase() === SWELL_TIER_2_LOWER ||
      t.tag.toLowerCase() === SWELL_TIER_3_LOWER
    )
  ) {
    // 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;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.