Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeveloperWil/57f10f317870c86e217fb91427494512 to your computer and use it in GitHub Desktop.
Save DeveloperWil/57f10f317870c86e217fb91427494512 to your computer and use it in GitHub Desktop.
WooCommerce: Custom Order Number Based On Billing Customers Initials And Random Number between 10,000 and 99,999
/**
* Generate an order ID based on the billing customers initials and a random number between 10000 and 99999
*
* @author Wil Brown zeropointdevelopment.com
* @param $order_id
*
* @return string
* @throws Exception
*/
function zpd_change_woocommerce_order_number( $order_id ) {
$order = new WC_Order($order_id);
$billing_first_name = strtoupper( $order->get_billing_first_name() );
$billing_last_name = strtoupper( $order->get_billing_last_name() );
$random_order_number = random_int(10000, 99999);
return $billing_first_name[0] . $billing_last_name[0] . '-' . $random_order_number;
}
add_filter('woocommerce_order_number', 'zpd_change_woocommerce_order_number');
@jotlo
Copy link

jotlo commented Nov 19, 2021

Hi. Great code there! Is it possible to apply this only on new orders but not existing orders?

@DeveloperWil
Copy link
Author

Hi. Great code there! Is it possible to apply this only on new orders but not existing orders?

This will only apply to new orders after you have copied the code into your child theme's functions.php file so it won't touch existing orders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment