How to duplicate PrestaShop module


There are situations in which a copy of a given module could be useful. Imagine that we need to display additional products on the home page. You can try to write an additional extension, but you can also copy and adjust the existing module.

You make all changes and modifications at your own risk. If you need a consultation with a PrestaShop specialist, contact us.

In this tutorial we will copy the module featured products. With a duplicate, we'll show more products on the main page.

Duplicate PrestaShop module

The copying method can be transferred to other modules. The steps we will take will be as follows:
1.Copying the module
2.Changing the basic parameters of the PrestaShop module
3.Installation

The first step is to copy the module files. Using an ftp client (eg filezilla), go to the modules directory and download the entire ps_featuredproducts directory, then change its name to ps_featuredproducts_2. The same is done with the php file which name is the same as the initial module name. The file is to be named ps_featuredproducts_2.php

Duplicate PrestaShop module

We are opening the php file for editing, and made several changes. First, we change the name of the class, the name of the module ($ this-> name), the display name ($ this-> displayName) and the path to the view file:

class Ps_FeaturedProducts_2 extends Module implements WidgetInterface
{
    private $templateFile;

    public function __construct()
    {
        $this->name = 'ps_featuredproducts_2';
        ...
        $this->displayName = $this->trans('Featured products 2',...
        ...
        $this->templateFile = 'module:ps_featuredproducts_2/...



Now we are looking for all values that are saved to the database. With this module, there is no need to change the table name in the database because it only uses the universal configuration table. 

The code contains three "keys" which values ​​change in the above-mentioned table. These are HOME_FEATURED_CAT, HOME_FEATURED_NBR and HOME_FEATURED_RANDOMIZE. The easiest way will be to change them using the substitution tool, which is often available after pressing the Ctrl + h key combination. For ease of use, change the HOME_FEATURED string to HOME_FEATURED_2

Duplicate PrestaShop module

If the Ctrl + h command does not work for some reason, manually replace all searches of the HOME_FEATURED string with HOME_FEATURED_2.

The next step is replacing parameters in the translating function. We replace the Modules.Featuredproducts with Modules.Featuredproducts2. We must also do this modification in the view file. Therefore, in addition to editing the ps_featuredproducts_2.php file, we also edit ps_featuredproducts_2 \ views \ templates \ hook \ ps_featuredproducts.tpl

Duplicate PrestaShop module

In the above-mentioned file, you can also change the name of the section. E.g. from Our Products to Best Products.

In order to set up the way it was done in the default module, it is worth to add classes to the section and h1 elements and change the bottom link:

<section class="featured-products clearfix">
  <h1 class="h1 products-section-title text-uppercase">{l s='Best Products' d='Modules.Featuredproducts2.Shop'}</h1>
…
<a class="all-product-link float-xs-left float-md-right h4" href="{$allProductsLink}">{l s='All products' d='Modules.Featuredproducts2.Shop'}<i class="material-icons"></i></a>


Now save edited files. If you have not made any changes on the server, then move the entire ps_featuredproducts_2 directory to the modules folder on the server.

The next stage is to install module in the administration panel.

Duplicate PrestaShop module

We can also change modules display order on the main page  by changing its position in the administration panel.

Duplicate PrestaShop module

Nothing prevents you to duplicate the module once more by replacing the „two” with the next digit.

 

In order to ensure maximum convenience to users when using the website, this page uses cookie files. Detailed information is available in our Privacy Policy. Click " I agree", so that this information is no longer shown