Jump to content
  • Checkout
  • Login
  • Get in touch


The e-commerce.

Random Order Number


Recommended Posts

I've read all the posts and the problems that happen in the download.php, editing orders, and viewing order history and status.


There is a simple SOLUTION. All the problems happen because the default oscommerce version assumes the 'order_id' is an interger (int). So when all the columns in the table are changed from 'int' to 'varchar', you have to go through the pain of changing the script and renaming varibles ( e.g. (int)$oid to $iod ). And trust me it gets complicated and causes problems in various other pages.




(1) DO NOT include any HYPENS or LETTERS in the random order number, just simple INTEGERS.


(2) Change the column 'order_id' in TABLES orders, orders_products, orders_products_attributes, orders_products_download, orders_status_history and orders_total to BIGINT(20). So you DO NOT HAVE TO ALTER ANY SCRIPTS! The column is still considerd an integer.


(3) Place whatever algoritham you want to use to generate your order number in checkout_process.php right before:

// load the before_process function from the payment modules


*** NOTE make sure that the algoritham does NOT have any hypens or letters... only integers.


For example:


srand ((float) microtime() * 10000000);

$r1 = rand(1000,9999);

$t1 = date("yzHis");


$ordernum = $t1.$r1;


(4) Change the following code in checkout_process.php:


$sql_data_array = array('customers_id' => $customer_id,


to the following code:


$sql_data_array = array('orders_id' => $ordernum,

'customers_id' => $customer_id,


(5) Change this Code on the same page:


$insert_id = tep_db_insert_id();


to the following code:


$insert_id = $ordernum;


So, now you get a number like 5781838173280.


'5' being the year 2005 (It is NOT '05', b/c as a BIGINT the first '0' is dropped)

'78' being the number of the day in the year

'18' being the hour (24-hour)

'38' is minutes

'17' is seconds

'3280' random number


Now you wont have any problems either in your other pages, and you dont have to alter anything else... very simple.



OK, I have tried this for MS2 and it does not work :blink:

Link to comment
Share on other sites

  • 2 weeks later...
  • Replies 103
  • Created
  • Last Reply

The reason for everybodies problem is b/c the default of version oscommerce treats all instances of the order_id (e.g. orders_id, oID, etc.) as and integer (int). So, YOU HAVE TO REMOVE ALL the (int) occurances from the scripts.


Someone suggested that you just remove the hypens and letters, but that wont work for long b/c once your order_id goes above "2147483647" all the occurances of (int) will cause a problem b/c the maximum value of (int) is "2147483647", so all the orders will be duplicate!




(1) Install the script

(2) Change orders_id in the database from int(11) to varchar(20)

(3) Remove all occurances of (int) infront of all variables that represent "order_id" (e.g. order_id, oID) There are many, you have to go through all the main pages where a table with an orders_id is being called in an sql (e.g. TABLE_ORDERS, TABLE_ORDERS_PRODUCTS, etc.)


If thats too much work for you:



(1) Dont do it. Simple. b/c doing it half way will only result in future problems.

Link to comment
Share on other sites

  • 2 months later...



I do not want those long order numers.

Is not possible to:


1) Create a random number between 15001-99999

2) Check if the random number exist in the database.

3) if it exist run the loop one more time creating a new random number.

4) insert the new number as order id


Why? Because I want a small ordernumber that it easy too type but is random.


Is this not possible?


Best regards


Link to comment
Share on other sites

  • 2 years later...
I got it to work - here is what I did:

1. changed orders_id type from INT(11) to VARCHAR(11) in the following tables:







2. Pasted randomizing code right before this line

// load the before_process function from the payment modules

3. changed

$sql_data_array = array('customers_id' => $customer_id,


 $sql_data_array = array('orders_id' => $ordernum,

'customers_id' => $customer_id,

4. Changed this

 $insert_id = tep_db_insert_id();

to this

$insert_id = $ordernum;

that's all. It shows just once in account history.

Thanks a lot for the great idea!!!!


after I have done all those suggested by the post(though long time ago) I now have a problem.

At the admin side, [customer>>order] and I select the order - I get an error msg "Error Order does not exit"

but it does exist, and the order confirmation email is sent out, and the link on the confirmation email all works.


Errors are also found in the invoice and packing slip - all the details are missing (sold to, ship to, payment method, the prices and no. of units). The only accurate item on the invoice and packing slip is the item ordered.


Very strange.


Can anyone help??




THanks Heaps,

Yian Tay

(a php noob)

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...