Developer Tips: How to Make Pagination for Product Comments in PrestaShop

August 22, 2012 Alex Simonchik Prestashop
Developer Tips: How to Make Pagination for Product Comments in PrestaShop

Have you ever used a standard PrestaShop Product Comments module? This module allows website visitors to post comments about products.

But this module has a very essential drawback: it lacks of pagination for product comments. To show how it works, let’s write a simple script that will create 100 comments for the first product. (Read the entire post. Below we’ve hidden a free module that enables pagination for product comments…)

We name file autoComment.php and put it into root PrestaShop category. When you click the link http://YOUR_STORE/autoComment.php, you will see 100 newly created fake comments for the first product.

Take a look at a scroll bar on the right: there is no pagination.

Let’s start with finding the hook that is responsible for displaying comments. This is hookProductTabContent hook with the code:

We are focused on performance of ProductComment::getByProduct function, because it returns all comments that are related to a product. Open ProductComment.php  file, located in the module root, and look for the getByProduct function code:

Notice that the query has LIMIT parameter, but because optional parameters n and p are not transmitted, it is ignored when forming a line with sql-query. Well, then we should transmit them and for that purpose we create tpl-templates, containing the required logic, and put them in YOUR_STORE_DIR/themes/YOUR_THEME/modules/productcomments. This action will minimize changes in existing (original) module files as far as the file from a module folder is in a priority load.

We need to create several files: productcomments.tpl (modified standard template), pagination_comments.tpl (new template, responsible for pagination for comments) and ajax_load_content.tpl (it contains content, returned by ajax request).

Code of productcomments.tpl file:

In this file we have changed js-code that transfers ajax-request to the modules/productcomments/ajax_load controller with parameters p – the page number and id_product. Also we call pagination_comments.tpl {include file = “$ tpl_dir. /Modules/productcomments/pagination_comments.tpl”} in this file. Don’t forget to put pagination-loader.gif file into modules/productcomments /img folder, which will be displayed during the ajax-request.

The code of pagination_comments.tpl:

The most crucial points here are transfer of parameters in rel page numbers you are planning to move to and pagination_page_number class for elements, displaying page numbers to move to.

In our ajax-request there is a file that a standard module doesn’t have. It is ajax_load.php with the content:

The content of ajax_load_content.tpl:

Last thing we need to do is to add changes into hookProductTabContent() function in order to transfer n and  p parameters into query.

That’s it. Now your page with comments looks this way:

In the module sources we used the parameter, responsible for sorting direction. It is considered in the query while getting comments for a product (BELVG_COMMENTS_SORT_DIRECTION).

Based on explanations above, we made pagination_for_product_comments module that you may easily download. To install it successfully, you need to replace the productcomments folder, containing a standard module, and put the folder with changed templates into your theme.



8 comments

  1. Great work ! This should become a default prestashop behavior for product comments. Thanks !!

  2. Danni,

    Thanks for reading the article. It should be a default PrestaShop behavior for product comments, but unfortunately it’s not yet.

  3. I wrote this article for v1.4, but I think that you can do it on the analogy of this, because modules don’t really change from version to version.

  4. Thanks for sharing this one! It’s working great on previous version of PS. Just wondering, how to do this in prestashop 1.6? Im planning to update.

  5. Bobz,

    We already have certain plans about future articles, but we will definitely keep your suggestions in mind about optimizing this article for the version 1.6

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *