I am running my eCommerce shop using Prestashop 1.6. I have already reconfigured my Order Numbers to the format SO/2018/000001
based on ps_orders.ps_id_order
using this answer as a guide. Updating override/classes/PaymentModule.php
with this block:
$oNum = str_pad($order->id, 6, '0', STR_PAD_LEFT);
$oYear = substr(date(Y),0,4);
$order->reference = "SO/$oYear/$oNum";
$order->update();
I then used the following query to update set determine the next id_order
:
ALTER TABLE ps_orders AUTO_INCREMENT = 10
This made my reference
longer than the defined field, so I also had to adjust the definition in /override/class/Order.php
:
'reference' => array('type' => self::TYPE_STRING, 'size' => 14),
And in /override/class/OrderPayment.php
:
'order_reference' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything', 'size' => 14),
With all that done, my order numbers were formatted the way I wanted, but are not exactly as desired.
Rather than using the ps_orders
table's key index field as the basis of my order numbers, I would like to use a separate sequence which resets each year. Similar to how the OrderInvoiceCore
class uses the number
field to generate the actual invoice number based on the Invoice Options
set in the back office.
What is the best way to go about achieving this desired result? I am thinking that adding a sequence to the OrderCore
class, and using that to generate my order reference numbers in place of ps_orders.id_order
would do the trick. If that is the best solution in my case, how do I go about implementing such?
You can get the number of orders in current year and add one to it, when generating the reference.
$nextid = (int)Db::getInstance()->ExecuteS("SELECT count(`id_order`) FROM `"._DB_PREFIX_."orders` WHERE `date_add` > '".date('Y')."-01-01'") + 1;
I would do it by overriding the add function in Order class to something like:
public function add($autodate = true, $null_values = true)
{
$nextid = Db::getInstance()->ExecuteS("SELECT count(`id_order`) FROM `__DB_PREFIX__orders` WHERE `date_add` > '".date('Y')."-01-01'") + 1;
$this->reference = "SO/".date('Y')."/".str_pad($nextid, 6, '0', STR_PAD_LEFT);
parent::add($autodate, $null_values);
}