When a customer applies a (Basic Code) discount with a fixed amount we would like to display a message to them if the discount is not maxed out.
Example:
-
Cart with 1 item worth $15.
-
Customer applies a Product-level $20 fixed-amount discount code.
-
The amount off is $15.
-
We would like to display a message that says “You still have $5 on your discount.”
I understand that current behavior may not provide this but since it is an extension that uses the storefront API we cannot query the admin for the discount by it’s code from the extension.
Is there a best practice to get the Discount’s original amount info?
@Brian_Hunter there’s no direct way to get the discount’s original configured amount from the Checkout UI Extension. The extension only sees what was actually applied, not what the discount was set to.
One workaround is handling this server side. When the discount code is entered you can query the Admin API from your app backend to get the discount details, then pass the original amount back to the extension via a metafield or a cart attribute:
// In your app backend
const discount = await fetchDiscountByCode(code); // Admin API
const originalAmount = discount.customerGets.value.amount;
// Store it somewhere the extension can read
await updateCartAttribute('discount_original_amount', originalAmount);
Then in the extension read it from cart attributes and do the math:
const originalAmount = cart.attributes.find(
a => a.key === 'discount_original_amount'
)?.value;
const applied = cart.discountAllocations.reduce(
(sum, d) => sum + parseFloat(d.discountedAmount.amount), 0
);
const remaining = parseFloat(originalAmount) - applied;
Not the cleanest setup but it works. I hope so, and that this method works for you.