Skip to content

Instantly share code, notes, and snippets.

@dangoodman
Created March 9, 2017 11:49
Show Gist options
  • Save dangoodman/e0a4073537b13841c11aa82107e2adf3 to your computer and use it in GitHub Desktop.
Save dangoodman/e0a4073537b13841c11aa82107e2adf3 to your computer and use it in GitHub Desktop.
Woocommerce: Hide specified shipping options when shipping to PO boxes
<?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