Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active July 13, 2020 15:26
Show Gist options
  • Save xadapter/39e61a4c160ca5dfce07a9e9df39b162 to your computer and use it in GitHub Desktop.
Save xadapter/39e61a4c160ca5dfce07a9e9df39b162 to your computer and use it in GitHub Desktop.
Snippet to hide WooCommerce shipping methods on the cart/checkout page based on the destination country. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_country', 10, 2);
function wf_remove_shipping_options_for_particular_country($available_shipping_methods, $package){
global $woocommerce;
//Config this array with country code and corresponding shipping methods to hide.
$country_list = array(
'CA' => array('wf_fedex_woocommerce_shipping','free_shipping'),
);
$customer_country = $woocommerce->customer->get_shipping_country();
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