Issue adding date created to order number when creating order manually in WooCommerce

I use the following code to modify the order number in WooCommerce.

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);

function change_woocommerce_order_number( $order_id, $order ) {
    $prefix = '160-';
    // you will need to get your city as a variable to pass in to priffix 
    $order = wc_get_order( $order_id );
    $order_date = $order->get_date_created();
    $date_created = $order_date->date( 'Ymd' );
    
   
    // You can use either one of $order->id (or) $order_id
    // Both will wor
    return $prefix . $order->id . '-'.$date_created;
}   

This code works during the checkout process but I get an error like this when i manually create an order in WooCommerce backend.

enter image description here

How can I prevent this?

Solutions

Try this code.

Replace this lines.

 $order_date = $order->get_date_created();
 $date_created = $order_date->date( 'Ymd' );

To

$date_created = $order->get_date_created()->date('Ymd');

Your code contains some mistakes

  • The use $order = wc_get_order( $order_id );is not necessary, as $order already passed to the function
  • $order->id has been replaced since WooCommerce 3 (2017) with $order->get_id()
  • However, using/replacing $order->get_id() is not needed in this answer, as this is also passed to the function
  • The error will not occur when you place an order, but will occur when you want to create a new order in the backend (which is the case for you). Simply because the order has yet to be created, and that value does not yet exist

So you get

function filter_woocommerce_order_number( $order_number, $order ) { 
    // Prefix
    $prefix = '160-';
    
    // Is null
    if ( is_null( $order->get_date_created() ) ) {
        $order->set_date_created( new WC_DateTime() );
    }
    
    // Get date created
    $date_created = $order->get_date_created()->date( 'Ymd' );

    return $prefix . $order_number . '-' . $date_created;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );

Similar questions

Issue Creating Indices on Created Tables for Wordpress Plugin
I'm having an issue getting some indices created when I create my tables for a new Wordpress plugin. Here is the code: I also tried it with the ALTER TABLE statements in with the actual creation, the tables themselves get created, but the indices do not. What am I doing wrong? Or is there a different way to do this?
How to compare last modified date and created date using WP_Query
Following WP documentation at https://developer.wordpress.org/reference/classes/wp_query/#date-parameters I am trying to create a query that displays only posts which have a post modified date that is different their post created date: This is returning nothing however. Any ideas what I am doing wrong or how this can be achieved some other way?
Change Email with Phone number on Woocommerce Order Tracking Form or Simply track with only order number
I want to change the Email field with the phone field to track order status on the WooCommerce order tracking form page. if not possible then I want to simply disable the Email field so my client can track their order status by entering just order number. inside order/form-tracking.php I tried replacing "order_email" with "order_phone" and even tri...
How to Add Order Number, Order Date and Hour in a Woocommerce Email Template
I've been struggling for more than 4 days to customize my small WooCommerce shop email template by wanting to add the following requisites - order number, order date and hour when the order was mode. This is the email template:
Number of Posts - Display number (x of x), if number is less than 4 ( 4 is the post_per_page)
I am showing 4 custom post type posts per category on a parent post type archive page with category rows. I want to show the count of the category posts ($list->found_posts), but I am limiting the displayed posts to a random 4 posts. I have successfully displayed the total number of posts for each category (business_listing is the post type) The...
Adding prefix to WooCommerce order number if order has items from a specific product category
In a webshop that can only have one item in the cart, I need to add a prefix to the order number when the order contains an item from a specific category For this I wrote the following code: Now I need the following function to run if $category_in_order: But I cant seem to find out. Can I add a filter and function whitin an if statement?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.