questions

Replace zero cost to “Free” from WooCommerce shipping method full label

Rate this post

To display the shipping cost when it’s equal to zero I am using the following code (because woocommerce hide zero cost for shipping methods):

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_add_zero_cost_to_shipping_label', 10, 2 );
   
function custom_add_zero_cost_to_shipping_label($label, $method) {
    // if shipping rate is 0, concatenate ": $0.00" to the label
    if ( ! ( $method->cost > 0 ) ) {
        $label .= ': ' . wc_price(0);
    } 
 
    // return original or edited shipping label
    return $label;
}

How can I change this code to display “Free” instead of displaying $0,00 (zero cost)?

Is this possible with a tweak on the below code?

 

✔️Solution:

Just use the following that will append “Free” to shipping methods labels when if the related shipping method has zero cost (except for free shipping):

add_filter( 'woocommerce_cart_shipping_method_full_label', 'Add_free_to_shipping_label_for_zero_cost', 10, 2 );
function Add_free_to_shipping_label_for_zero_cost( $label, $method ) {
    // If shipping method cost is 0 or null, display 'Free' (except for free shipping method)
    if ( ! ( $method->cost > 0 ) && $method->method_id !== 'free_shipping' ) {
        $label .= ': ' . __('Free');
    }
    return $label;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Leave a Reply

Your email address will not be published.

Back to top button