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.
How can I prevent this?
2
Answers
Try this code.
Replace this lines.
To
Your code contains some mistakes
$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()
$order->get_id()
is not needed in this answer, as this is also passed to the functionSo you get