Hi, I built a code example for this. The following code will limit the allowed rates based on the shipping classes in the cart. This is quite simple logic where if any of the classes are found then only the allowed rates will be shown. Hopefully you can customise this to match your requirements.
NB! it’s untested so use with caution.
add_filter('bring_shipping_rates', function( $rates ) {
// Rates to allow when matching one of the shipping classes.
$limited_rates = [
'mail'
];
// Shipping classes to limit rates for.
$shipping_classes = [
'shipping_class_1',
'shipping_class_2',
'shipping_class_3',
'etc...',
];
// Do not limit rates unless criteria is met.
$limit_rates = false;
// Look for items with a shipping class that doesn not allow mail.
$items = WC()->cart->get_cart();
foreach ( $items as $item ) {
$shipping_class = $item['data']->get_shipping_class();
if ( in_array( $shipping_class, $shipping_classes ) ) {
$limit_rates = true;
break;
}
}
if ( ! $limit_rates ) {
return $rates;
}
foreach ( $rates as $key => $rate ) {
if ( in_array( $rate['bring_product'], $limited_rates ) ) {
// Allow specified rates.
continue;
}
// Remove other rates.
unset( $rates[ $key ] );
break;
}
return $rates;
}, 999 );