Last active
March 31, 2016 12:02
-
-
Save WooForce/3e8e65075e469e1e422d to your computer and use it in GitHub Desktop.
WooCommerce: Hide a shipping method for a particular 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', '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 IDs that need the method removed | |
$shipping_class_ids = array( | |
1111, | |
2232, | |
4235 | |
); | |
$shipping_services_to_hide = array( | |
'wf_shipping_usps:D_PRIORITY_MAIL', | |
'wf_shipping_usps:D_EXPRESS_MAIL', | |
'wf_shipping_usps:D_MEDIA_MAIL' | |
); | |
$shipping_class_exists = false; | |
foreach(WC()->cart->cart_contents as $key => $values) { | |
if (in_array($values['data']->get_shipping_class_id() , $shipping_class_ids)) { | |
$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