At times, you may desire to modify the display of shipping methods, for example, hiding shipping method ‘B’ when shipping method ‘A’ is available. The following code allows customers to show free express delivery and hide the paid express delivery option, only when the free shipping option is available. If the free express delivery is unavailable, the alternative shipping method will be displayed.
add_filter( 'woocommerce_package_rates', 'ts_customizing_shipping_methods', 10, 2 ); function ts_customizing_shipping_methods( $rates, $package ) { $flat_express_delivery_rate_id = 'flat_rate:4'; // Paid delivery by express mail (cost $35) $free_express_delivery_rate_id = 'free_shipping:5'; // Free delivery by express mail // When Free delivery by express mail is available if( isset($rates[$free_express_delivery_rate_id]) ) { // Remove Paid delivery by express mail (cost $35) unset($rates[$flat_express_delivery_rate_id]); } return $rates; }
Output
Consider that the shipping methods present in the below image are the available shipping options.
If the free express delivery method is present in the $rates
array, it removes (hides) the paid express delivery method.
Now, let’s assume that free shipping is not one of the available shipping options.
Whenever the free shipping option is not available, only then the paid delivery express mail will be shown in the shipping methods.
Here we are hiding selective shipping method when free shipping is available. But you can also refer to this post that will hide all other shipping methods when free shipping is available which covers different scenarios in hiding shipping methods.