Prestashop – Create Unique Reference ID with Product Combinations Generator

I use the prestashop amazon module and Amazon requires a unique reference number per combination. To save time modifying each reference I modified the combinations generator controller. If you have tons of attributes this will save you tons of time.

I’m pretty noob at this kind of stuff but it works. If you know a better way to do this let me know.

Thanks, zeki893

Here we go:

open AdminAttributeGeneratorController.php

Look for function addAttribute

You’ll see

foreach ($attributes as $attribute)
{
$price += (float)preg_replace('/[^0-9.-]/', '', str_replace(',', '.', Tools::getValue('price_impact_'.(int)$attribute)));
$weight += (float)preg_replace('/[^0-9.]/', '', str_replace(',', '.', Tools::getValue('weight_impact_'.(int)$attribute)));
}

I modified it into

foreach ($attributes as $attribute)
{
$price += (float)preg_replace('/[^0-9.-]/', '', str_replace(',', '.', Tools::getValue('price_impact_'.(int)$attribute)));
$weight += (float)preg_replace('/[^0-9.]/', '', str_replace(',', '.', Tools::getValue('weight_impact_'.(int)$attribute)));

//get the attributes name
$sqltest = 'SELECT * FROM `attribute_lang` WHERE `id_attribute` = '. $attribute .' LIMIT 0 , 1';
if ($resultstest = Db::getInstance()->ExecuteS($sqltest))
foreach ($resultstest as $rowtest)
$id_attribute_name = $rowtest[name];

//get the attributes group id to see if its color or size
$sqltest2 = 'SELECT * FROM `attribute` WHERE `id_attribute` = '. $attribute .' LIMIT 0 , 1';
if ($resultstest2 = Db::getInstance()->ExecuteS($sqltest2))
foreach ($resultstest2 as $rowtest2)
$id_attribute_colorsize = $rowtest2[id_attribute_group];

//this will determine if the attribute is a color or size
//11 is the color group id in my mysql database
if ($id_attribute_colorsize== 11) {
$id_attribute_color=$id_attribute_name;
} 
//12 is the size group id in my mysql database
elseif ($id_attribute_colorsize == 12) {
$id_attribute_size=$id_attribute_name;
}
}

You will need to modify

$id_attribute_colorsize== 11 and $id_attribute_colorsize == 12

I checked mysql attribute_group table to find the attribute group ID of color and size.

right below it you’ll find

if ($this->product->id)
{
return array(
'id_product' => (int)$this->product->id,
'price' => (float)$price,
'weight' => (float)$weight,
'ecotax' => 0,
'quantity' => (int)Tools::getValue('quantity'),
'reference' => pSQL($_POST['reference']),
'default_on' => 0,
'available_date' => '0000-00-00'
);
}

modify

‘reference’ => pSQL($_POST[‘reference’]),

to

‘reference’ => pSQL($_POST[‘reference’]).$id_attribute_color.’_’.$id_attribute_size,

You can re-order color or size anyway you’d like.

Your results will look like this.
products combinations generator
full modified function:

protected function addAttribute($attributes, $price = 0, $weight = 0)
  {
    foreach ($attributes as $attribute)
    {
      $price += (float)preg_replace('/[^0-9.-]/', '', str_replace(',', '.', Tools::getValue('price_impact_'.(int)$attribute)));
      $weight += (float)preg_replace('/[^0-9.]/', '', str_replace(',', '.', Tools::getValue('weight_impact_'.(int)$attribute)));
      //get attribute name
      $sqltest = 'SELECT * FROM  `attribute_lang` WHERE  `id_attribute` = '. $attribute .' LIMIT 0 , 1';
      if ($resultstest = Db::getInstance()->ExecuteS($sqltest))
    			foreach ($resultstest as $rowtest)
        			$id_attribute_name = $rowtest[name];
        		//echo $id_attribute_name;
      //get attribute color or size
      $sqltest2 = 'SELECT * FROM  `attribute` WHERE  `id_attribute` = '. $attribute .' LIMIT 0 , 1';
      if ($resultstest2 = Db::getInstance()->ExecuteS($sqltest2))
    			foreach ($resultstest2 as $rowtest2)
        			$id_attribute_colorsize = $rowtest2[id_attribute_group];
        	if ($id_attribute_colorsize== 11) {
        		$id_attribute_color=$id_attribute_name;

        	} elseif ($id_attribute_colorsize == 12) {
        		$id_attribute_size=$id_attribute_name;
        	}
    }

    if ($this->product->id)
    {
      return array(
        'id_product' => (int)$this->product->id,
        'price' => (float)$price,
        'weight' => (float)$weight,
        'ecotax' => 0,
        'quantity' => (int)Tools::getValue('quantity'),
        'reference' => pSQL($_POST['reference']).$id_attribute_color.'_'.$id_attribute_size,
        'default_on' => 0,
        'available_date' => '0000-00-00'
      );
    }
    return array();
  }

This can also be done via overrides so you don’t modify the core file.

Leave a Reply