简体   繁体   中英

Add order_id beneath custom Woocommerce Order Number on Edit Order page

We use the Booster plugin which creates a unique WooCommerce order number such as WC-456544646. I now need to add the original order_id which still works in the background and with the WooCommerce API.

Booster provides a shortcode for the original order ID [wcj_order_id]

Here is the HTML for the custom Woo order number on the Edit Order page.

<h2 class="woocommerce-order-data__heading">
Order #WC-63872414 details</h2>

在此处输入图片说明

How can I insert a shortcode onto the 'Edit Order' page?

(Woocommerce/Orders/[Select Order/Order Details] eg /post.php?post=638&action=edit

I looked into Booster plugin code. It uses woocommerce_order_number filter hook to replace the order ID in WC edit order page and orders. In /woocommerce-jetpack/includes/class-wcj-order-numbers.php :

add_filter( 'woocommerce_order_number', array( $this, 'display_order_number' ), PHP_INT_MAX, 2 );

Since it's not passing the $instance of neither WCJ_Order_Numbers nor WC_Jetpack classes in any action hook, in order to remove this text and use a custom one, we can not use remove_filter function. So to override Booster plugin, we have to hook another function to woocommerce_order_number .

To add WC order_id to admin order pages:

add_action( 'admin_init', 'append_wc_order_id');
function append_wc_order_id()
{
    add_filter('woocommerce_order_number', 'filter_wc_order_id', PHP_INT_MAX);
    function filter_wc_order_id($order_id)
    {
        /* If you are using 'init' action hook, uncomment bellow line to apply this code only to your admin pages: */
        //if (!is_admin()) return $order_id;
        return $order_id . ' | #' . do_shortcode('[wcj_order_id]');
    }
}

This code goes to your functions.php file of your theme.
Tested & Wordking :

截屏

Notes:
1. The filter hook in the Booster code, uses PHP_INT_MAX priority so in order to override this, we can not use bigger integer priority. Therefore I used admin_init action to hook my function to woocommerce_order_number , after other plugins done their job. It may function as you expected, without using init hooks, but it's not guaranteed. You can use init hook instead of admin_init to add the WC order_id on the front-end side of your site too (eg 'My Account' page). Read the comments inside the code.
2. To remove the order ID generated by Booster plugin on admin pages, you may change this line:

return $order_id . ' | #' . do_shortcode('[wcj_order_id]');

to:

return do_shortcode('[wcj_order_id]');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM