Skip to content

Instantly share code, notes, and snippets.

@gh-o-st
Created November 5, 2019 06:50
Show Gist options
  • Select an option

  • Save gh-o-st/dbcb14f361072a29b3476dfdd066b891 to your computer and use it in GitHub Desktop.

Select an option

Save gh-o-st/dbcb14f361072a29b3476dfdd066b891 to your computer and use it in GitHub Desktop.
Create pseudo-random WooCommerce order numbers
<?php
// Create pseudo-random order numbers while retaining
// the uniqueness of Woo's order number system
function change_woocommerce_order_number($order_id) {
// The prefix is prepended to the actual order number
// It consists of the month number (without leading zeros) followed by a hyphen
// and then followed by the current minutes (minus leading zeros)
$prefix = date('n').'-'.ltrim(date('i'), 0); // This is prepended to the order number
// The suffix is appended to the actual order number
// It consists of the current seconds (without leading zeros) followed
// by the current hour (24 hour format)
$suffix = ltrim(date('s'), 0).date('G');
// Get rid of the # symbol
$order_id = str_ireplace("#", "", $order_id);
// Put it all together...
$new_order_id = $prefix . $order_id . $suffix;
return $new_order_id;
}
add_filter('woocommerce_order_number', 'change_woocommerce_order_number');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment