Search code examples
phpwordpresswoocommerceordersshipping-method

How to change Woocommerce shipping labels in cart, checkout and also on orders


I'm trying to change the shipping label based on whether any of the products in cart has an addon.

For that purpose, I am using How can I modify a Woocommerce shipping label answer code, with some changes to suit my needs.

The HTML:

<input type="hidden" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate5" value="flat_rate:5" class="shipping_method">
<label class="shipping__list_label" for="shipping_method_0_flat_rate5">Standard Shipping (2-3 days): <span class="woocommerce-Price-amount amount"><bdi>3,95<span class="woocommerce-Price-currencySymbol">€</span></bdi></span></label>

So the label Standard Shipping (2-3 days) must display Shipping with customization (4-6 days) if a certain condition is met (in this case: the field custom_text is not empty).

The functions.php:

//Change Standard Shipping Label if product is customized
add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_shipping_label', 10, 2 );
function change_shipping_label( $full_label, $method ){
    
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( !empty( $cart_item['custom_text'] ) ) {
        $full_label = str_replace( "Standard Shipping (2-3 days)", "Shipping with customization (4-6 days)", $full_label );
        }
    }   
    return $full_label;
}

This was a success. If the condition is met the label changes to Shipping with customization (4-6 days) prior checkout.

However, the label change does not apply after checkout (thank you page and emails, it displays the original "Standard Shipping (2-3 days)".

Can I display this label change also on thank you page and emails?


Solution

  • Update 2

    Based on the provided code and informations, to change specific shipping label on orders, using shipping rate Id to target the desired shipping method:

    add_action( 'woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4 );
    function action_wc_checkout_create_order_shipping_item( $item, $package_key, $package, $order ) {
        // Targeting "flat_rate:5" by its instance ID
        if( $item->get_instance_id() == 5 ) {
            $item->set_method_title( "Shipping with customization (4-6 days)" );
        }
    }
    

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

    Related: Change specific shipping method title on WooCommerce orders after checkout