Comment créer un onglet personnalisé dans la page produit du back-office de PrestaShop

Comment créer un onglet personnalisé dans la page produit du back-office de PrestaShop

PrestaShop, with its powerful and flexible framework, allows developers to customize various aspects of the platform to meet specific business needs. One such customization is adding a custom tab in the product page of the back office. In this blog, we will walk through the step-by-step process to achieve this by creating a sample module called kbcustommodule.

We can create the custom tab in the back office product page in two ways:

  1. Using Symfony components.
  2. Using the displayAdminProductsExtra hook

Method 1:

This tutorial is applicable to PrestaShop version 8.1 and above, using Symfony components.

Step 1: Create your module

kbcustommodule/ |– composer.json |– config/ | |– services.yml |– src/ | |– Form/ | | |– Modifier/ | | | |– ProductFormModifier.php | | |– Type/ | | | |– CustomTabType.php |– kbcustommodule.php Start by creating a folder named kbcustommodule under the modules directory of your PrestaShop installation. In this folder, create the kbcustommodule.php file, which will contain the main class of your module. The module class initializes the module and registers the required hooks. Here is an example of the kdcustommodule.php file:

Create a custom tab in the back-office product page in PrestaShop-Step-1-Create-your-module

Run the composer dump-autoload command in your module’s root directory to enable namespace autoloading.

Step 2: Define the services

Create a services.yml file under your module’s config directory (kbcustommodule/config/services.yml). This file defines the Symfony services needed to customize the form.

Create a custom tab in the back office product page in PrestaShop-Step 2-Define services

Step 3: Create the Custom Tabbed Form Type

Create a CustomTabType class in the src/Form/Type folder of your module. This class defines the fields that will appear in the custom tab.

Create a custom tab in the back office product page in PrestaShop - Step 3 - Create the custom tab form type

Step 4: Create the ProductForm classEdit

The ProductFormModifier class defines how the custom tab integrates with the existing product form.

Create a custom tab in the back-office product page in PrestaShop-Step 4-Create-the-ProductForm-classEdit

Final step: install and test

  1. Install the module via the back office or manually place it in the modules directory and activate it.
  2. Navigate to the product page in the back office. You should see a new tab called “Custom Tab” with a field called “Custom Field”.
  3. Test the functionality by adding data to the custom field and ensuring it is saved correctly.

After successfully installing the above module, you will get the following result.

Google Tag Manager for Prestashop

Integrate Google Tag Manager into Prestashop

SHOP

Method 2:

The displayAdminProductsExtra hook allows you to add custom content or tabs to the product edit page in the Back Office. This method avoids the complexity of defining Symfony services and is easier for developers who are not familiar with Symfony.

Step 1: Create the module

Start by creating a basic module structure. For example: Kbcustommodule/ |– config/ |– translations/ |– views/ | |– templates/ | |– admin/ | |– products/ | |– custom_tab.tpl |– Kbcustommodule.php

Step 2: Define the module class

In the Kbcustommodule.php file, define your module and register the displayAdminProductsExtra hook.

 

Create a custom tab in the back-office product page in PrestaShop - Step 2 - Define the module class

Step 3: Create the template file

In the views/templates/admin/products/custom_tab.tpl file, define the HTML code and content of your custom tab.

Create a custom tab in the back-office product page in PrestaShop - Step 3 - Create the template file

Step 4: Backup Data

To save custom data, use the actionAdminControllerSetMedia hook with JavaScript or PHP. You can also use the actionProductUpdate hook in PHP. Example in Kbcustommodule.php:

Create a custom tab in the back office product page in PrestaShop - Step 4 - Save data

Step 5: Add the field to the database

Modify the database to store your custom data. For example: ———————————————————————— ALTER TABLE ps_product ADD custom_field TEXT NULL; ————————————————————————– After successfully installing the above module. You will get the following result For further assistance, feel free to contact us or leave a comment below. You can also contact us on Knowband Help Desk .

Gabriel

We are professionals in the eCommerce industry, specializing in plugin development. With years of experience, We provide the best plugins for platforms like - Magento, Prestashop, OpenCart and Shopify. We also offer custom module development and customization services for websites and modules. Our team remains at the forefront of industry trends, delivering cutting-edge solutions to enhance eCommerce website functionality. With a customer-centric approach, our company is trusted partner for eCommerce plugin solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *