CreateIT
CreateIT
BLOG

How to customize a WooCommerce order number?

WooCommerce

How to customize a WooCommerce order number?

SHARE

Challenge:
apply a prefix and a sufix to woo order_number
Solution:
define woocommerce_order_number filter

By design, WooCommerce order number is just the next available ID from the wp_posts table. ID value is set as AUTO_INCREMENT. When you’re adding new WP posts / pages, a new row will be added in the wp_posts table. Also, shop orders are defined as records there.

SQL Find all orders

To retrieve all order list, we can use the SQL Select query.

# select all woocommerce orders
SELECT * FROM wp_posts WHERE post_type LIKE 'shop_order'

Custom order number

We would like to customize the order number displayed in order details, email notifications and API responses. The prefix will be 2 letter language code, the suffix will be the year when the order was created. Our WordPress uses multisite network configuration. A custom order number allows to distinguish orders between different sub sites / languages.

/**
 * // functions.php
 * Customize order number
 * Example: EN/604/2021
 */
add_filter( 'woocommerce_order_number', 'ct_change_woocommerce_order_number', 1, 2);

function ct_change_woocommerce_order_number( $order_id, $order ) {
    $blog_id = get_current_blog_id();
    $prefix = 'EN/';
    $orderDate = new DateTime($order->get_date_created());
    $suffix = '/' . $orderDate->format('Y');
    if($blog_id == 2) {
        $prefix = 'DE/';
    }
    return $prefix . $order->get_id() . $suffix;
}

That’s it for today’s guideline. Don’t forget to follow us for other useful suggestions and tips.

Need help?

  • Looking for support from experienced programmers?

  • Need to fix a bug in the code?

  • Want to customize your webste/application?

ADD COMMENT

Your email address will not be published. Required fields are marked *

createIT Contact