Hi, we have various Shopify functions running to create an x for xx discount, i.e. 2 for $25 based on product tags, with custom messages to tell customers to add an item to get full discount, and discount only quantities of two. We also have that for a 3 for $39 etc.
What we would want to do is offer this discount at Checkout based on a discount code. Is this possible using Checkout Extension with cart lines:
And with Discounts api:
I.E. check to see if discount code is applied, then discount multiples of two, message customer to add one if qualifying products based on tags are even, etc. ?
The idea is to send a code to our most select customers so only they get the discount, not every customer.
Edited to add, the products that would be eligible for a 2 for XX discount etc. could all have different prices, so I would need to do something like this for non-even quantities:
const targets = input.cart.lines
// Only include cart lines with a quantity of two or more
.filter((line) => line.merchandise.__typename == "ProductVariant" && line.merchandise.product.two_for_tag == true)
.map((line) => {
return /** @type {Target} */ ({
// Use the cart line ID to create a discount target
cartLine: {
id: line.id,
},
lineitemPrice: line.cost.amountPerQuantity.amount,
lineitemQty: line.quantity,
});
});
<snip>
// below is code fragment for non-even qualifying quantities
var obj = new Object();
obj.targets = [{ cartLine: value.cartLine}] ;
var temp_discount = ( (parseFloat(value.lineitemPrice).toFixed(2) - 12.50) * ( value.lineitemQty - 1) ).toFixed(2);
//num.toLocaleString('en-US', {minimumFractionDigits: 8, useGrouping: false})
var temp_discount_str = temp_discount.toLocaleString('en-US', {minimumFractionDigits: 2, useGrouping: false});
obj.message = "Add one more to get full discount!";
obj.value = { "fixedAmount": {amount: temp_discount_str}};
customDiscount.discounts.push(obj);
<snip>
Thanks!