Get a free advice now!

    Pick the topic

    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Tags

    How to customize a WooCommerce order number?

    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.

    Comments
    0 response

    Add comment

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

    Popular news

    eCommerce growth – is your business ready?
    • Services
    • Trends

    eCommerce growth – is your business ready?

    April 8, 2024 by createIT
    Digital marketing without third-party cookies – new rules
    • Technology
    • Trends

    Digital marketing without third-party cookies – new rules

    February 21, 2024 by createIT
    eCommerce healthcheck
    • Services
    • Trends

    eCommerce healthcheck

    January 24, 2024 by createIT
    Live Visitor Count in WooCommerce with SSE
    • Dev Tips and Tricks

    Live Visitor Count in WooCommerce with SSE

    December 12, 2023 by createIT
    Calculate shipping costs programmatically in WooCommerce
    • Dev Tips and Tricks

    Calculate shipping costs programmatically in WooCommerce

    December 11, 2023 by createIT
    Designing a cookie consent modal certified by TCF IAB
    • Dev Tips and Tricks

    Designing a cookie consent modal certified by TCF IAB

    December 7, 2023 by createIT
    Understanding the IAB’s Global Vendor List (GVL)
    • Dev Tips and Tricks

    Understanding the IAB’s Global Vendor List (GVL)

    December 6, 2023 by createIT

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

    Contact us