phpwordpresswoocommercehook-woocommerceshipping-method

Customize Shipping Label from WooCommerce Cart and Checkout Pages


I want to remove the shipping label value in the cart and checkout pages when the value get assigned to shipping. Ex:- Change ' flat rate 100.00$ ' to ' 100.00$ ' . That means i just want the value without label text. Could you please help me to achieve this?

I have changed as $rates[$targeted_rate_id]->label = ''; in the shipping_package_rates_filter_callback function. but it is still showing ' : ' (colon symbol) at the start of the value. Ex:- :100.00$ .

shipping_package_rates_filter_callback

add_filter( 'woocommerce_package_rates', 'shipping_package_rates_filter_callback', 100, 2 );
function shipping_package_rates_filter_callback( $rates, $package ) {
    // HERE define your targeted rate id
    $targeted_rate_id = 'flat_rate:4'; 
    
    if ( ! array_key_exists($targeted_rate_id, $rates) ) 
        return $rates;
    
    $new_cost = WC()->session->get('shipping_cost'); // Get new cost from WC sessions

    if ( ! empty($new_cost) ) {
        // Set shipping label empty
        $rates[$targeted_rate_id]->label = '';
        $rate = $rates[$targeted_rate_id];
        $cost = $rate->cost; // Set the initial cost in a variable for taxes calculations
        $rates[$targeted_rate_id]->cost = $new_cost; // Set the new cost

        $has_taxes = false; // Initializing
        $taxes     = array(); // Initializing
        foreach ($rate->taxes as $key => $tax_cost){
            if( $tax_cost > 0 ) {
                $tax_rate    = $tax_cost / $cost; // Get the tax rate conversion
                $taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array
                $has_taxes   = true; // There are taxes (set it to true)
            }
        }
        if( $has_taxes ) {
            $rates[$targeted_rate_id]->taxes = $taxes; // Set taxes costs array
        }
    } else {
        unset($rates[$targeted_rate_id]);
    }
    return $rates;
}

Solution

  • To remove the colon symbol ": " from a displayed shipping method, use the following code:

    add_filter( 'woocommerce_cart_shipping_method_full_label', 'display_only_shipping_method_price', 10, 2 );
    function display_only_shipping_method_price( $label, $method ) {
        // HERE define your targeted rate id
        $targeted_rate_id = 'flat_rate:4';
    
        if ( $method->id === $targeted_rate_id && 0 < $method->cost ) {
            $label = str_replace( $method->label . ': ', '', $label);
        }
        return $label;
    }
    

    Related: