Hello ,
I am working on one addion in checkout using checkout extention . where if customer enter PO box in shipping address during checkout, system show show error and not allow to proceed. here is my code, but its not working. any help wil be apriciated
import { extension, Banner } from "@shopify/ui-extensions/checkout";
export default extension("Checkout::Dynamic::Render", (root, api) => {
const { checkout, i18n } = api;
// Function to validate the shipping address
function validateAddress(checkoutState) {
const shippingAddress = checkoutState.shippingAddress || {};
const addressLines = [shippingAddress.address1, shippingAddress.address2];
const poBoxPattern = /\b[P|p][. ]?[O|o][. ]?[B|b][O|o][X|x]\b/;
for (let line of addressLines) {
if (line && poBoxPattern.test(line)) {
displayErrorMessage();
break;
}
}
}
// Function to display an error message
function displayErrorMessage() {
const banner = root.createComponent(Banner, {
status: 'critical',
title: i18n.translate('errors.poBoxNotAllowed'),
}, i18n.translate('errors.poBoxNotAllowedMessage'));
root.appendChild(banner);
root.mount(); // Ensure to mount the root again to reflect the changes
}
// Listen for checkout updates
checkout.on('change', (newCheckoutState) => {
validateAddress(newCheckoutState);
});
// Initial validation in case the extension loads after an address is already entered
validateAddress(checkout.getState());
});