只是寻找一些关于此的指导。假设我的产品 ID 为 7025617862758,付款选项为信用卡和 AfterPay。当该产品添加到购物车后,无论购物车中还有什么其他产品,我都想隐藏 AfterPay 付款选项,并且只允许信用卡。
我正在考虑为此创建一个应用程序(我没有这样做的经验),然后使用 graphql 进行购物车查询以检查它有哪些产品。如果该 ID 在购物车中,则删除付款选项。
这听起来是正确的方法吗?还是我把事情复杂化了?
是的,这绝对是正确的方法,创建自定义应用程序并不那么复杂。 Shopify 的文档相当不错。
您需要使用“新”结账扩展功能。
一个例子是:
// @ts-check
// Use JSDoc annotations for type safety
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
*/
/**
* @type {FunctionRunResult}
*/
const NO_CHANGES = {
operations: [],
};
// The configured entrypoint for the 'purchase.payment-customization.run' extension target
/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
// Get the cart total from the function input, and return early if it's below 100
const cartTotal = parseFloat(input.cart.cost.totalAmount.amount ?? "0.0");
if (cartTotal < 100) {
// You can use STDERR for debug logs in your function
console.error("Cart total is not high enough, no need to hide the payment method.");
return NO_CHANGES;
}
// Find the payment method to hide
const hidePaymentMethod = input.paymentMethods
.find(method => method.name.includes("Cash on Delivery"));
if (!hidePaymentMethod) {
return NO_CHANGES;
}
// The @shopify/shopify_function package applies JSON.stringify() to your function result
// and writes it to STDOUT
return {
operations: [{
hide: {
paymentMethodId: hidePaymentMethod.id
}
}]
};
};
您可以通过此链接找到更多详细信息https://shopify.dev/docs/apps/checkout/ payments/getting-started