Short description of issue
API product discounts are not displayed.
Reproduction steps
When using the checkout Cart Lines API in the app UI, if the coupon is for an order, the displayed product price is calculated by evenly deducting the coupon price from each product. Furthermore, the API’s discountAllocations is empty, and there’s no price before the discount. This causes calculation errors when developing shipping insurance plugins (obtained by subtracting the shipping insurance product price from the totalAmount and the shipping insurance rate). Because the shipping insurance product price is the discounted price after the order coupon, the calculation is incorrect, and the wrong shipping insurance price is subtracted from the total price.
Additional info
We hope this issue can be resolved as soon as possible, as it is crucial for displaying the correct insurance price to users on the checkout page.
What type of topic is this
Troubleshooting
Upload screenshot(s) of issue
Hi there,
This is actually expected behavior, not a bug. When a coupon is applied at the order level, Shopify distributes it internally but discountAllocations on the Cart Lines API stays empty until the order is actually created. So chasing it per-line during checkout won’t work reliably.
For your insurance calculation, just work off the cart totals instead:
const total = cart.cost.totalAmount.amount;
const insuranceLine = cart.lines.find(line =>
line.merchandise.product.handle === ‘shipping-insurance’
);
const insurancePrice = insuranceLine
? parseFloat(insuranceLine.cost.totalAmount.amount)
: 0;
const insurableAmount = parseFloat(total) - insurancePrice;
Already discounted total, minus your insurance line, no need to touch discountAllocations at all.
Let me know if you hit anything else.
Are you referring to the checkout UI or the Cart Transform Function? I’m referring to the checkout UI extension.cart.cost is not present in the Cart Transform Function. If it’s in the Checkout UI Extension, then it’s incorrect. The Cart Transform uses the original price, while the Checkout UI Extension uses the discounted price.
Yes that’s exactly the point, in the Checkout UI Extension cart.cost.totalAmount already reflects the discounted price, which is what you want for the insurance calculation. You subtract the insurance line from that total and you get the correct insurable amount without touching discountAllocations at all.
The Cart Transform Function is a different context and works differently, but for this use case the Checkout UI Extension approach should give you the right numbers.
However, the total discounted price displayed in cart.cost.totalAmount of the Checkout UI Extension is the price after deducting a portion of the discount for the shipping insurance product, not the actual price, as shown in the screenshot of console.log. Therefore, it’s necessary to know discountAllocations.
For example: a product’s price is 1699, with a discount of 12 yuan, and the shipping insurance price is 50.97. However, the shipping insurance price read in the Checkout UI Extension is 50.63. The total price of all protected products is 1749.34 - 50.63 = 1699.34, which doesn’t match my original price.
This means the price calculated when selecting whether to use shipping insurance is based on a different calculation method, as shown in the image.
The discount gets distributed proportionally across all lines including the insurance product, so that’s why the numbers don’t add up. You can try using the original price instead:
const insuranceLine = cart.lines.find(line =>
line.merchandise.product.handle === 'shipping-insurance'
);
const insuranceOriginalPrice = insuranceLine
? parseFloat(insuranceLine.cost.compareAtAmountPerQuantity?.amount
?? insuranceLine.cost.amountPerQuantity.amount)
: 0;
const insurableAmount = parseFloat(cart.cost.totalAmount.amount) - insuranceOriginalPrice;
compareAtAmountPerQuantity should hold the pre-discount price. Let me know if that gives you the correct numbers.
There is no such thing as compareAtAmountPerQuantity
You’re right, that field isn’t available in the Checkout UI Extension context. In that case the only reliable way to get the pre-discount insurance price would be to store it as a cart attribute or line item property when the insurance product is first added to the cart, before any discounts are applied. That way you always have the original price to reference regardless of what happens with order-level discounts.
Yes, I hope the official response will come as soon as possible.Yes, I hope the official response will be as soon as possible. I will notify you immediately if the problem is resolved.
1 Like