Created
March 9, 2017 11:49
-
-
Save dangoodman/e0a4073537b13841c11aa82107e2adf3 to your computer and use it in GitHub Desktop.
Woocommerce: Hide specified shipping options when shipping to PO boxes
This file contains 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
<?php | |
// Paste everything below this line to your theme's functions.php file. | |
add_filter('woocommerce_package_rates', function($rates, $package) { | |
$destination = $package['destination']; | |
// Put postcode, address 1 and address 2 into an array | |
$check_address = array( | |
$destination['address'], | |
$destination['address_2'], | |
$destination['postcode'], | |
); | |
// Implode address, make lowercase, and remove spaces and full stops | |
$check_address = strtolower(str_replace(array(' ', '.'), '', implode('-', $check_address))); | |
// Check for po boxes in the combined address string | |
if (strstr($check_address, 'pobox')) { | |
// Hide shipping options not suitable for po boxes | |
foreach ($rates as $rate_id => $rate) { | |
if (preg_match('/fastway|couriers please safe drop|couriers please no safe drop/i', $rate->label)) { | |
unset($rates[$rate_id]); | |
} | |
} | |
} | |
return $rates; | |
}, 100, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment