Skip to content

Instantly share code, notes, and snippets.

@WooForce
Created October 22, 2016 16:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save WooForce/1169b93ca74d8ea636b73c46e58b7b0b to your computer and use it in GitHub Desktop.
Hide shipping method while shipping class available in the package (cart)
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