Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Allow Admin to Edit Order Numbers in WooCommerce?

In WooCommerce, there has been some flexibility provided to store owners to edit orders on the edit orders page. However, out-of-the-box solutions might lack such flexibility desired by administrators to edit the default order numbers. To address this, we will show you how to implement a custom solution allowing administrators to edit order numbers directly from the WooCommerce admin interface.

Solution: Allow Admin to Edit Order Numbers in WooCommerce

The code snippet will bring in an input field labeled “Custom Order number” to the admin’s edit order page. So that admin can set their preferred order number as per their choice. For example, if the default WooCommerce order number is “123” admin can edit it by a custom order number “ABC456” set on the edit order page. Now in the WooCommerce orders table and in emails, “ABC456” will be shown as the order number instead of “123”.

add_action('woocommerce_admin_order_data_after_order_details', 'ts_display_admin_order_order_number_custom_field', 10, 1);
function ts_display_admin_order_order_number_custom_field($order){
    $custom_order_number = get_post_meta($order->get_id(), '_order_number', true);
    // Output the custom order number field
    echo '<div class="edit_order_number"><p class="form-field _order_number_field" style="width:100%;">
    <label for="_order_number">'. __("Order number", "woocommerce").':</label>
    <input type="text" id="_order_number" name="_order_number" value="'. $custom_order_number .'">
    </p></div>';
}
// Save custom order number when order is updated in the admin page
add_action('woocommerce_process_shop_order_meta', 'ts_save_admin_order_order_number_custom_field');
function ts_save_admin_order_order_number_custom_field($post_id){
    // Check the user's permissions.
    if (!current_user_can('edit_shop_order', $post_id)) {
        return;
    }
    // Make sure that '_order_number' is set.
    if (isset($_POST['_order_number'])) {
        // Get the custom order number from the POST data
        $custom_order_number = sanitize_text_field($_POST['_order_number']);
        // Update custom field value
        update_post_meta($post_id, '_order_number', $custom_order_number);
    }
}
// Filter to replace order number with custom order number
add_filter('woocommerce_order_number', 'ts_replace_order_number_with_custom', 10, 2);
function ts_replace_order_number_with_custom($order_number, $order){
    $custom_order_number = $order->get_meta('_order_number');
  
    if (!empty($custom_order_number)) {
        $order_number = $custom_order_number;
    }
    return $order_number;
}

Output

When an admin goes to the edit order page in the WooCommerce admin panel, they will see a new input field labeled ‘Order number’. This field is displayed below the standard order details section. From here, admin can enter their preferred custom order number and update the field.

How to Allow Admin to Edit Order Numbers in WooCommerce? - Tyche Softwares

If the admin goes back and visits the orders table in the WooCommerce admin panel, they will see the original order ID has been replaced by the custom order number.

How to Allow Admin to Edit Order Numbers in WooCommerce? - Tyche Softwares

Giving admins the ability to tweak order numbers in WooCommerce sounds handy but it’s a very rare use case. Most businesses prefer automation for quick processing. Also, this customization comes with its own set of headaches, like confusion and manual errors. To smooth things out, think about automatically generating unique random custom order numbers. By leveraging automation, you can maintain a more organized and secure order management system

Browse more in: Code Snippets, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x