Created
August 20, 2021 06:16
-
-
Save warnakey/76cf246f117cd9b166b64b8795313872 to your computer and use it in GitHub Desktop.
Code for WooCommerce site to disable UPS shipping options if shipping to a PO Box (because UPS doesn't ship to PO Boxes)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- You can add this to your footer.php file --> | |
<!-- UPS does'nt ship to PO Boxes, so if your site has multiple carriers (ex. USPS, FedEx & UPS) you would disable UPS for PO boxes --> | |
<!-- ** REPLACE ** the ID below to be the one for your WooCommerce checkout page !!!!!! --> | |
<?php if($post->ID == 2309) { // only works on checkout page ?> | |
<script> | |
// find the billing and shipping email fields to use for event listeners | |
// This code will fire when the customer clicks off of either email field | |
var billingemailfield = document.getElementById('billing_email'); | |
var shippingemailfield = document.getElementById('shipping_email'); | |
// after they are done typing in their billing email | |
billingemailfield.addEventListener('blur', (event) => { | |
// get the address fields from the WooCommerce checkout page | |
var getbillingaddressone = document.getElementById('billing_address_1'); | |
var getbillingaddresstwo = document.getElementById('billing_address_2'); | |
var getshippingaddressone = document.getElementById('shipping_address_1'); | |
var getshippingaddresstwo = document.getElementById('shipping_address_2'); | |
// get the values from those fields | |
var baovalue = getbillingaddressone.value; | |
var batvalue = getbillingaddresstwo.value; | |
var saovalue = getshippingaddressone.value; | |
var satvalue = getshippingaddresstwo.value; | |
// Make the addresses lower case to make checking for PO box easier | |
var lowerbao = baovalue.toLowerCase(); | |
var lowerbat = batvalue.toLowerCase(); | |
var lowersao = saovalue.toLowerCase(); | |
var lowersat = satvalue.toLowerCase(); | |
// strip out any periods from the address to make checking for PO box easier | |
var poboxcheckbao = lowerbao.replace(/\./g,''); | |
var poboxcheckbat = lowerbat.replace(/\./g,''); | |
var poboxchecksao = lowersao.replace(/\./g,''); | |
var poboxchecksat = lowersat.replace(/\./g,''); | |
// combine the addresses to make checking easier (in case they put po box on the 2nd address line instead of 1st) | |
var poboxcheckbilling = poboxcheckbao.concat(' ', poboxcheckbat); | |
var poboxcheckshipping = poboxchecksao.concat(' ', poboxchecksat); | |
// text we will check for in the address fields to see if they are shipping to a PO box | |
const poboxstringone = "po box"; | |
const poboxstringtwo = "pobox"; | |
const poboxstringthree = "post office box"; | |
// check if it is true that the billing or shipping address contain po box. Returns true or false | |
var doesbillingcontainpoboxstringone = poboxcheckbilling.includes(poboxstringone); | |
var doesbillingcontainpoboxstringtwo = poboxcheckbilling.includes(poboxstringtwo); | |
var doesbillingcontainpoboxstringthree = poboxcheckbilling.includes(poboxstringthree); | |
var doesshippingcontainpoboxstringone = poboxcheckshipping.includes(poboxstringone); | |
var doesshippingcontainpoboxstringtwo = poboxcheckshipping.includes(poboxstringtwo); | |
var doesshippingcontainpoboxstringthree = poboxcheckshipping.includes(poboxstringthree); | |
// if the shipping address was not typed in, only check the billing address values for PO Box | |
if (poboxcheckshipping.length == 1) { | |
// check if they typed in po box in the billing address | |
if (doesbillingcontainpoboxstringone == true || doesbillingcontainpoboxstringtwo == true || doesbillingcontainpoboxstringthree == true) { | |
// ** REPLACE ** the IDs below with the ones you want to disable on your WooCommerce checkout !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
// Notice the 'parentElement' below, that's because WooCommerce does something like this | |
// so you can't just target the <li> itself | |
// <ul> | |
// <li> | |
// <input type="radio" name="shipping_method[0]" id="shipping_method_0" value="method0"> | |
// <label for="shipping_method_0">Free US Shipping (UPS)</label> | |
// </li> | |
// </ul> | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
// This jQuery listens for when the checkout has been updated and hides the UPS options | |
jQuery(document.body).on('updated_checkout', function(){ | |
// ** REPLACE ** the IDs below with the ones you want to disable (like above) !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
}); | |
} | |
} | |
// if the shipping address was typed in (not just the billing), only check the shipping address values for PO Box | |
if (poboxcheckshipping.length > 1) { | |
// check if they typed in po box in the shipping address | |
if (doesshippingcontainpoboxstringone == true || doesshippingcontainpoboxstringtwo == true || doesshippingcontainpoboxstringthree == true) { | |
// ** REPLACE ** the IDs below with the ones you want to disable on your WooCommerce checkout !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
// This jQuery listens for when the checkout has been updated and hides the UPS options | |
jQuery(document.body).on('updated_checkout', function(){ | |
// ** REPLACE ** the IDs below with the ones you want to disable (like above) !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
}); | |
} | |
} | |
}); | |
// after they are done typing in their shipping email (in case they do this as well) | |
shippingemailfield.addEventListener('blur', (event) => { | |
var getbillingaddressone = document.getElementById('billing_address_1'); | |
var getbillingaddresstwo = document.getElementById('billing_address_2'); | |
var getshippingaddressone = document.getElementById('shipping_address_1'); | |
var getshippingaddresstwo = document.getElementById('shipping_address_2'); | |
var baovalue = getbillingaddressone.value; | |
var batvalue = getbillingaddresstwo.value; | |
var saovalue = getshippingaddressone.value; | |
var satvalue = getshippingaddresstwo.value; | |
/* Make the addresses lower case */ | |
var lowerbao = baovalue.toLowerCase(); | |
var lowerbat = batvalue.toLowerCase(); | |
var lowersao = saovalue.toLowerCase(); | |
var lowersat = satvalue.toLowerCase(); | |
// strip out the periods from the address to make checking for PO box easier | |
var poboxcheckbao = lowerbao.replace(/\./g,''); | |
var poboxcheckbat = lowerbat.replace(/\./g,''); | |
var poboxchecksao = lowersao.replace(/\./g,''); | |
var poboxchecksat = lowersat.replace(/\./g,''); | |
// combine the addresses to make checking easier | |
var poboxcheckbilling = poboxcheckbao.concat(' ', poboxcheckbat); | |
var poboxcheckshipping = poboxchecksao.concat(' ', poboxchecksat); | |
// things we will check for | |
const poboxstringone = "po box"; | |
const poboxstringtwo = "pobox"; | |
const poboxstringthree = "post office box"; | |
// check if it is true that the billing or shipping address contain po box | |
var doesbillingcontainpoboxstringone = poboxcheckbilling.includes(poboxstringone); | |
var doesbillingcontainpoboxstringtwo = poboxcheckbilling.includes(poboxstringtwo); | |
var doesbillingcontainpoboxstringthree = poboxcheckbilling.includes(poboxstringthree); | |
var doesshippingcontainpoboxstringone = poboxcheckshipping.includes(poboxstringone); | |
var doesshippingcontainpoboxstringtwo = poboxcheckshipping.includes(poboxstringtwo); | |
var doesshippingcontainpoboxstringthree = poboxcheckshipping.includes(poboxstringthree); | |
// if the shipping address was not typed in, only check the billing address values for PO Box | |
if (poboxcheckshipping.length == 1) { | |
// check if they typed in po box in the billing address | |
if (doesbillingcontainpoboxstringone == true || doesbillingcontainpoboxstringtwo == true || doesbillingcontainpoboxstringthree == true) { | |
// ** REPLACE ** the IDs below with the ones you want to disable on your WooCommerce checkout !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
// This jQuery listens for when the checkout has been updated and hides the UPS options | |
jQuery(document.body).on('updated_checkout', function(){ | |
// ** REPLACE ** the IDs below with the ones you want to disable (like above) !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
}); | |
} | |
} | |
// if the shipping address was also typed in, only check the shipping address values for PO Box | |
if (poboxcheckshipping.length > 1) { | |
// check if they typed in po box in the shipping address | |
if (doesshippingcontainpoboxstringone == true || doesshippingcontainpoboxstringtwo == true || doesshippingcontainpoboxstringthree == true) { | |
// ** REPLACE ** the IDs below with the ones you want to disable on your WooCommerce checkout !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
// This jQuery listens for when the checkout has been updated and hides the UPS options | |
jQuery(document.body).on('updated_checkout', function(){ | |
// ** REPLACE ** the IDs below with the ones you want to disable (like above) !!!!! | |
// check that the shipping elements exist before hiding them to prevent errors | |
var shipping_method_0ExistsCheck = document.getElementById("shipping_method_0"); | |
if (shipping_method_0_ExistsCheck) { | |
document.getElementById("shipping_method_0").parentElement.style.display = "none"; | |
} | |
var shipping_method_1ExistsCheck = document.getElementById("shipping_method_1"); | |
if (shipping_method_1ExistsCheck) { | |
document.getElementById("shipping_method_1").parentElement.style.display = "none"; | |
} | |
}); | |
} | |
} | |
}); | |
</script> | |
<?php } ?> | |
<!-- END OF function to Disable UPS options if PO Box is entered --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment