How to customize a WooCommerce order number? - createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    How to customize a WooCommerce order number?

    December 13, 2021
    Last update: February 8, 2023
    < 1 min read
    25
    0
    0
    How to customize a WooCommerce order number?

    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.

    Meet our authors
    Aleksander Fredrych
    Alex
    Co-founder & CTO
    Monika Regulska
    Monika
    Head of Marketing
    Yuriy
    Yuriy
    Client's Ambassador
    Grzegorz Oleksa
    Grzegorz
    Content Specialist
    Maciej Brzeziński
    Maciej
    Technical Consultant

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us