Search code examples
phpwordpresswoocommercecheckoutshipping-method

Change WooCommerce shipping method label only on checkout page


I am trying to change the label of a radio button only on the checkout page, not the cart page. The label is present on both pages.

When i enter the below code it changes the label on checkout page but makes the cart page blank.

add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_shipping_label', 10, 2 );
function change_shipping_label( $full_label, $method ){
    if( ! is_checkout()) return; // Only on checkout page?

    $full_label = str_replace( "Custom Carrier (Enter Details Next Page)", "Custom Carrier", $full_label );

    return $full_label;
}

Checkout Page (correct label change

Cart Page (all labels have been wiped out

Someone who knows why this is?


Solution

  • You actually return nothing, because you only use return;. While it should be return $label;

    • is_checkout() - Returns true on the checkout page.
    • str_replace - Replace all occurrences of the search string with the replacement string

    So you get:

    function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) {
        // NOT returns true on the checkout page.
        if ( ! is_checkout() )
            return $label;
    
        $label = str_replace( "Custom Carrier (Enter Details Next Page)", "Custom Carrier", $label );
    
        return $label;
    }
    add_filter( 'woocommerce_cart_shipping_method_full_label', 'filter_woocommerce_cart_shipping_method_full_label', 10, 2 );