1
votes

I have some custom fields in my shipping information that is not being displayed "properly" in the shipping information of orders under woocommerce -> orders -> order#. The cart and checkout pages are different via php code that changes a label. The cart image1 is the original\core label that is defined in shipping methods image2, this is being sent to the order information page. I would like the checkout information to show instead which is shown in image3. Image4 is how it currently looks under the order information. The code I use to change the shipping information on checkout and emails is below.

// 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;
}

Cart page original label:

cart page original label

Defined under WooCommerce / Settings / Shipping:

defined under WooCommerce / Settings / Shipping

Checkout shipping information and what I want to show under the order:

checkout shipping information and what i want to show under the order

Shipping info under WooCommerce Admin > Orders > Edit Order:

shipping info under WooCommerce / Orders / Edit Order

1

1 Answers

1
votes

Instead of using the code from your previous question answer, use the following that will set the correct shipping method name and title for "Custom Carrier" shipping method with the carrier name and number, when customer place an order:

// Custom shipping label For Custom Carier with carrier name and number
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 ) {
    if ( isset($_POST['carrier_name']) && ! empty($_POST['carrier_name']) ) {
        // Get carrier number
        $carrier_number = isset($_POST['carrier_number']) && ! empty($_POST['carrier_number']) ? '(' . sanitize_text_field($_POST['carrier_number']) . ')' : ''; 
        
        $item->set_method_title( sprintf( '%s: %s %s', __("Custom Carrier", "woocommerce"), sanitize_text_field($_POST['carrier_name']), $carrier_number ) );
    }
}

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

So the Custom Carrier shipping method will be displayed everywhere on admin orders, customer orders (order received and order view) and email notifications.

Note: Be sure that 'carrier_name' and 'carrier_number' are the correct input names for your fields on checkout page.