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:

  • 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!

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