Hi Everyone,
Hoping someone can help me out. We are trying to move away from checkout.liquid and scripts.
Currently we have a script that prevents checkout when the address is invalid (po-box) by hiding the shipping options as well as only displaying the free shipping option if there is one.
I have created a function that does this and it mostly works the only issue I have is that once all shipping options are hidden then the function stops firing. Has anyone seen this before?
Or does anyone know another way to prevent checkout based on the address that will work with extensibility?
See code below
const PAYLOAD = {
operations: ,
};
export function run(input) {
// Define a type for your configuration, and parse it from the metafield
/**
- @type {{
- stateProvinceCode: string
- message: number
- }}
*/
const configuration = JSON.parse(
input?.deliveryCustomization?.metafield?.value ?? “{}”
);
input.cart.deliveryGroups.forEach(group => {
const eligableForFreeShipping = cartIsEligableForFreeShipping(group.deliveryOptions)
const addressIsValid = validateAddress(group.deliveryAddress)
console.log(addressIsValid = ${addressIsValid} , eligableForFreeShipping = ${eligableForFreeShipping})
group.deliveryOptions.forEach(option => {
if (
!addressIsValid
||
(eligableForFreeShipping && Number(option.cost.amount) !== 0)) {
PAYLOAD.operations.push({
hide: {
deliveryOptionHandle: option.handle
}
})
}
});
})
return PAYLOAD;
};
class AddressSelector {
constructor(triggers) {
this.triggers = triggers.map(trigger => trigger.toLowerCase().trim());
}
match(address) {
const addressFields = [address.address1, address.address2].map(line =>
line ? line.toLowerCase() : “”
).join(" ");
return this.triggers.some(trigger => addressFields.includes(trigger));
}
}
const validateAddress = address => {
let isValid = true
// Add PO Box and HC Box triggers
const poBoxTriggers = [“po box”, /* … other triggers … /];
const hcBoxTriggers = [[“hc”, / … /], [“box”, / … */]];
if (
new AddressSelector(poBoxTriggers).match(address)
||
(new AddressSelector(hcBoxTriggers[0]).match(address) && new AddressSelector(hcBoxTriggers[1]).match(address))
) {
isValid = false
}
console.log(Address validator return:${isValid})
return isValid
}
const cartIsEligableForFreeShipping = deliveryOptions => deliveryOptions.some(o => Number(o.cost.amount) === 0)

