I’m trying to use the useDiscountAllocations React Hook. I expect to receive an array of the discounts that have been added to the cart in the checkout, but I’m receiving an empty array:
import {
useDiscountAllocations,
} from "@shopify/checkout-ui-extensions-react";
const discountAllocations = useDiscountAllocations();
The “discountAllocations” variable is an empty array, but the checkout has a discount code added:
I could get it to do what I need by changing the hook to “useDiscountCodes”. This way I get an array of the discount codes that have been applied to the checkout.
I still don’t understand what discount allocations are.
This is returning an empty array for me as well. A fairly simple workaround for anyone struggling with this broken hook is below:
Loop each of your line-items (useCartLines), calculate the total of each line-item applied discounts (cartLine.discountAllocations), that’s your total applied discounts.