Add global custom field for all products - PrestaShop 1.6

811 views Asked by At

I want to add custom field, but one for all products, instead one per product. For that I have created override/classes/Product.php:

class Product extends ProductCore{

    public $is_exclusive = null;

    function __construct( $id_product = null, $full = false, $id_lang = null, 
                             $id_shop = null, Context $context = null ) {

        Product::$definition['fields']['is_exclusive'] = 
             array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml');
    }

I can create a field in ps_product_lang, but this will create one row per one product.

What I want is one field for all products. It can be edited and in "Product" page.

2

There are 2 answers

1
Mahdi Shad On BEST ANSWER

If you want just one field for all products, you have to add one Configuration.

You can override Product class and add this to construction method (if this field is only readable)

function __construct( $id_product = null, $full = false, $id_lang = null, 
                         $id_shop = null, Context $context = null ) {

    $this->is_exclusive = Configuration::get('custom_key');
}

also you have to add your custom value to Configuration table manually


True way is creating new module. you can manage your configurations in module config page:

Configuration::updateValue($key, $value); // save data

Configuration::get($key, $id_lang); // get data

If your data are affiliated with the language:

$value = [
    1 => 'value 1', // for language 1
    2 => 'value 2', // for language 2
    3 => 'value 3', // for language 3
];
Configuration::updateValue($key, $value); // save data

Configuration::get($key, 2); // get data of language 2

tip: The easiest way is to create a new module through PrestaShop module creator and then edit it.

0
ethercreation On

In this case, either you create it in the ps_product table, so that it is only present once with an override of the Product class, and an override of the BO of the product page.

Either what I recommend, is to create a module that will be grafted on the hook "displayAdminProductsExtra" so that you have more fields in the BO of your product. So you can have your table of additional information, without changing the structure of Prestashop.

You can then put a tpl to display it on the hook you want in FO.

Regards