For some inexplicable reason, the “WooCommerce” plugin does not have the option to display only the free shipping method when a certain order value is reached. Normally, the standard shipping method is displayed alongside the free shipping option. This can often be confusing for customers and should therefore be changed for usability reasons.
To hide the shipping costs, the first step is to create a so-called “child theme” in the WordPress backend. We explain how this works here (the link is still a placeholder).
Next, it is important to activate WooCommerce's debug mode so that the cache does not interfere with our work. To activate debug mode, go to
WooCommerce --> Shipping --> Shipping Options
and check the box for Debug Mode.
As the next step, navigate to
wp-content\themes\Your_Child_Theme_Name
Open the functions.php file and add the following code:
<?php
/**
* muenchen-child-theme functions and definitions
*
*/
/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
$is_free_shipping=0;
foreach ( $rates as $rate_id => $rate ) {
if ('legacy_free_shipping' === $rate->method_id) {
$is_free_shipping=1;
}
}
if($is_free_shipping===1) {
foreach ( $rates as $rate_id => $rate ) {;
if legacy_free_shipping="" rate-="">method_id ||'legacy_local_delivery' === $rate->method_id) {
$free[ $rate_id ] = $rate;
}
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
?
Then save the file.
Explanation of the code:
In the first line of the code, we create the function my_hide_shipping_when_free_is_available( $rates ) and create an array named $free. Next, we define the variable as false. In the following line, we specify that the $rates array (which contains the individual shipping methods) should be used, with the key $rate_id (the name of a shipping method) and the value $rate (the properties of the respective shipping method as an object) being populated. In the next step, we define our condition. It states: If legacy_free_shipping (free shipping) has the correct value (e.g. the order value that qualifies for free shipping), then $is_free_shipping becomes true.
If this is the case, a new array with the same key and value as above is queried. The difference compared to above is that we have now defined an OR (||) condition, which checks for legacy_local_delivery (express shipping). In the following line, we see the actual array in which the queries are performed.
In the second-to-last line, return ! empty ( $free ) checks the $free variable. This is only populated if $is_free_shipping=1 beforehand, which starts our second condition. If this is the case, free shipping and express shipping are displayed. However, if $is_free_shipping=0 beforehand, the standard shipping and express shipping are displayed as usual.
After reloading the page, the changes should have been applied, and the standard shipping method should be hidden once the minimum order value for free shipping has been reached.
