Jump to content

[Free Module] Order Reference | Change order reference to a custom number with variables


muynck
 Share

Recommended Posts

Hi PrestaShop-merchants,

I have created a module for solving a age old problem: changing the order reference into something meaning full. The random string of characters is not a preferred order reference for most merchants.

You can use variables to fill the order reference with, like $cart->id, $order->id, $shop->name, or a combination of those. You can use any variable from several PrestaShop Objects (main classes). For a more detailed explanation checkout: https://github.com/blauwfruit/orderreference

See the latest releases: https://github.com/blauwfruit/orderreference/releases

Give me your opinion! Thanks!

 

  • Like 5

Share this post


Link to post
Share on other sites

  • 3 months later...

Thanks :)

Works great but what if I want permanent string or digit in the begining and order nummer with 4 preceding zeroes? For example 2020  or  ABC. This doesnt work:

2020{order->id:%04d} - 20200001,20200002,20200003,..

ABC{order->id:%04d}  - ABC0001,ABC0002,ABC0003,..

 

Thanks for answer.

Share this post


Link to post
Share on other sites

  • 2 months later...
  • 1 month later...
On 7/10/2020 at 12:50 PM, way1 said:

Hello! Could you advice, how get this kind of format?

[order date}_[random unique number of 3 digits]

for example:

2020-07-10_678

{order->date_add:%10.10s}{_}{cart->id:%03d} = 2020-07-10_678

{order->date_add:%7.7s}{_}{cart->id:%03d} = 2020-07_678

{order->date_add:%4.4s}{_}{cart->id:%03d} = 2020_678

If your PS create more than 999 carts, then the 3 last numbers would be increase to 4 in order to fit 1000 and so on.

Share this post


Link to post
Share on other sites

14 hours ago, EstebanGarviso said:

{order->date_add:%10.10s}{_}{cart->id:%03d} = 2020-07-10_678

{order->date_add:%7.7s}{_}{cart->id:%03d} = 2020-07_678

{order->date_add:%4.4s}{_}{cart->id:%03d} = 2020_678

If your PS create more than 999 carts, then the 3 last numbers would be increase to 4 in order to fit 1000 and so on.

At the moment a random number is not really possible. Can you create an issue to do this feature request? You can do this here: https://github.com/blauwfruit/PrestaShop-Order-Reference/issues

Share this post


Link to post
Share on other sites

  • 3 months later...

Great modul and thanks for it.

Questions:

1. Can I add a new starting number
e.x I want that my id start at 200
How can I do it?

2. e.x XYZ202100200 - starting at 200

This string:  {XYZ}{order->date_add:%4.4s}{order->id:%05d}
Right??

What would be the string for the year 21 instead of 2021

Thanks for some help and feedback!
Bjorn

Share this post


Link to post
Share on other sites

  • 3 months later...

Hi and thanks for the module! I noticed one small glitch when I used this:

{BE}{order->date_add:%4.4s}{order->id:%03d}

I created few orders and all had the same number: BE2021210.

I think it is due to the fact that it is taking first three digits from the id_order because in my case they are 2106003, 2106004, 2106005...

Is it possible to change it so it will take last three digits instead first three from the order ID?

Looks like I have an update. A customer received three emails, all with an identical ref.: BE2021210. However me, as a merchant, I received order confirmation email sent via ps_emailalerts contained correct numbers: BE20212106003, BE20212106004... In database, I see BE2021210 next to each of those orders. 

Since the reference cannot have more than 9 digits/letters, the only solution is to use last 3-4 numbers from the id_order.

Edited by mr_absinthe (see edit history)

Share this post


Link to post
Share on other sites

  • 2 weeks later...
On 6/13/2021 at 10:12 PM, mr_absinthe said:

Hi and thanks for the module! I noticed one small glitch when I used this:


{BE}{order->date_add:%4.4s}{order->id:%03d}

I created few orders and all had the same number: BE2021210.

I think it is due to the fact that it is taking first three digits from the id_order because in my case they are 2106003, 2106004, 2106005...

Is it possible to change it so it will take last three digits instead first three from the order ID?

Looks like I have an update. A customer received three emails, all with an identical ref.: BE2021210. However me, as a merchant, I received order confirmation email sent via ps_emailalerts contained correct numbers: BE20212106003, BE20212106004... In database, I see BE2021210 next to each of those orders. 

Since the reference cannot have more than 9 digits/letters, the only solution is to use last 3-4 numbers from the id_order.

I wanted to fetch the last number from the order id myself. So I made an small addition to orderreference.php
I haven't tested it, but it look fine in the back office preview.

You can see what I changed at
https://github.com/topfuel75/PrestaShop-Order-Reference/commit/a6a87c6d01762a7470fb24d88410966943b7bd87#diff-1da936e8328bb26324e0931c8b2c217f8a6c73cbc56f1d05ea5f511b1146f455

So in your case the following format should pull out the last 3 digits from the order id:

{BE}{order->date_add:%4.4s}{order->id:%03d:last:3}

I noticed that the following also seems to work

{BE}{order->date_add:%4.4s}{order->id:%d:last:3} 

 

Let me know how it goes.

Edited by redrum
Clarification (see edit history)

Share this post


Link to post
Share on other sites

I tested

{BE}{order->date_add:%4.4s}{order->id:%03d:last:3}

and it is working fine, as expected, well done!

It would be nice if someone can change order date to have last two digits of the year (year short) and month. For example:

June 2021 would become 2106. And if the yy and mm would be separate, we could use 21-06, 21_06, etc.

Currently we can only have 2021-06 when using

{order->date_add:%7.7s}

 

Edited by mr_absinthe (see edit history)

Share this post


Link to post
Share on other sites

29 minutes ago, mr_absinthe said:

It would be nice if someone can change order date to have last two digits of the year (year short) and month. For example:

June 2021 would become 2106.

Currently we can only have 2021-06 when using

{order->date_add:%7.7s}

 

The closest I come to that, without changing the code further, is 21-06.

{order->date_add:%7.7s:last:5}

 

Edited by redrum (see edit history)

Share this post


Link to post
Share on other sites

  • 5 months later...

I updated to your latest version and this no longer works (works just fine in previous version)

{BE}{order->date_add:%4.4s}{order->id:%03d:last:3}

It returns BE2021ord

this is working fine on previous version:

{order->date_add:%7.7s:last:5}

but not in version v1.2.0 (returns ANJPZFLNX) - tested twice

this is also not working on the latest PS v 1.7.7.6.:

{BE}{order->date_add:%4.4s:last:2}{order->id:%05d}

returns GUYWNOULU as well as this:

{BE}{order->date_add:%4.4s:last:2}{order->id:%05d:last:5}

also returns letters

 

Edited by mr_absinthe (see edit history)

Share this post


Link to post
Share on other sites

In the new update a bug is fixed. The reference will not be longer than 9 characters. Because if it was, there could be an error and it would return an empty value.

 

Try to create a format that will not exceed the 9 characters. Let me know if this helps.

 

Share this post


Link to post
Share on other sites

This seems to fix the issue, change in this:

    public function getFormattedReference($id_order)
    {
		$string = $this->getFormat();
		preg_match_all('/{(.*?)}/', $string, $matches);
		$reference = '';
		foreach ($matches[1] as $value) {
			$vars = explode(':', $value);
			if (count($vars)==2) {
				$reference .= sprintf("$vars[1]", $this->getVariable($vars[0], $id_order));
			} elseif (count($vars)==3) {
				if ($vars[2] == 'capitalize') {
					$reference .= strtoupper(sprintf("$vars[1]", $this->getVariable($vars[0], $id_order)));
				}
			} else {
				$reference .= $vars[0];
			}
		}

 

to this:

    public function getFormattedReference($id_order)
    {
		$string = $this->getFormat();
		preg_match_all('/{(.*?)}/', $string, $matches);
		$reference = '';
		foreach ($matches[1] as $value) {
			$vars = explode(':', $value);
			if (count($vars)==2) {
				$reference .= sprintf("$vars[1]", $this->getVariable($vars[0], $id_order));
			} elseif (count($vars)==3) {
				if ($vars[2] == 'capitalize') {
					$reference .= strtoupper(sprintf("$vars[1]", $this->getVariable($vars[0], $id_order)));
				}
			} elseif (count($vars)==4) {
				if ($vars[2] == 'last') {
					$reference .= substr(strtoupper(sprintf("$vars[1]", $this->getVariable($vars[0], $id_order))), -($vars[3]));
				}				
			} else {
				$reference .= $vars[0];
			}
		}

 

Edited by mr_absinthe (see edit history)

Share this post


Link to post
Share on other sites

5 minutes ago, mr_absinthe said:

This seems to fix the issue, change in this:

    public function getFormattedReference($id_order)
    {
		$string = $this->getFormat();
		preg_match_all('/{(.*?)}/', $string, $matches);
		$reference = '';
		foreach ($matches[1] as $value) {
			$vars = explode(':', $value);
			if (count($vars)==2) {
				$reference .= sprintf("$vars[1]", $this->getVariable($vars[0], $id_order));
			} elseif (count($vars)==3) {
				if ($vars[2] == 'capitalize') {
					$reference .= strtoupper(sprintf("$vars[1]", $this->getVariable($vars[0], $id_order)));
				}
			} else {
				$reference .= $vars[0];
			}
		}

 

to this:

    public function getFormattedReference($id_order)
    {
		$string = $this->getFormat();
		preg_match_all('/{(.*?)}/', $string, $matches);
		$reference = '';
		foreach ($matches[1] as $value) {
			$vars = explode(':', $value);
			if (count($vars)==2) {
				$reference .= sprintf("$vars[1]", $this->getVariable($vars[0], $id_order));
			} elseif (count($vars)==3) {
				if ($vars[2] == 'capitalize') {
					$reference .= strtoupper(sprintf("$vars[1]", $this->getVariable($vars[0], $id_order)));
				}
			} elseif (count($vars)==4) {
				if ($vars[2] == 'last') {
					$reference .= substr(strtoupper(sprintf("$vars[1]", $this->getVariable($vars[0], $id_order))), -($vars[3]));
				}				
			} else {
				$reference .= $vars[0];
			}
		}

 

Can you create an issue and a pull request for this?

Edited by muynck (see edit history)

Share this post


Link to post
Share on other sites

23 minutes ago, muynck said:

To be honest, I don't know what "last:5" does.

Can you give me an example of a format that you're trying to achieve?

BE2021003

Is that it?

last will return last digits, for example date_add:%7.7s will normally return 2021-12 (if order placed today) but last:5 will return 21-12 only. Feel free to do the pull request.

Share this post


Link to post
Share on other sites

Can you try this:

 

{BE}{order->date_add:%4.4s}{order->id:%03d}

The problem with this one however, is that when you have 1000 orders, the order number will be: BE2021100

Because the last 0 is the 10th characters... 

Share this post


Link to post
Share on other sites

That is why you would be better to use this:

{BE}{order->date_add:%4.4s}{order->id:%04d:last:3}

and you'll get BE2021000

or even better

{BE}{order->date_add:%4.4s:last:2}{order->id:%05d:last:5}

which should give you BE211000 and if you reach 10000 orders, you're safe: BE2110000

Share this post


Link to post
Share on other sites

  • 1 month later...

Hi @muynck  

Thanks for solving a age old problem 😉  its really big step forward ;)  

would be possible create an field to full up manually in order ? I asking because i have a very special customer who wants his own number  ....  ;(  

could you help me or give some tip how to do this ? 

Share this post


Link to post
Share on other sites

Sorry for not being able to check the latest post until now @mr_absinthe.
I haven't looked at the latest releases of this module, but I don't think they have added the code I posted with the possibility to fetch the last characters. That's probably why it stopped working after you did an update of the module.

Share this post


Link to post
Share on other sites

18 hours ago, Max Clee said:

Hi @muynck  

Thanks for solving a age old problem 😉  its really big step forward ;)  

would be possible create an field to full up manually in order ? I asking because i have a very special customer who wants his own number  ....  ;(  

could you help me or give some tip how to do this ? 

I don't know what you mean by "create an field to full up manually"?

Share this post


Link to post
Share on other sites

On 12/7/2021 at 5:39 PM, mr_absinthe said:

That is why you would be better to use this:

{BE}{order->date_add:%4.4s}{order->id:%04d:last:3}

and you'll get BE2021000

or even better

{BE}{order->date_add:%4.4s:last:2}{order->id:%05d:last:5}

which should give you BE211000 and if you reach 10000 orders, you're safe: BE2110000

Cool! Hopefully that code will be added in future releases.

@muynck Perhaps you should take a look at these two post and include that feature in the next release.
 


and
 

 

Share this post


Link to post
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More