For discussing the development and usage of Checkout UI extensions, post-purchase extensions, web pixels, Customer Accounts UI extensions, and POS UI extensions
I want to override the existing error message on checkout extensibility. I have written code to override existing error message but it is working only when the email is invalid. It is not working when email is blank.
My code :
import { reactExtension, useBuyerJourneyIntercept, useEmail } from '@shopify/ui-extensions-react/checkout'; export default reactExtension( 'purchase.checkout.block.render', () => <Extension />, ); function validEmailAddress(email) { return String(email) .toLowerCase() .match( /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); } function Extension() { const email = useEmail(); useBuyerJourneyIntercept(({ canBlockProgress }) => { console.log("email1:",email); if (canBlockProgress && (!email || email.trim() === '')) { console.log("email2:",email); return { behavior: "block", reason: "Empty email address", errors: [ { message: "A valid email address is required to proceed", target: "$.cart.buyerIdentity.email", } ], }; } if (canBlockProgress && !validEmailAddress(email)) { return { behavior: "block", reason: "Invalid email address", errors: [ { message: "A valid email address is required to proceed", target: "$.cart.buyerIdentity.email", } ], }; } return { behavior: "allow", }; }); return null; }
Hi Sachinaghera,
It seems like your code is working as expected in terms of validating the email for its format using the validEmailAddress
function. The issue appears to be when the email field is left blank.
The useEmail
hook might not be updating the email
state when the email field is empty. This could be due to how Shopify handles the email field in the checkout step (i.e., it might not trigger an update for blank fields).
Here are some debugging steps you can try:
email
state: This can help determine if the state changes when the email field is left blank. Try typing in the email field and then deleting everything to see if the state updates.Hope this helps!
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
Yes it's state is changing and also to handle it I use undefied error message.
Hi Liam,
I have made my checkout extension app using javascript, how can I use this hook useEmail as it is a react hook only ?