Great question — answering as the UphillTech team:
There are some mistakes and your code can be simplified. To disable free shipping for specific shipping classes exclusively , try the following revisited code: add_filter( 'woocommerce_package_rates', 'hide_free_shipping_conditionally', 10, 2 ); function hide_free_shipping_conditionally( $rates, $package ) { // Define the targeted shipping classes slugs (not names) $targeted_classes = array( 'extra' ); $found = $others = false; // Initializing // Loop through cart items for current shipping package foreach( $package['contents'] as $item ) { if ( in_array( $item['data']->get_shipping_class(), $targeted_classes ) ) { $found = true; } else { $others = true; } } // When there are only items...
Full answer by LoicTheAztec: https://stackoverflow.stackexchange.com/a/66996791 (CC BY-SA 4.0).
Need it done for your business? UphillTech.