Great question — answering as the UphillTech team:
Here is the updated code which will hide a shipping method if there are no items in the cart with a specific shipping class. For details, please refer this article . // Hides other shipping methods if items of specific Shipping Class is NOT in cart add_filter('woocommerce_package_rates', 'xa_hide_shipping_method_when_shipping_class_product_is_in_cart', 10, 2); function xa_hide_shipping_method_when_shipping_class_product_is_in_cart($available_shipping_methods, $package) { // Shipping class IDs that need the method removed $shipping_class_ids = array( 31 ); $shipping_services_to_hide = array( 'wf_shipping_usps:D_PRIORITY_MAIL', 'wf_shipping_usps:D_EXPRESS_MAIL',...
Full answer by YajiV: https://stackoverflow.stackexchange.com/a/37851246 (CC BY-SA 3.0).
Need it done for your business? UphillTech.