WooCommerce custom function for Free Shipping Label

When i put the following into to my functions.php it crashes my entire site.
the purpose of this function is to change

elseif ( $method->id !== 'free_shipping' ) {
    $label .= ' (' . __( '**Free**', 'woocommerce' ) . ')';

to this…

Read More
elseif ( $method->id !== 'free_shipping' ) {
    $label .= ' (' . __( '**To Be Calculated**', 'woocommerce' ) . ')';

When i change the one word in the original woocommerce/includes/wc-cart-functions.php it works perfectly. I don’t want it to be overwritten with an update.

/**
* Get a shipping methods full label including price
* @param  object $method
* @return string
*/
function wc_cart_totals_shipping_method_label( $method ) {
$label = $method->label;

if ( $method->cost > 0 ) {
    if ( WC()->cart->tax_display_cart == 'excl' ) {
        $label .= ': ' . wc_price( $method->cost );
        if ( $method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax ) {
            $label .= ' <small>' . WC()->countries->ex_tax_or_vat() . '</small>';
        }
    } else {
        $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
        if ( $method->get_shipping_tax() > 0 && ! WC()->cart->prices_include_tax ) {
            $label .= ' <small>' . WC()->countries->inc_tax_or_vat() . '</small>';
        }
    }
} elseif ( $method->id !== 'free_shipping' ) {
    $label .= ' (' . __( 'To Be Calculated', 'woocommerce' ) . ')';
}

return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}

Related posts

Leave a Reply

2 comments

  1. if you are using latest woo commerce then following filter will be helpful to you.

    add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_local_pickup_free_label', 10, 2 );
    function remove_local_pickup_free_label($full_label, $method){
        $full_label = str_replace("(Free)","(TBD)",$full_label);
    return $full_label;
    }
    
  2. You should rebuild the function through the filter. Using str_replace will not work on translated installs, unless each language is checked in the replacement.

    function ua_woocommerce_cart_shipping_method_full_label( $label, $method ) {
    $label = $method->label;
    
    if ( $method->cost > 0 ) {
        if ( WC()->cart->tax_display_cart == 'excl' ) {
            $label .= ': ' . wc_price( $method->cost );
            if ( $method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax ) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
            if ( $method->get_shipping_tax() > 0 && ! WC()->cart->prices_include_tax ) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        }
    } 
    
    return $label;
    }
    add_filter( 'woocommerce_cart_shipping_method_full_label', 'ua_woocommerce_cart_shipping_method_full_label', 10, 2 );