Skip to content
Advertisement

Changing shipped via text in WooCommerce orders and emails for specific shipping method

I am trying to change the shipped via in emails and in order information. I have a custom radio button with customer fields that are filled out only on the checkout page. Currently WooCommerce takes the label of the radio button but not the fields.

I would like to have the fields only in customer email and in the order information under woocommerce -> orders -> order#.

I can get it to change to text but lost on how to include the custom fields on a new line, which are carrier_name and carrier_number – these are the only fields that should show in both email and order information under WooCommerce.

The carrier_name and carrier_number are only available from the checkout page, not the cart page. When the ‘Custom Carrier’ is not selected it should print in email the correct shipping method, which I think it does by default but just wanted to include the requirement.

The third image shows the order information under woocommerce > orders > order#, this should show only carrier_name and carrier_number.

My code:

//adjusting emails to show custom carrier name and number
add_filter( 'woocommerce_order_shipping_to_display_shipped_via', 'wdo_remove_shipping_label_thnakyou_page_cart', 10, 2 );
function wdo_remove_shipping_label_thnakyou_page_cart($label, $method) {
    $shipping_label = get_post_meta($order->id, 'carrier_name') ;
    $shipping_label = 'Just Test!!!';
    return $shipping_label;
}

1 The Carrier Info I want to show, under Custom Carrier

1 The Carrier Info I want to show, under Custom Carrier

2 What I can currently get to send in email

2 What I can currently get to send in email

3 Order Information from woocommerce -> orders -> order#

3 Order Information from woocommerce -> orders -> order#

4 Order information without the code I put in, I changed the label using php code on checkout page different from cart page “Custom Carrier (Enter Details Next Page)” to checkout page “Custom Carrier”:

4 Order information without the code I put in, I changed the label using php code on checkout page different from cart page  "Custom Carrier (Enter Details Next Page)" to  checkout page "Custom Carrier"

Advertisement

Answer

There are some mistakes in your code like for example wrong function arguments…
Try the following instead:

// Adjusting order and emails "shipping via" to show custom carrier name and number
add_filter( 'woocommerce_order_shipping_to_display_shipped_via', 'wdo_filter_order_shipping_to_display_shipped_via', 10, 2 );
function wdo_filter_order_shipping_to_display_shipped_via( $shipped_via, $order ) {
    $carrier_name = $order->get_meta('carrier_name'); // Get carrier name

    // Targeting orders with defined "carrier name" for "Custom Carrier" shipping method
    if ( $carrier_name ) {
        $carrier_number = $order->get_meta('carrier_number'); // get carrier number
        $shipped_via = '&nbsp;<small class="shipped_via">' . sprintf( __( 'via Custom Carrier: %s (%s)', 'woocommerce' ), $carrier_name, $carrier_number ) . '</small>';
    }
    return $shipped_via;
}

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


Important note (update):

Change specific shipping method title on WooCommerce orders after checkout answer code replace this answer as it handle the change everywhere including on admin edit orders pages too and it’s a much lightweight solution.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement