Skip to content

Instantly share code, notes, and snippets.

@WooForce

WooForce/functions.php

Last active Mar 31, 2016
Embed
What would you like to do?
WooCommerce: Hide the undesired method for the particular existing shipping class
add_filter('woocommerce_package_rates', 'hide_auspost_method_when_shipping_class_product_is_in_cart', 10, 2);
function hide_auspost_method_when_shipping_class_product_is_in_cart($available_shipping_methods, $package)
{
$shipping_class_ids = array(
16
);
$shipping_services_to_hide = array(
'wf_australia_post:AUS_PARCEL_REGULAR',
'wf_australia_post:AUS_PARCEL_EXPRESS',
'wf_australia_post:AUS_PARCEL_COURIER',
'wf_australia_post:INTL_SERVICE_EPI',
'wf_australia_post:INTL_SERVICE_ECI_PLATINUM',
'wf_australia_post:INTL_SERVICE_ECI_M',
'wf_australia_post:INTL_SERVICE_RPI',
'wf_australia_post:INTL_SERVICE_PTI',
'wf_australia_post:INTL_SERVICE_AIR_MAIL',
'wf_australia_post:INTL_SERVICE_SEA_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