Product’s Reference code field to be required and unique
Hi All,
To have product reference unique and required just need to change two core files. Follow the steps given below.
1. To make product reference required. Open file Product.php ( /classes/Product.php )
Find code
1 |
'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32), |
and change it to
1 |
'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32, 'required' => true), |
2. To make Product Reference unique open AdminProductsController.php ( /controllers/admin/AdminProductsController.php )
Find function checkProduct() , it should be like this
1 |
public function checkProduct() |
and add this line in starting of checkProduct() function
1 2 |
if( Tools::getValue('id_product') == 0) $this->checkUniqueRef(); |
and at the end of file ( before last curly brace ) add following function
1 2 3 4 5 6 7 8 9 10 |
public function checkUniqueRef() { $reference = Tools::getValue('reference'); $sql = 'SELECT id_product FROM `'._DB_PREFIX_.'product` WHERE reference="'.$reference.'" '; $res = Db::getInstance()->getValue($sql); if($res) { $this->errors[] = sprintf(Tools::displayError('Product with reference %s is already exists.'), $reference); } } |
That’s It!!!!
I have tested this in Prestashop version 1.6.x.y and working well at my end. Hope these helps you guys!!!