I'm using Prestashop 1.7 I have added a code that will generate the Order reference to match the order ID, it works great but the issue is that in case I have multi orders, both orders will be assigned same order reference. For example: If order ID is 118, the order reference will be 000000118 but if there are 2 orders, Order IDs will be assigned 119 and 120, but in order reference they will be seen as 000000119 0000000119
Here is my code:
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);
}
}
To force the reference to be the same as the id, instead of overriding the generateReference, you can override the add function to something like:
public function add($autodate = true, $null_values = true)
{
$res = parent::add($autodate, $null_values);
if($res){
$this->reference = str_pad($this->id, 9, '0', STR_PAD_LEFT);
$this->update();
}
return $res;
}
If you don't want to use an override, you can use a module, hooked to actionObjectOrderAddAfter
, that is called by objectmodel with:
Hook::exec('actionObject'.get_class($this).'AddAfter', array('object' => $this));
easy solution override :
<?php
class Order extends OrderCore
{
public static function generateReference()
{
$last_id = Db::getInstance()->getValue(' SELECT MAX(id_order) FROM '._DB_PREFIX_.'orders');
return str_pad((int)$last_id + 1, 5, '00000', STR_PAD_LEFT);
}
}
?>
at last, you should delete class_index.php
in /var/cache/prod
to use it.