The right way to add a new field in admin product page in Prestashop 1.7
Asked Answered
T

1

6

I've created a module for Prestashop 1.7 in order to add a new field in the Admin Product page. Due to the lack of proper documentation, I want to ask the proper way to add my custom field, a select. I want to get saved and updated on product save.

I use the code below to add a tpl containing a form

public function hookDisplayAdminProductsExtra($params) {
    $this->smarty;

    $available_items = $this->getAvailableBadges();
    $id_product  = Tools::getValue('id_product');

    $this->context->smarty->assign('items_number', $available_items);
    return $this->display(__FILE__, '/views/templates/admin/admin_products.tpl');
}

The problem is that I don't know how to make to override the Product.php Class in order to have my $field and how to create the form element for the tpl.

The form I want to create I something like that

<select name="" id="">
    {foreach from=$items_number item=option}
        <option value="{$option}">
            {$option}
        </option>
    {/foreach}
</select>

Sorry for the lack of information, but I find the new way of creating modules very confusing. Thanks in advance

Tharpe answered 13/10, 2017 at 10:16 Comment(6)
I’m not answering you question but i had to do the same thing (add a custom field to product in PS 1.7. I’ve done this #43676196 But you’re right, best solution is to create a module for this I thinkInsphere
Thats the point, i don't want to accomplish the task with the "easy" way, i want to know the right way. Such a pitty that many things about Prestashop lacks proper documentationTharpe
did you find "the way" to do it?Czarism
It is really a good question. IMO, the famous answer mentioned is not the right way because it involves modifying src folder.Jannery
Did you find a way to save the data you have selected? From the new field you made.Anorak
Unfortunately no and i don't honestly think that there is a right way of doing things like that in this CMS. Maybe i am wrong, I've done using it for some years nowTharpe
H
1

To override the Product class :

Create a file "Product.php" in override/classes/

and add the following :

//override the class
class Product extends ProductCore {
    //add your attribute
    public $field_name;

    //override the construct function
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, \Context $context = null) {

        //add your custom field to the array $definitions['fields']
        self::$definition['fields']['field_name'] = [
            'type' => self::TYPE_STRING,
            'lang' => true, //if you have multiple languages on your site
            'required' => false,
            'size' => 255
        ];
    }
}

Then, add a new entry in your database, "field_name" in ps_product if lang is set to false or in ps_product_lang if lang is set to true.

You have different types : "TYPE_STRING", "TYPE_HTML", "TYPE_BOOL", "TYPE_INT", ... I don't know the exhaustive list. Depending on the type you choose you have to create the right column type in database ("VARCHAR", "TEXT", ....)

Some good documentation can be found in forum (stackoverflow, https://www.prestashop.com/forums/) but also on blog like this one : https://www.h-hennes.fr/blog/

Hamper answered 12/9, 2019 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.