Created
October 22, 2016 16:09
Star
You must be signed in to star a gist
Hide shipping method while shipping class available in the package (cart)
This file contains 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', 'hide_shipping_method_when_shipping_class_product_is_in_cart', 10, 2); | |
function hide_shipping_method_when_shipping_class_product_is_in_cart($available_shipping_methods, $package) | |
{ | |
// Shipping class Slugs that need the method removed | |
$shipping_classes = array( | |
'perishable', | |
); | |
$shipping_services_to_hide = array( | |
'wf_shipping_ups:14', | |
'wf_shipping_ups:01', | |
'wf_shipping_ups:59', | |
'wf_shipping_ups:03', | |
'wf_shipping_ups:02', | |
'wf_shipping_ups:12', | |
); | |
$shipping_class_exists = false; | |
foreach($package['contents'] as $key => $values) { | |
if (in_array($values['data']->get_shipping_class() , $shipping_classes)) { | |
$shipping_class_exists = true; | |
break; | |
} | |
} | |
if ($shipping_class_exists) { | |
foreach($shipping_services_to_hide as & $value) { | |
unset($available_shipping_methods[$value]); | |
} | |
} | |
return $available_shipping_methods; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment