I need some advice on how to proceed with the following implementation. For my site in multiple languages that uses Polylang as a plugin, I would need to have separate and consecutive numbering for the orders. For example: Italian: IT-2024-01, IT-2024-02, IT-2024-03 German: DE-2024-01, DE-2024-02, DE-2024-03.
Right now, I just used this snippet for the Italian:
/**
* Orders Prefix
*/
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$order = wc_get_order( $order_id );
if ( $order && $order->get_date_created() ) {
$order_year = $order->get_date_created()->format( 'Y' );
$prefix = 'IT-' . $order_year . '-';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
return $order_id;
}
2
Answers
I have fixed the issue in two parts.
First I get the current language using polylang function
Then I pass it two custom function, which populates the desired sequence
Note: I have considered prefix with leading zeros upto 3 digits.
For e.g., EN-2024-001 will be the first order number for new order placed after this code integration.
This requires to be done when the order is created after checkout, setting the order number as custom metadata, to keep a lightweight process each time the order number is read from database.
The following code will set as order metadata the order number, sequentially by language code, year, and incremental number (on 2 digits. You can define the desired number of digits).
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
For orders created manually, it requires something different, as the language code is not yet defined.