soulefay Posted January 19, 2015 Share Posted January 19, 2015 (edited) Hello everyone. I would like to know how to make the 'Reference code' field as required, and that the value must be unique, cannot be repeated? And if possible (optional but preferable), warning will be shown if repeated Reference code is detected. I did tried to search for the solution but I couldn't find any. Appreciate everyone's feedbacks. Many thanks! I am using PrestaShop version: 1.6.0.9 Edited January 19, 2015 by soulefay (see edit history) Link to comment Share on other sites More sharing options...
soulefay Posted January 19, 2015 Author Share Posted January 19, 2015 I'm sorry, just realized I've posted this on the wrong section. Can moderator please help me to delete this topic? Or I'll start a new topic under technical section instead. Many thanks. Link to comment Share on other sites More sharing options...
vekia Posted January 19, 2015 Share Posted January 19, 2015 to mark it as required open Classes/Product.php there is a code like: 'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32), change it to: 'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32, 'required' => true), 1 Link to comment Share on other sites More sharing options...
soulefay Posted January 19, 2015 Author Share Posted January 19, 2015 (edited) Thanks vekia for your prompt reply! But how about detecting whether the value is unique or repeated? Edited January 19, 2015 by soulefay (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted January 19, 2015 Share Posted January 19, 2015 im trying to understand it how to achieve it with code anyway, meanwhile you can try to change "reference" field in ps_products table via phpmyadmin (database manager) to UNIQUE Link to comment Share on other sites More sharing options...
soulefay Posted January 19, 2015 Author Share Posted January 19, 2015 Thanks, I've added UNIQUE and it does work! but the error message will prompt "An error occurred while creating an object. product". May i know where/which file can i edit the error message please? Link to comment Share on other sites More sharing options...
vekia Posted January 19, 2015 Share Posted January 19, 2015 this error message is an "global" message, if you will change it, it will appear also in other cases totally not related to reference field. it will be confusing a little. Link to comment Share on other sites More sharing options...
soulefay Posted January 19, 2015 Author Share Posted January 19, 2015 True also, wouldn't be wise to change it. Is it possible to add a new error for this then? Specifically for the reference code field. Anyway, thanks for your help! Really appreciate it. And could you help me to delete this topic please: https://www.prestashop.com/forums/topic/395109-products-reference-code-field-to-be-required-and-unique/?do=findComment&comment=1927558. Double posted. Thanks! Link to comment Share on other sites More sharing options...
soulefay Posted January 21, 2015 Author Share Posted January 21, 2015 vekia, I wish to revert back so I DROP INDEX but I'm still getting the same error: An error occurred while creating an object. product and when i tried to duplicate a product, i'll get this error: An error occurred while creating an object even if the Reference code is empty. Help please! Link to comment Share on other sites More sharing options...
Divyesh Prajapati Posted March 26, 2015 Share Posted March 26, 2015 (edited) 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 'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32), and change it to '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 public function checkProduct() and add this line in starting of checkProduct() function if( Tools::getValue('id_product') == 0) $this->checkUniqueRef(); and at the end of file ( before last curly brace ) add following function 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!!! cheers :) Edited March 27, 2015 by Divyesh Prajapati (see edit history) 1 Link to comment Share on other sites More sharing options...
ovy79ro Posted March 27, 2015 Share Posted March 27, 2015 Hello, Works in version 1.4.8? Link to comment Share on other sites More sharing options...
Divyesh Prajapati Posted April 30, 2015 Share Posted April 30, 2015 Hello, Works in version 1.4.8? Yeah; May be. Have you tried ? Link to comment Share on other sites More sharing options...
Divyesh Prajapati Posted April 30, 2015 Share Posted April 30, 2015 I think this should me marked as [solved]. Link to comment Share on other sites More sharing options...
TiendaRunningCol Posted March 19, 2016 Share Posted March 19, 2016 (edited) Hola! Soy nueva en esto, ya creé mi tienda pero al hacer compras de prueba antes de salir al aire, me aparece este mensaje y no me deja seguir. UNIQUE_REFERENCE La órden de pago con id [106481557] ya se encuentra registrada con la referencia 6. La verdad no se que hacer, les agradezco la ayuda que me puedan brindar. Edited March 19, 2016 by TiendaRunningCol (see edit history) Link to comment Share on other sites More sharing options...
nicetazmenia Posted December 14, 2016 Share Posted December 14, 2016 (edited) Hello Divyesh Prajapati First, thank you for the putting a solution here. I tried your instruction and I manage to put those code to the appropriate files. I know the the reference required works since I am getting an error where I leave that field blank, and when I put the reference number it allows me to update the product; however testing it to have a unique ref number it does not work for me, it still allow me to enter the same reference # to the different product, of course to doing this intentionally since I wanted to see if this work. I am using Prestashop 1.6.1.9. I wanted to clarify maybe I did not put the code where it is supposed to be, here is where I put the code: 1. I open file Product.php = added the 'required' => true 'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32, 'required' => true), 2. I open open AdminProductsController.php = this is where I am not 100% sure. Under the public function checkProduct(), I place the code right after the opening { if( Tools::getValue('id_product') == 0) $this->checkUniqueRef(); And then I put the other code right before the last } of the file, which from my understanding is the code to call the checkProduct() function. Please let me know if I place them correctly, and please I need help to have this working. Thank you! Edited December 14, 2016 by nicetazmenia (see edit history) Link to comment Share on other sites More sharing options...
chirag_0110 Posted November 1, 2017 Share Posted November 1, 2017 Hi, Added 'required' => true in classes/Product.php to make reference field required on product add edit. Here is my line of code . 'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32, 'required' => true), But when i try to add product without reference , There is no message display for reference validation and product saved successfully. I checked after clear cache but no luck. I am using PS-1.7.2.2 with default theme "classic". Can anyone help me to resolve this issue. Thanks in advance. Link to comment Share on other sites More sharing options...
ibrahimovich87 Posted May 15, 2018 Share Posted May 15, 2018 On 1/11/2017 at 7:34 AM, chirag_0110 said: Hi, Added 'required' => true in classes/Product.php to make reference field required on product add edit. Here is my line of code . 'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32, 'required' => true), But when i try to add product without reference , There is no message display for reference validation and product saved successfully. I checked after clear cache but no luck. I am using PS-1.7.2.2 with default theme "classic". Can anyone help me to resolve this issue. Thanks in advance. Hi all, i nedd this function on PS 1.7.2.5 and unique Reference code too, anyone can Help me? Link to comment Share on other sites More sharing options...
Aaron Kleinman Posted March 18, 2022 Share Posted March 18, 2022 Speaking of the reference uniqueness check in v1.7.8.4, works (both upon creation/update) but with a small code alteration 1. In the function public function checkProduct() insert following (after the opening bracket) if( Tools::getValue('id_product') != 0) $this->checkUniqueRef(); 2. As already suggested above, at the end of the file (before last bracket) insert following: 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); } } 1 Link to comment Share on other sites More sharing options...
wad.agency Posted March 21, 2022 Share Posted March 21, 2022 On 3/18/2022 at 12:38 PM, Aaron Kleinman said: Speaking of the reference uniqueness check in v1.7.8.4, works (both upon creation/update) but with a small code alteration 1. In the function public function checkProduct() insert following (after the opening bracket) if( Tools::getValue('id_product') != 0) $this->checkUniqueRef(); 2. As already suggested above, at the end of the file (before last bracket) insert following: 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); } } Ok, good solution, but in checkUniqueRef you have to add in sql another check like this 👍 public function checkUniqueRef() { $reference = Tools::getValue('reference'); $product_id = Tools::getValue('id_product'); $sql = 'SELECT id_product FROM `'._DB_PREFIX_.'product` WHERE reference="'.$reference.'" AND id_product != '.$product_id; $res = Db::getInstance()->getValue($sql); if($res) { $this->errors[] = $this->trans( 'Product with reference %s is already exists', [ $reference, ], 'Admin.Notifications.Error' ); } } Link to comment Share on other sites More sharing options...
Aaron Kleinman Posted March 21, 2022 Share Posted March 21, 2022 4 hours ago, wad.agency said: Ok, good solution, but in checkUniqueRef you have to add in sql another check like this 👍 public function checkUniqueRef() { $reference = Tools::getValue('reference'); $product_id = Tools::getValue('id_product'); $sql = 'SELECT id_product FROM `'._DB_PREFIX_.'product` WHERE reference="'.$reference.'" AND id_product != '.$product_id; $res = Db::getInstance()->getValue($sql); if($res) { $this->errors[] = $this->trans( 'Product with reference %s is already exists', [ $reference, ], 'Admin.Notifications.Error' ); } } Honestly that's where I tried to implement this check initially - on SQL, without changing the collations of the reference column, as it's not really a good practice. Also tried to develop the trigger "BEFORE INSERT" and "BEFORE UPDATE" but functionally I didn't achieve any result - still inserts even if duplicate. On the other hand, this code indeed does the check, but there's one disadvantage to it, which I'm trying now to understand how to go about - upon update of the product it still performs this check and throws back a duplicate error. You can remove the reference -> save -> and place the reference back, but ultimately as a quick solution will do the job. Will post the modified code later. Link to comment Share on other sites More sharing options...
Quique García Posted July 17 Share Posted July 17 This answer is no longer valid since version 8.1.x if you are using the new product features, somebody knows how to achieve this with the new version? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now