INSIGHTS 

How to add new custom field to products in Prestashop. 

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.

When would you want to add a custom field?

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.

How to add a custom field in PrestaShop 1.6.

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.

Step 1) Override the product class.

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.

  1. Create the override file.

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

2. 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.

Step 2) Update the database.

Next, you'll need to add the new field to the database.

  1. Add the field to the product table.

• Execute the following SQL query:

ALTER TABLE `ps_product` ADD `second_reference` VARCHAR(255) NULL AFTER `reference`;
SQL

2. 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`;
SQL

4. This ensures that your custom field is compatible with PrestaShop's multistore functionality.

Step 3) Update the back office interface.

To make the new field visible and editable in the back office, you'll need to modify the product form.

Override the admin template.

Go to override/controllers/admin/templates/products/ and create a file named informations.tpl.

  • Copy the contents of admin/themes/default/template/controllers/products/informations.tpl to this new file.
  • Add the following HTML code to integrate the new field:
<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>
Smarty

2. This code snippet creates a new form field in the product information tab where you can enter the secondary reference number.

Step 4) Clear cache and test.

Finally, you'll need to clear the PrestaShop cache and test your new field.

  1. Clear the cache - Delete the class_index.php file located in the cache/ directory. This forces PrestaShop to recognise your override.
  2. Test the field - Go to the back office, edit a product, and check that the new "Second Reference" field appears and works as expected.

Adding a custom field in PrestaShop 1.7 and 8.x.

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.

Step 1) Extend the product class

As with PrestaShop 1.6, you'll need to create an override for the Product class.

  1. Create the override file
  • Go to override/ classes/ and create Product.php
  • Add the following code
<?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
3. This code adds the second_reference field to the Product class, just as in PrestaShop 1.6, but with adjustments for Symfony's architecture.

Step 2) Update the database.

Again, you’ll need to add the custom field to the database.
  1. Add the field to the product table

• Run the following SQL query:

ALTER TABLE `ps_product` ADD `second_reference` VARCHAR(255) NULL AFTER `reference`;
SQL

2. Add the field to the product shop table

• For multistore support, run this query:

ALTER TABLE `ps_product_shop` ADD `second_reference` VARCHAR(255) NULL AFTER `reference`;
SQL

Step 3) Customise the back office form.

In PrestaShop 1.7 and 8.x, you’ll use Twig templates and Symfony forms to customise the back office.

  1. Override the admin product controller.

    • 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

2. Update the twig template.

• 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>
Twig

3. This code snippet ensures that the new field is visible and editable in the back office.

Step 4) Clear cache and test.

As always, after making these changes, you need to clear the cache and test.

  1. Clear cache - Delete the prod and dev folders in var/cache/ to force PrestaShop to recognise your changes.
  2. Test the field - Go to the back office, edit a product, and verify that the "Second Reference" field is correctly displayed and functions as expected.

How does adding a new field to your products benefit your business?

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:

  • Tailored product data.

Customise product fields to manage specific information like secondary SKUs or industry-specific details.

  • Enhanced customer experience.

Provide extra product details to build trust and differentiate your offerings.

  • Improved inventory management.

Track products more accurately with custom fields like supplier codes or batch numbers.

  • Multistore flexibility.

Customise product attributes across different stores for targeted marketing.

  • Operational efficiency.

Automate processes and reduce manual data entry, saving time and reducing errors.

Back to Insights

Mailchimp stuff.

Registered in England & Wales. Company No. 7945108. VAT Registration No. 102 579 529.