Prestashop 1.6 - Add a custom field to the category

I would like to know how to add a custom area to a category and how I can edit it in the back office (in the description field). the field that I would like to add is the name description_long

Field Type TEXT

I have already rewritten my front office, and my field is well displayed.

override \ Classes \ category.php

 <?php class Category extends CategoryCore { public $description_long; /** * @see ObjectModel::$definition */ public static $definition = array( 'table' => 'category', 'primary' => 'id_category', 'multilang' => true, 'multilang_shop' => true, 'fields' => array( 'nleft' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'nright' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'level_depth' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true), 'id_parent' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'id_shop_default' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'is_root_category' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), 'position' => array('type' => self::TYPE_INT), 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), // Lang fields 'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128), 'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128), 'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'), 'description_long' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'), // CUSTOM FIELD 'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128), 'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255), 'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255), ), ); } 

Did not find a single passage, can anyone help?

+5
source share
3 answers

to add a field to your backoffice, you need to override the AdminCategoriesController, exactly the renderForm () function and add a new field to it. To do this, create a new AdminCategoriesController file under / override / controller / admin /, then declare the extension of the source controller in it and copy it to the renderForm function (completely) from the kernel source file.

 class AdminCategoriesController extends AdminCategoriesControllerCore { public function renderForm() { ... } } 

now we have to edit it in a couple, first we need to add a new field to the description, so in the declaration 'name' => 'description' inside your renderForm (), you will see that this is a list of the array, and each of them describes a form field . Immediately after the description array adds a new field:

  array( 'type' => 'textarea', 'label' => $this->l('Description long'), 'name' => 'description_long', 'lang' => true, 'autoload_rte' => true, 'hint' => $this->l('Invalid characters:').' <>;=#{}', ), 

this ad asks Prestashop to create a new field with the following specification:

  • textarea field
  • multilingual field
  • javascript plugin for editing

  • name "description_long"

By declaring the field in this way, we allow pretashop to process it in the same way as any other class property, so our part will not need any work to add and update the field in the database.

Now there is one more thing worth doing in our renderForm () function, now the last instruction is parent::renderForm() , which in the original class called AdminController to ask it to make forms, but right now, as we extend the class, the command our parent calls AdminCategoriesControllerCore, overriding all of our work and displaying the default form. To avoid this change, from parent::renderForm to AdminController::renderForm() , specifying a call to the class of interest.

+3
source

To someone who fights, full answers:

Add a new description_long field to the category in the Prestashop category, you need 3 steps:

  • Update DB

Add your field named description_long to the category_lang table, you can simulate the characteristics of the description column

  1. Category Override Class

Create the file here /override/classes/Category.php using this code:

 class Category extends CategoryCore { public $description_long; public function __construct($id_category = null, $id_lang = null, $id_shop = null){ self::$definition['fields']['description_long'] = array('type' => self::TYPE_HTML, 'lang' => true); parent::__construct($id_category, $id_lang, $id_shop); } } 
  1. Override AdminCategoriesControllerCore Class

Create the file here /override/controllers/admin/AdminCategoriesController.php using this code:

 class AdminCategoriesController extends AdminCategoriesControllerCore{ public function renderForm() { $this->fields_form_override =array( array( 'type' => 'textarea', 'label' => $this->l('Description long'), 'name' => 'description_long', 'lang' => true, 'autoload_rte' => true, 'hint' => $this->l('Invalid characters:').' <>;=#{}', ), ); return parent::renderForm(); } } 
+10
source

Add this line to your __construct function of the overridden class

 public function __construct($id_category = null, $id_lang = null, $id_shop = null) { self::$definition['fields']['description_long'] = array('type' => self::TYPE_HTML, 'lang' => true); parent::__construct($id_category, $id_lang, $id_shop); } 

where description_long is your new field name.

0
source

All Articles