Discussions

How to use non-consecutive order numbers

Hi,
We run a small business, and as such do not expect to have a lot of orders through our site. Because of this, we would like to use order numbers that do not correspond to the number of orders that have actually been placed. Is there a way to change the way that order numbers are assigned? For example, to increment the number by 63 each time, or to just assign a random string?

Posted: Apr 29, 2013

Comments

lmeurs on August 4, 2013

According to http://www.drupalcommerce.org/discussions/8900/how-change-order-number-n... you can alter the order number using hook_commerce_order_presave().

<?php
/**
 * Implements hook_commerce_order_presave().
 *
 * Alter order number.
 * $order does not need to be referenced since it is an object.
 */
function yourmodule_commerce_order_presave($order) {
  if (isset(
$order->order_id)) {
   
// Alter order number by multplying the order id with 63.
   
$order->order_number = sprintf('%08d', $order->order_id * 63);
  }
}
?>

Dr.Osd on January 30, 2014

Thanks for this solution to change order number!
Can you help me: how can i change both order id and order number to timestamp?
Problem is an order id placed in url of order.

rawdesk on October 7, 2015

I've extended this hook_commerce_order_presave() so it also takes care of a higher increment of auto generated order_id's, as follows :

    function hook_commerce_order_presave($order) {
      if (!isset($order->order_id) && $order->status == 'cart') {
        // Presave on first add to cart event when order_id has not yet been assigned
   
      $last_id = db_query('SELECT MAX(order_id) FROM {commerce_order}')->fetchField();
    $auto_increment = $last_id + 7;
    $sql = "ALTER TABLE {commerce_order} AUTO_INCREMENT = $auto_increment";
    db_query($sql);
      } else {
       $order->order_number = sprintf('%08d', $order->order_id);
      }
    }

As visible in http://i.imgur.com/f0u0ff0.png, the first presave() fires on first add to cart without a filled in order_id. This was what i was looking for to manipulate the AUTO_INCREMENT value in the commerce_order table once per new cart creation. In this example increased with 7, but you could also increase it with a random value.

Additionally, i setted the order_number to the same order_id value, yet zero prefixed to 8 digits.