less than a minute read • Updated 9 minutes ago
Prevent shipping to PO boxes
How to stop customers from completing checkout with a PO Box shipping address.
The following functionality prevents your customers from shipping to PO Box addresses.
Add Javascript
In the Foxy admin (https://admin.foxy.io), go to Settings > Templates and add the code to the Custom footer field (Twig is supported; it is injected before the closing </body> tag).
javascript
{% if context == "checkout" %}
<script>
var validatePO = function() {
var addressString = "";
$("[name$='_address1'],[name$='_address2']").not("[name^='billing']").each(function() { addressString += this.value; });
addressString = addressString.replace(/\./g,"");
addressString = addressString.replace(/ /g,"");
addressString = addressString.toLowerCase();
if(addressString.indexOf('pobox') != -1) {
alert("We can not ship to a PO Box shipping address. Please choose a different shipping address location.");
return false;
}
else {
return true;
}
};
FC.client.on('customer-login.done', validatePO);
FC.client.on('customer-address-change', validatePO);
FC.client.on('checkout-submit', validatePO);
$("body").on("change", "[name$='_address1'],[name$='_address2']", function() {
if (this.name.indexOf("billing")) {
var address = FC.json.shipping_address;
var multiship = this.name.match(/shipto_(\d+)/);
if (multiship) {
address = FC.json.multiship_data[multiship[1]];
}
if (validatePO() && FC.util.addressHasLocationInfo(address)) {
FC.client.event('customer-address-change.done').trigger({ "fields":{}, "address": address });
}
}
});
</script>
{% endif %}