Jump to content

Product's Reference code field to be required and unique


soulefay

Recommended Posts

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 by soulefay (see edit history)
Link to comment
Share on other sites

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), 
  • Confused 1
Link to comment
Share on other sites

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

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

  • 2 months later...

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 by Divyesh Prajapati (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 10 months later...

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 by TiendaRunningCol (see edit history)
Link to comment
Share on other sites

  • 8 months later...

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 by nicetazmenia (see edit history)
Link to comment
Share on other sites

  • 10 months later...

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

  • 6 months later...
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

  • 3 years later...

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);
	       }
    }

 

  • Like 1
Link to comment
Share on other sites

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

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

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
×
×
  • Create New...