Change specific shipping method title on WooCommerce orders after checkout

不羁岁月 提交于 2021-02-10 14:35:27

问题


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:

Defined under WooCommerce / Settings / Shipping:

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

Shipping info under WooCommerce Admin > Orders > Edit Order:


回答1:


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.



来源:https://stackoverflow.com/questions/65754067/change-specific-shipping-method-title-on-woocommerce-orders-after-checkout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!