Last active
December 1, 2017 05:10
-
-
Save WooForce/d3f58140283cc899be390cc8faf5832b to your computer and use it in GitHub Desktop.
Hide shipping method based on shipping class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2); | |
function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package) | |
{ | |
$hide_when_shipping_class_exist = array( | |
42 => array( | |
'free_shipping' | |
) | |
); | |
$hide_when_shipping_class_not_exist = array( | |
42 => array( | |
'wf_shipping_ups:03', | |
'wf_shipping_ups:02', | |
'wf_shipping_ups:01' | |
), | |
43 => array( | |
'free_shipping' | |
) | |
); | |
$shipping_class_in_cart = array(); | |
foreach(WC()->cart->get_cart_contents() as $key => $values) { | |
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id(); | |
} | |
foreach($hide_when_shipping_class_exist as $class_id => $methods) { | |
if(in_array($class_id, $shipping_class_in_cart)){ | |
foreach($methods as & $current_method) { | |
unset($available_shipping_methods[$current_method]); | |
} | |
} | |
} | |
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) { | |
if(!in_array($class_id, $shipping_class_in_cart)){ | |
foreach($methods as & $current_method) { | |
unset($available_shipping_methods[$current_method]); | |
} | |
} | |
} | |
return $available_shipping_methods; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment