Get in touch

Your Name *

Your Email *

Subject

Your Message

* required field.

If you ever need to add more data to a product, and make it editable in the back office (for example a secondary reference number) there are only a few files and database fields to change – We can also make it multi-store compatible!

To start with, we need to create an override of the Product class.

Go to overrides/Classes/ and create a file called Product.php.

In the newly created file, put the following:

<?php
class Product extends ProductCore
{
}

Now we need to declare our variable for our new reference field

<?php
class Product extends ProductCore
{
public $second_reference;
}

We will now need to declare the new variable in the definition of this class – instead of copying the entire multi-dimension array from the original Product class file, we can simply piggyback off the construct class and add to the definition from there:

<?php
class Product extends ProductCore
{
public $second_reference;
public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
{
self::$definition['fields']['second_reference'] = array('type' => self::TYPE_STRING, 'validate' => 'isString');
parent::__construct($id_product, $id_lang, $id_shop);
}
}

As you can see above, we are pointing to our own $definition variable, pointing at the fields array inside and adding our own value called ‘second_reference’ – We then are setting validation values:
Type – The type of data that the system should be expecting in the input field in the back office – eg. TYPE_STRING, TYPE_FLOAT, TYPE_INT, TYPE_BOOL.
Validate – These are set validation string for a similar validation technique – eg. isString, isInt, isBool.

If we wanted to use this new field in a multi-store, we can add an additional string here : ‘shop’ => true. This will mean that we need to add another field into our database, but will be explained below.

Once all this is done, we will need to go to the cache folder, and delete the class_index file within. This will force Prestashop to look for all overrides and add our new Product class override in the system and tell Prestashop to use our new file.

Now we will need to add our new field to the database.

In the _product table, we need to add another column to the end of the table – we should name it the same as our variable:

 

 

It is unlikely we will need any more than 255 characters, so VARCHAR field type is fine – if you are adding a text field for example, it may be worth using the ‘text’ file type.

If you are using these fields for a multi-store, you will need to add the same field to the _product_shop database table as well.

Now, when there is data in these field for this product, the new field will be available to use within the product object. In a php file, we can use the following to access the value in the database in our new field:

$product->second_reference;

In Smarty templates we can use:

{$product->second_reference}

The final piece of the puzzle is to add the field to the back office product information tab so the field can be edited and managed from here.
We will also need to create an override for our admin template so any updates wont wipe out any of our code.

Go to overrides/controllers/admin/templates/products/ (create the files if they do not exist) and create a new file called informations.tpl. Now go to your admin directory/themes/default/template/controllers/products/informations.tpl and copy the entire file to your new override.

Again, delete your class_index file in cache and the back office should be using your new override.

Now you can go to your override and add your new field – in our case we’ll copy the ‘reference’ form group and add it just after – you should be copying the following:

<div class="form-group">
<label class="control-label col-lg-3" for="reference">
<span class="label-tooltip" data-toggle="tooltip"  title="{l s='Your internal reference code for this product.'} {l s='Allowed special characters:'} .-_#\">
{$bullet_common_field} {l s='Reference code'}
</span>
</label>
<div class="col-lg-5">
<input type="text" id="reference" name="reference" value="{$product->reference|htmlentitiesUTF8}" />
</div>
</div>

You will need to change the field names to your new field so it looks like this:

<div class="form-group">
<label class="control-label col-lg-3" for="second_reference">
<span class="label-tooltip" data-toggle="tooltip" title="{l s='Our second reference code'}">
{$bullet_common_field} {l s='Second Reference code'}
</span>
</label>
<div class="col-lg-5">
<input type="text" id="second_reference" name="second_reference" value="{$product->second_reference|htmlentitiesUTF8}" />
</div>
</div>

And that’s it!

If you refresh your back office and edit a product, you will see your new field in the information tab and add the data you need!