Adding custom fields to products in PrestaShop is a common requirement for businesses and developers who need to store additional product information. Whether you're using PrestaShop 1.6, 1.7, or 8.x, our guide will walk you through the process of adding a new product field, making it editable in the back office and ensuring it's compatible with the multistore feature.
PrestaShop comes with a robust set of default fields for products, but sometimes you need more. Whether you're storing a secondary reference number, a custom SKU, or any other piece of data, adding a custom field allows you to tailor your store to meet your specific needs. However, the challenge comes with working out how to seamlessly integrate new fields into your existing Prestashop environment without disrupting existing functionality.
PrestaShop 1.6, although now outdated, is still used by many businesses. Adding a custom field to products in this version is straightforward but requires modifying both the codebase and the database.
To begin, you'll need to create an override of the Product class. This ensures that your changes won't be lost during an update.
• Navigate to override/classes/ and create a new file named Product.php.
• Add the following code to this file:
<?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, $full, $id_lang, $id_shop);
}
}
PHP2. This code adds a new property, second_reference, to the Product class. The $definition array is updated to include this field, specifying its type and validation rules.
Next, you'll need to add the new field to the database.
• Execute the following SQL query:
ALTER TABLE `ps_product` ADD `second_reference` VARCHAR(255) NULL AFTER `reference`;
SQL2. This adds a new column to the ps_product table, allowing you to store the secondary reference.
3. Support multistore Functionality - If you're using the multistore feature, you also need to add this field to the ps_product_shop table:
ALTER TABLE `ps_product_shop` ADD `second_reference` VARCHAR(255) NULL AFTER `reference`;
SQL4. This ensures that your custom field is compatible with PrestaShop's multistore functionality.
To make the new field visible and editable in the back office, you'll need to modify the product form.
Go to override/controllers/admin/templates/products/ and create a file named informations.tpl.
<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>
Smarty2. This code snippet creates a new form field in the product information tab where you can enter the secondary reference number.
Finally, you'll need to clear the PrestaShop cache and test your new field.
PrestaShop 1.7 introduced the Symfony framework and with PrestaShop 8.x, the platform has become even more modular and robust. Adding a custom field in these versions requires a slightly different approach due to the use of Symfony and Twig.
As with PrestaShop 1.6, you'll need to create an override for the Product class.
<?php
class Product extends ProductCore
{
public $second_reference;
public static $definition = array(
'table' => 'product',
'primary' => 'id_product',
'multilang' => true,
'fields' => array(
'second_reference' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'size' => 255),
),
);
public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
{
parent::__construct($id_product, $full, $id_lang, $id_shop);
}
}
PHP• Run the following SQL query:
ALTER TABLE `ps_product` ADD `second_reference` VARCHAR(255) NULL AFTER `reference`;
SQL• For multistore support, run this query:
ALTER TABLE `ps_product_shop` ADD `second_reference` VARCHAR(255) NULL AFTER `reference`;
SQLIn PrestaShop 1.7 and 8.x, you’ll use Twig templates and Symfony forms to customise the back office.
• Create or edit the AdminProductsController.php file in override/controllers/admin/.
• Add the following code to include your new field in the product form:<?php
class AdminProductsController extends AdminProductsControllerCore
{
public function __construct()
{
parent::__construct();
$this->fields_form_override = array(
'legend' => array(
'title' => $this->trans('Product', array(), 'Admin.Catalog.Feature'),
'icon' => 'icon-info-sign'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->trans('Second Reference', array(), 'Admin.Catalog.Feature'),
'name' => 'second_reference',
'size' => 255,
'required' => false,
'desc' => $this->trans('Enter the secondary reference code for the product.', array(), 'Admin.Catalog.Help')
),
),
);
}
}
PHP• Navigate to src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig.
• Add the following code in the appropriate section:
<div class="form-group">
<label class="control-label col-lg-3" for="second_reference">
{{ 'Second Reference Code'|trans({}, 'Admin.Catalog.Feature') }}
</label>
<div class="col-lg-5">
<input type="text" id="second_reference" name="second_reference" value="{{ product.second_reference }}" class="form-control">
</div>
</div>
Twig3. This code snippet ensures that the new field is visible and editable in the back office.
As always, after making these changes, you need to clear the cache and test.
There are multiple ways that adding a new field to your products can be of significant value to your business, these are just some of the ways that it can be useful:
Customise product fields to manage specific information like secondary SKUs or industry-specific details.
Provide extra product details to build trust and differentiate your offerings.
Track products more accurately with custom fields like supplier codes or batch numbers.
Customise product attributes across different stores for targeted marketing.
Automate processes and reduce manual data entry, saving time and reducing errors.