Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: useDiscountAllocations React Hook is not working

Solved

useDiscountAllocations React Hook is not working

joelurraco
Shopify Partner
19 0 14

Hello,

 

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:

 

 

Captura de pantalla de 2023-07-11 11-27-15.png

 

I have a simple component that renders the value {discountAllocations.length}, and it always renders 0.

 

Any help will be appreciated.

Accepted Solution (1)

anxiouspenguin
Shopify Partner
10 2 1

This is an accepted solution.

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.

 

https://shopify.dev/docs/api/checkout-ui-extensions/2023-07/react-hooks/cart/usecartlines

 

Screenshot 2024-04-15 at 2.02.08 PM.png

View solution in original post

Replies 5 (5)

lizk
Shopify Staff
246 58 79

Hi there 👋

I was not able to reproduce this issue. The code you have provided looks correct.

If you would like me to look into this further could you provide the code for your extension in the entirety. 

To learn more visit the Shopify Help Center or the Community Blog.

joelurraco
Shopify Partner
19 0 14

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.

 

dominicgonzaga3
Shopify Partner
1 0 0

This issue is still persistent useDiscountCodes does return a non-empty but  useDiscountAllocations is always empty regardless

Hugh_trt
Shopify Partner
6 0 2

I met the same issue, got an empty array by using 

useDiscountAllocations()

anxiouspenguin
Shopify Partner
10 2 1

This is an accepted solution.

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.

 

https://shopify.dev/docs/api/checkout-ui-extensions/2023-07/react-hooks/cart/usecartlines

 

Screenshot 2024-04-15 at 2.02.08 PM.png