![]() ![]() Return $this->display(_FILE_, 'newfieldstut.tpl') Įxplanation: first, we make sure the current product object is correctly loaded if so we display the content of the module’s tpl file. If (Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product')))) Public function hookDisplayAdminProductsExtra($params) Time to test it out! Add the following function to your module: ![]() Okay, our module is installed, and the hooks have been registered as well. If it does, move on to the next step!Ĭreating a new tab in the Product Back Office (DisplayAdminProductsExtra) Now, just to be sure we did everything correctly without errors, let’s try installing the module! Reach the Modules page in the back office and look for New Fields Tutorial in the modules list, as shown below.Ĭlick install, and see if it works. Therefore, we call alterTable with ‘add’ when installing, and ‘remove’ when uninstalling. To avoid creating two functions, I stacked together the drop and add statements, passing a $method variable to determine what the function should do. Notice how I decided to structure the alterTable method. 2- Alter the product_lang table, to add the new field.1- As with every module, call the parent’s install method.'product_lang DROP COLUMN `custom_field`' Įxplanation: A couple of things here! First, in the install method, we 'product_lang ADD `custom_field` TEXT NOT NULL' If (!parent::uninstall() OR !$this->alterTable('remove')) !$this->registerHook('displayAdminProductsExtra')) !$this->registerHook('actionProductUpdate') OR !$this->registerHook('actionAdminControllerSetMedia') OR Let’s add a new column to product_lang with an ALTER TABLE, and provide a way to remove it if this module is uninstalled: Now that the basic structure is laid out, we want to specify the install and uninstall functions. ![]() $this->description = $this->l('Test module from Nemo\'s Post Scriptum Tutorial ().') $this->displayName = $this->l('New Fields Tutorial') Then open up newfieldstut.php and start by adding some regular module’s code. Drop the newfieldstut folder (the one from the “Start here” archive!) in the modules/ folder of your Prestashop Root. You can download the blank module folder from the link at the beginning of the article. ![]() We will create a new field for demonstrational purposes only, but feel free to dare beyond the limits of this tutorial if you want!įirst of all, let’s start with a simple module template. Then, we will see how to overcome a little issue with translatable fields, using the ActionAdminControllerSetMedia hook, and lastly, on data submission, we will update the new custom field once the product is updated. By plugging code snippets into this hook, we can easily update the column when the product itself is updated.Īt this point, we can line out our plan of action: after giving the module a basic setup, we will first create the template file that will display the new field’s input box on its own table. Specifically, we will add a new column (custom_field) to the product_lang table and add a new translatable field, without using overrides at all. This is called for every Prestashop Admin page, thus we will need to prevent it from running in pages that are not the product’s configuration.ĪctionProductUpdate: the new field needs to be saved in the database. Specifically, we will be using the following ones:ĭisplayAdminProductsExtra: this hook adds new tabs to the product’s back office interface, thus, we will use this hook to plug in our new content’s input field with translationsĪctionAdminControllerSetMedia: if we ever need to style our new tab, or add custom javascript functions, this is the ideal hook to use to plug css and js files. Now, in order to add new fields to Prestashop’s products’ back office, it is essential to be aware of the hooks that can be used to perform various operations in the admin panel. If you are totally new to Prestashop, I strongly recommend that you read the Official documentation on how to create a Prestashop Module before approaching this tutorial. However, by using a module and a new tab, it becomes much more maintainable!ĭownload Project Files Prestashop back office hooksĪlthough we will go through the basics of setting up a module, this tutorial assumes you already know how to create one from scratch. You can use flexbox to change the order of these boxes on your desired screen resolutions, however I believe it has no support for lower than IE9, but if you dont have to support older browsers, check out the demo here.It is easy enough to modify Prestashop’s products’ back office and add new fields by editing the AdminProductsController. If you have to maintain a strict order on your columns, like: Small box ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |