Hello,
I am looking to create an extension similar to the discount experience here: https://shopify.dev/docs/apps/selling-strategies/discounts/experience/getting-started
but rather than apply a percentage monetary discount, apply free shipping when x amount of products have been added to the cart.
// -check
import { DiscountApplicationStrategy } from "../generated/api";
// Use JSDoc annotations for type safety
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
* @typedef {import("../generated/api").Target} Target
* @typedef {import("../generated/api").ProductVariant} ProductVariant
*/
/**
* {FunctionRunResult}
*/
const EMPTY_DISCOUNT = {
discountApplicationStrategy: DiscountApplicationStrategy.First,
discounts: [],
};
// The configured entrypoint for the 'purchase.product-discount.run' extension target
/**
* {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
const targets = input.cart.lines
// Only include cart lines with a quantity of two or more
// and a targetable product variant
.filter(line => line.quantity >= 2 &&
line.merchandise.__typename == "ProductVariant")
.map(line => {
const variant = /** {ProductVariant} */ (line.merchandise);
return /** {Target} */ ({
// Use the variant ID to create a discount target
productVariant: {
id: variant.id
}
})
});
if (!targets.length) {
// You can use STDERR for debug logs in your function
console.error("No cart lines qualify for volume discount.");
return EMPTY_DISCOUNT;
}
return {
//need to give the customer free shipping here
};
};
My graphQL is currently as follows:
query RunInput {
discountNode {
metafield(namespace: "$app:shipping-discount", key: "function-configuration") {
value
}
}
cart {
lines {
quantity
merchandise {
__typename
...on ProductVariant {
id
}
}
df{
}
}
}
}
Thanks