Skip to content

Instantly share code, notes, and snippets.

@rwkyyy
Forked from xadapter/functoin.php
Last active July 14, 2020 08:55
Show Gist options
  • Save rwkyyy/e5bf97cdbda9164165adc1f451aa0660 to your computer and use it in GitHub Desktop.
Save rwkyyy/e5bf97cdbda9164165adc1f451aa0660 to your computer and use it in GitHub Desktop.
remove shipping methods based on city
// tweak for city shipping options
add_filter('woocommerce_package_rates', 'restrict_shipping_options_based_on_city', 10, 2);
function restrict_shipping_options_based_on_city($available_shipping_methods, $package){
global $woocommerce;
// Config this array with city names and corresponding shipping methods to hide.
$country_list = array(
'Cefa' => array('flat_rate','free_shipping'),
);
//get shipping city (or anything else you need to filter)
$customer_country = $woocommerce->customer->get_shipping_city();
//do the checks
if ( in_array( $customer_country , array_keys($country_list) ) ) {
if( !empty( $country_list[$customer_country] ) ){
foreach ($country_list[$customer_country] as $shipping_methods) {
foreach ($available_shipping_methods as $shipping_method => $value) {
if( strpos( $shipping_method, $shipping_methods ) !== false ) {
unset($available_shipping_methods[$shipping_method]);
}
}
}
}
}
return $available_shipping_methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment