Get a free advice now!

    Pick the topic

    [checkbox* "Developer Outsourcing" "Web developing" "App developing" "Digital Marketing" "eCommerce systems" "eEntertainment 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

    PHPStorm – fix long load time of a directory window
    • Dev Tips and Tricks

    PHPStorm – fix long load time of a directory window

    January 20, 2023 by createIT
    reCAPTCHA v3 – WordPress implementation
    • Dev Tips and Tricks

    reCAPTCHA v3 – WordPress implementation

    January 20, 2023 by createIT
    How to compare GIT and server files
    • Dev Tips and Tricks

    How to compare GIT and server files

    January 19, 2023 by createIT
    How to trigger a click event inside iframe?
    • Dev Tips and Tricks

    How to trigger a click event inside iframe?

    January 19, 2023 by createIT
    FOOEvents – generate a custom CSV report
    • Dev Tips and Tricks

    FOOEvents – generate a custom CSV report

    January 19, 2023 by createIT
    Headless chrome – testing webgl using playwright
    • Dev Tips and Tricks

    Headless chrome – testing webgl using playwright

    January 18, 2023 by createIT
    Preview big SQL files with PilotEdit
    • Dev Tips and Tricks

    Preview big SQL files with PilotEdit

    January 18, 2023 by createIT
    Outsourcing a team of backend developers
    • Our Highlights

    Outsourcing a team of backend developers

    January 18, 2023 by createIT
    A new look for an aspiring brand
    • Our Highlights

    A new look for an aspiring brand

    January 18, 2023 by createIT
    Percy AI visual regressions – playwright example
    • Dev Tips and Tricks

    Percy AI visual regressions – playwright example

    January 17, 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