For discussing the development and usage of Checkout UI extensions, post-purchase extensions, web pixels, Customer Accounts UI extensions, and POS UI extensions
Hi,
I'm working on a Checkout UI Extension to automatically add a product to cart when a Buy X Get Y discount code is added. I am using useDiscountCodes (doc here) to read the applied codes. The issue that I have the error "discount code isn’t valid for the items in your cart", because the discount cannot be applied to the current checkout and my
import {
Banner,
useApi,
useTranslate,
reactExtension,
useDiscountCodes
} from '@shopify/ui-extensions-react/checkout';
import { useEffect } from 'react';
export default reactExtension(
'purchase.checkout.block.render',
() => <Extension />,
);
function Extension() {
const translate = useTranslate();
const { extension } = useApi();
const discountCodes = useDiscountCodes();
useEffect(() => {
if(discountCodes) {
console.log('Discount codes:', discountCodes);
}
}, [discountCodes]);
return (
<Banner title="checkout-ui">
{translate('welcome', {target: extension.target})}
</Banner>
);
}
Discount codes: Array(0)
Is it possible to read the discount code input? Or am I doing something wrong?
Thank you,
Quentin
Solved! Go to the solution
This is an accepted solution.
What you could do:
The automatic discount then does the work, SUMMERBOGO is only the trigger.
Are there any specific rule created for this discount on the admin that would block it from being added?
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hi @Liam ,
Not specially, I am not allowed to add the discount because the gift product ("Y" product in "Buy X Get Y") is not added to cart yet. But I need to read the discount code to add the product so I'm blocked 😵 Is there any way to intercept the not applied discount code in a checkout UI extension? Or maybe a best way to automatically add a product when the Buy X Get Y discount is entered? Using a Shopify Function or something?
Thank you so much!
This is an accepted solution.
What you could do:
The automatic discount then does the work, SUMMERBOGO is only the trigger.
It is exactly the solution I'm working on @Stephen2020 😉 I'm also improving the UX a bit by adding an error message and by removing the discount code when not applicable. To avoid having a product full-price in the cart in case the BXGY discount cannot be applied.
I assume it is the only solution for now!
Thank you very much!
Is this something you're working on for the App Store? Or is this a personal project where you wouldn't mind sharing the code? I'm trying to accomplish this very thing.