Skip to content

Instantly share code, notes, and snippets.

@Nishadup
Created September 29, 2017 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Nishadup/09884f1105abda3a9b1e112abd355f4d to your computer and use it in GitHub Desktop.
Save Nishadup/09884f1105abda3a9b1e112abd355f4d to your computer and use it in GitHub Desktop.
Restrict the shipping methods based on state and zip code
add_filter('woocommerce_package_rates', 'wf_restrict_shipping_methods_based_on_state_and_zipcode', 10, 2);
function wf_restrict_shipping_methods_based_on_state_and_zipcode($available_shipping_methods, $package){
$destination = $package['destination'];
$dest_postcode = $destination['postcode'];
$dest_state = $destination['state'];
//Config this array with state code, zip code, and services you have to show.
$restrict_methods = array(
'CA'=> array(
//Show only UPS Ground for California have zip code start with 910
'910' => array(
'wf_shipping_ups:03',
),
//Show only UPS Ground and 3 Day Select for California have zip code start with 912
'912' => array(
'wf_shipping_ups:03',
'wf_shipping_ups:12',
),
),
// Show only Ground and 3 Day Select for everywhere in New York
'NY'=> array(
'*' => array(
'wf_shipping_ups:03',
'wf_shipping_ups:12',
),
),
);
foreach ($available_shipping_methods as $current_service => $value) {
if( !empty($restrict_methods[$dest_state]) ){
foreach ($restrict_methods[$dest_state] as $postcode => $services_arr) {
if( ( $postcode == '*' || strstr( (string)$dest_postcode, (string)$postcode ) ) && in_array($current_service, $services_arr) ){
unset($available_shipping_methods[$current_service]);
}
}
}
}
return $available_shipping_methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment