Sunday, January 9, 2011

PrestaShop Tips - How to duplicate a existing module?

The PrestaShop modular features are the one of the best features I think. Most functionality can be developed separately and independently. And further more, it is open for any contributors who would like to contribute to the community.

Sometime, store owners want to duplicate some the modules like Advertiser block module, home featured module so the they can use more than one in the application.

Here I explain in detailed instruction on how to duplicate a module with examples.

Steps to duplicate a module: (use Home Featured module as an example)
1. Copy the existing module and rename to it for new name, for example
From: homefeatured to:homefeatured1

2. Change related file name to a different ones. for example
From: homefeatured.php To: homefeatured1.php
Ffom: homefeatured.tpl To:homefeatured1.tpl

3. Change the class name of copied module class to HomeFeatured1


class HomeFeatured1 extends Module


4. Change the modules name in the class constructor to a different one, for example

$this->name = 'homefeatured1';


5. If don not want to share the parameters, you have to change the parameter name
for example, HOME_FEATURED_NBR1


Configuration::updateValue('HOME_FEATURED_NBR1', intval($nbr));


6. Change new module to use new UI(tpl) file homefeatured1.tpl file

return $this->display(__FILE__, 'homefeatured1.tpl');


7. (for Prestashop 1.4 or higher), change the module name,and tab name in configre file config.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>blockadvertising1</name>
<displayName>Block advertising123</displayName>
<version>0.1</version>
<description>Adds a block to display an advertisement123</description>
<tab>advertising_marketing</tab>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
</module>




8. If the module load data from database, you need to change the part that load data.
This code usually in HookXXXXX() function. For example, HookHome, HookLeft, HookRight.

For Home Featured module are getting data from product category “Home”.
If you want load completely different data, you need use another category and for the 2nd module, load data from different category. Change the following “1” to your new category id, for example, 111.

code before change

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, ($nb ? $nb : 10));


code after change

$products = $category->getProducts(intval($params['cookie']->id_lang), 1111, ($nb ? $nb : 10));



-----------------------------------------------------
Looking for stable/bug-less and more features Paypal payment module for your PrestaShop? I recommend Agile Paypal Express Checkout.




Agile Paypal is a Paypal Express Checkout module for PrestaShop. It is very stable and bug-less PrestaShop Paypal module. Among many other features, Express Checkout that allow customer checkout without sign up/log in. This module will redirect customer to Paypal payment page in less than 3 clicks. See more detail here http://addons-modules.com/
-----------------------------------------------------

29 comments:

Anonymous said...

very useful post, thanks a lot! i was strugguling to add a new add to my site, and tahnks to your tips i succed to duplicate the advertising block.

Alvin said...

glad to know it helped.
and thanks for your comment.

Actually, there is a little bit changes at the new version of PrestaShop 1.4 (currently in RC3).

I have updated this post to latest so it should also work for latest version.

Muhammad Azeem said...

This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..

c#, dot.net, php tutorial

Thanks a lot..!

Leszek said...

good one,
Thank you.

Leszek said...

good one!t
thank you

Anonymous said...

this is really what i need. thanx bro.
Ahmad, Jakarta, Indonesia

Florina said...

Hi there!
It works to duplicate the module.
However, if I want to load data from another category it stops working. It shows the module and the following line: "No featured products"

Is there anything else I should do?

Alvin said...

I think you there is a problem inside hookHome() where you call a function to get your product list. Check that the function return correct product list as expected.

juan-capristan said...

Thanks a lot for your post. It worked fine for me, with prestashop 1.4.5. I would only add that It's also important to change $this->displayName if you want to be able to distinguish easily among de different versions of the modules.

Anonymous said...

Hi, and thank you for this super helpful post. I *almost* succeeded in duplicating the block advertising module. The image and text seem to be appearing ok. However, it doesn't seem to be able to link to a URL. The other advertising module is working fine. Do you know what I could be doing wrong? Thanks!!

Alvin said...

please make sure you have changed the configuration variable name and you saved and retrieved from the correctly one for each module.


Here is example: Configuration::updateValue('BLOCKADVERT_LINK', 'http://www.prestashop.com');

Anonymous said...

Hi Alvin,
Thanks for response. I have changed all instances of BLOCKADVERT_LINK to BLOCKADVERT_LINK2, and the link is still not working.

Anonymous said...

fantastic post, easy to understand, have got an issue thought the title for image is the same in all blocks anyone knows how to edit this please?

Anonymous said...

Hi Alvin,
Thanks for the response. I already made sure all instances of BLOCKADVERT_LINK were changed to BLOCKADVERT_LINK2. The link still doesn't work. It's strange. The first block advertising works perfectly. On the second one, the image is fine, the text is fine. But the link does not work. For some strange reason, if I insert a horizontal line in there, it will link (just the line though). But image link doesn't work. I hope you can help!

Alvin said...

the only thing i can image is that something wrong with the theme file (tpl), such as tag mismatched. Some tag is not closed.

Try to copy the tpl file from the one that it works, then make necessary again to see if it solve your problem.

Anonymous said...

Hi Allen, thanks again for your help. I tried your suggestion, and the link still doesn't work - it's really strange!
The weird thing is, on the module that works, I can click on the image, right click and save, etc. On the module that doesn't work, I'm not able to do this.

I'm not sure you can help me on this, but it's making me tear my hair out!

Thanks again.

Alvin said...

if you can share the URL, maybe I can have look at when I got a chance

Rekha said...

I have same problem as Florina in 1.4.5.1 when I select another category number, he module says 'No featured products". What is the fix Alvin?

Alvin said...

make sure you have used the correct category ID and you have products in that category.

Also there will be two places you need to replace the number "1" as showing below.


$category = new Category(1, Configuration::get('PS_LANG_DEFAULT'));

$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));

$products = $category->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));

Anonymous said...

I duplicated the whole module folder and renamed folder, files, and inside files as you outlined, but the new module name does not list under the Modules tab of the prestashop admin panel. version 1.4.9.

I did not see anything mentioned about adding the module to prestashop. Is it suppose to show up by itself once the duplication you described is done?
So far it has not.

Any other steps needed to make it show up in the modules list of pretashop?

Alvin said...


>I did not see anything mentioned about adding the module to prestashop.
>Is it suppose to show up by itself once the duplication you described is done?

Yes, it should show up automatically if there is no errors. I guess the copied module still have some errors. Make sure you have rename the module class name to the same name of folder.

Try to turn on the error display to see if there is any error. Set "display_errors" to "on" in file /config/config.inc.php


Yang said...

I tried to duplicate the module homefeatured but it show no products in the homepage. Instead I am getting the msg : No featured products. I have followed exactly as your tutorial, here is a part pf the file modified :

function hookHome($params)
{
global $smarty;

$category = new Category(50, Configuration::get('PS_LANG_DEFAULT'));
$nb = (int)(Configuration::get('HOME_FEATURED_NBR1'));
$products = $category->getProducts((int)($params['cookie']->id_lang), 50, ($nb ? $nb : 10));
$smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'homeSize' => Image::getSize('home')));

return $this->display(__FILE__, 'homefeatured1.tpl');
}
}

Alvin said...

You use wrong category ID.

$category = new Category(50, Configuration::get('PS_LANG_DEFAULT'));


Home featured is only on Home category which ID is 1.
You need to change 50 to 1.

Yang said...

Yes, I know, at first it was set to 1 for the category field but still getting the same msg so I have changed it.

Anyway today I have writed again 1 for category field, uploaded file, uninstall and install again but still getting the msg that there is no products.

Below is the homefeatured1.php file in TXT, if you could have a look ?

http://textuploader.com/?p=6&id=8y6gP

colin said...

Hi Alvin,

Very nice and useful post. Following the guidelines I was able to duplicate home featured module.
Still few things that not working for me:
I am not able to get products in this new module for all categories that I have defined. For some of them it works, for some it don't. And, yes, I do have products in all of my categories.
I also have a problem with translations. Even if I have define them for the duplicated module, they are not showing.

I got confused: you have reply to two of the comments, two things that for me seems to be opposite:

August 11, 2012 6:04 AM:
"make sure you have used the correct category ID and you have products in that category.
Also there will be two places you need to replace the number "1" as showing below.
$category = new Category(1, Configuration::get('PS_LANG_DEFAULT'));
$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
$products = $category->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));"

October 22, 2012 3:19 PM
"You use wrong category ID.
$category = new Category(50, Configuration::get('PS_LANG_DEFAULT'));
Home featured is only on Home category which ID is 1.
You need to change 50 to 1.
"

Is this prestashop version dependent? I am using 1.4.8.2 version.

colin said...

Hi Alvin,

I have manage to solve translation issue in my previous comment. I would like to share my solution. It requires to change "mod" value to new module name in .tpl file. Eg.
mod='homefeatured' becomes mod='myhomefeatured'

Still looking forward for a solution to get products in the module no matter what category id I use...

Alvin said...

Hi Colin
Thanks for your comment.

Yes you are right, for the text to be able to translatable at back office, you need put mod='modulename'.

For the category, any category should be OK as long as you put the products on that category.

If you do have products in that category but it does not hit, then you need to debug the category->getProducts() function to see what it wrong.

Please note:
make sure the category is visible to the current user (loged in user) or visitor. The access is controlled by Customer Group.

Colin said...

Hi Alvin,

Finally manage to solve category issue. Solution is simple and needed a break to clear my mind from this issue. There is only one place that needs to be changed and this is first parameter, representing category id, in following line:

$category = new Category(1, Configuration::get('PS_LANG_DEFAULT'));

This line should not be changed:

$products = $category->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));

Another thing that I have noticed is that category should not be enabled.

Hope this is helpful for other Prestashop fans.

Anonymous said...

Very nice indeed...
THANK YOU!

Waldo