Skip to content

Instantly share code, notes, and snippets.

@xadapter
Forked from Nishadup/function.php
Last active March 12, 2020 10:12
Show Gist options
  • Save xadapter/e3c4c535d69de9797ba72cff75c2b473 to your computer and use it in GitHub Desktop.
Save xadapter/e3c4c535d69de9797ba72cff75c2b473 to your computer and use it in GitHub Desktop.
Snippet to hide WooCommerce shipping methods if either Free shipping or Flat rate is present in the cart. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
add_filter('woocommerce_package_rates', 'xa_hide_shipping_methods_if_free_flaterate_exist', 10, 2);
if(!function_exists('xa_hide_shipping_methods_if_free_flaterate_exist')){
function xa_hide_shipping_methods_if_free_flaterate_exist( $available_shipping_methods, $package ){
$hide_if_shpping_method_exist = array('free_shipping','flat_rate'); //If the shipping methods given here is exists.
$method_to_hide = array('wf_australia_post'); //Hide all the shipping method provided here.
$do_hide = false;
foreach ($hide_if_shpping_method_exist as $method) {
foreach ($available_shipping_methods as $shipping_method => $value) {
if( strpos( $shipping_method, $method ) !== false ) {
$do_hide=true;
break 2;
}
}
}
if( $do_hide ){
foreach ($method_to_hide as $method) {
xa_hide_the_shipping_method( $method, $available_shipping_methods );
}
}
return $available_shipping_methods;
}
}
if(!function_exists('xa_hide_the_shipping_method')){
function xa_hide_the_shipping_method($method, &$available_shipping_methods){
foreach ($available_shipping_methods as $shipping_method => $value) {
if( strpos( $shipping_method, $method ) !== 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