Re: checkout extensibility error message

checkout extensibility error message

sachinaghera
Shopify Partner
7 0 2

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.

 

sachinaghera_0-1697778336127.png

 

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;
}




Replies 4 (4)

sachinaghera
Shopify Partner
7 0 2

@Liam can you please help me with this?

Liam
Community Manager
3108 340 872

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:

  • Console log the 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.
  • Update to the latest version of Shopify UI extensions: If you're not using the latest version of the UI extensions, updating might resolve the issue. 

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

sachinaghera
Shopify Partner
7 0 2

Yes it's state is changing and also to handle it I use undefied error message.

rkmishra89
Shopify Partner
5 0 0

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 ?