Quick Tip: Replace Order References with IDs in Prestashop

If you find it annoying to have Order References with random characters, here is how to take Order IDs back in Prestashop, for customers as well.

Watch the Screencast

Text Version

There is just one quick step to take, in order to replace the new odd reference number, and that is amending the generateReference method of the Order class.

Let’s create an override for it! Create a new file inside override/classes/order and name it Order.php.
Add the usual override code inside:

Class Order extends OrderCore
{

}

Then, inside it, let’s paste the generateReference method:

	public static function generateReference()
	{
		return strtoupper(Tools::passwdGen(9, 'NO_NUMERIC'));
	}

We want to grab the very last order ID, then increase it by one, increase it by one and pad it so all the new references have the same length in terms of characters

	public static function generateReference()
	{
		$last_id = Db::getInstance()->getValue('
			SELECT MAX(id_order)
			FROM '._DB_PREFIX_.'orders');
		return str_pad((int)$last_id + 1, 9, '000000000', STR_PAD_LEFT);

	}

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Zack Tim

    Hi Nemo, what about if I have multi orders, and each order receives an order number, when I applied your code, it was assigning previous order reference for both orders. For example if I placed order 118 and 119, order reference will be 00000118 twice for both orders, how to fix this with a loop ?

  • Beppe Beretta

    Hi Nemo, very well done! But, using override, it doesn’t works! May you put here the complete code for the page override/classes/order/Order.php ? Thanks!

  • Muddabir Aziz

    Is there a way to make invoice number follow the same number as order number ?

    • NemoPS

      Not really, they are usually auto incremented, so if for any reason you cancel an order, even if there is an initial match in their numbers, they will just go off.
      An alternative is to display the formatted order id instead of the invoice number, but it needs some modifications

      • Muddabir Aziz

        Can you put a tutorial for that as well?

  • Jaroslaw Baranowski

    Thanks Nemo, great work and great info that saved me tons of time!
    PS 1.6.1.17

  • Bart

    I’ve done it and it works perfect but only once. The first order work’s well but the next can’t be finelized. It look’s like the number can’t be increased. Any solution to this? My Presta is 1.7

    • NemoPS

      Strange, are they not auto incrementing?

  • Dipak Chatterjee

    How to increase the order reference length too ? by default it is only 9. we need 30

    • NemoPS

      You can modify the length in the database if it’s limited.

      • Dipak Chatterjee

        Already did it. but getting an exception from prestashop

  • ronniee

    For newer version of PS 1.6.1.x can you provide same tip for changing random reference?
    thanks

    • NemoPS

      Actually it should work on that version too

      • ronniee

        Yes!
        I confirm is working on PS 1.6.1.4!
        Thank youuuu!

  • http://www.pddesign.com.au/ Paul D

    Hi Nemo, I placed the order.php in the overrides/class/orders folder, what do I do next?

    • NemoPS

      Clear cache if you don’t see your changes taking place

      • http://www.pddesign.com.au/ Paul D

        Hi Nemo, got this to work but my client keeps making mistakes with his orders which keeps throwing the numbering out. Is there any other solutions out there besides this code?

  • Muddabir Aziz

    can we make invoice and delivery slip number same as order id?

  • Jane Fox

    The brilliant sample of classes overriding! :) Thanks.

  • Filip Ignatiou

    Excellent Tip, What if I want to add a prefix like “Shop1-” before the Reference number?
    Could you be so kind to provide the change in the code line:
    return str_pad((int)$last_id + 1, 9, ‘000000000’, STR_PAD_LEFT);
    Thank you

  • Jedrzej Przeździęk

    OK, correct code:

    getValue(‘
    SELECT MAX(id_order)
    FROM ‘._DB_PREFIX_.’orders’);
    return str_pad((int)$last_id + 1, 9, ‘000000000’, STR_PAD_LEFT);

    }
    }

    Remember to delete cache/class_index.php after override.

  • John Giovanny

    thank you Nemo, it seems to work. but i´m not sure about the override file, does it contain only the 8 lines of code?

    • NemoPS

      Yes, exactly

  • Josías Moreno

    Great tip, it works perfectly. Thank you!

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: