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

二次信任 提交于 2019-12-03 08:00:41

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!