Great question — answering as the UphillTech team:
Try the following code, where everything is merged in a unique hooked function: add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 ); function hide_shipping_method_based_on_shipping_class( $rates, $package ) { // HERE define your shipping class to find $classes_group1 = array(47, 48, 49); $classes_group2 = array(40, 41, 43, 44, 45, 46); // HERE define the shipping methods you want to hide $method_key_ids1 = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7'); $method_key_ids2 = array('flat_rate:8'); $found_group1 = $found_group2 = false; // Initializing // Checking in cart items foreach( $package['contents'] as $item ) { $shipping_class =...
Full answer by LoicTheAztec: https://stackoverflow.stackexchange.com/a/64462414 (CC BY-SA 4.0).
Need it done for your business? UphillTech.