OzPost – How to disable shipping method for a class

Do you have certain products that cannot be shipped via Australia Post due to them being dangerous goods for example? This is the function to be added to your websites child theme function file to exclude any shipping methods.

/*
*** Disable Shipping Method if Cart has Shipping Class – Hiding for WooCommerce Product
*/
add_filter( ‘woocommerce_package_rates’, ‘esolutions_hide_free_shipping_for_shipping_class’, 10, 2 );

function esolutions_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = 373; // shipping class ID – right click in Chrome and Inspect element
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ ‘data’ ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset( $rates[‘ozpost.RPP’] ); // right click in Chrome and Inspect element and add value
unset( $rates[‘ozpost.PPS5’] );
unset( $rates[‘ozpost.EXP’] );
}
return $rates;
}

Remember: If you are not seeing the updates go to WooCommerce Status and Tools to “Clear Customer Sessions” and clear your websites cache.