Get Free Audit

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

Aug 22, 2012

4844 Alex Simonchik

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.

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.

modules prestashop belvg

PrestaShop Modules

Take your store to the next level with PrestaShop modules from BelVG

Visit the store

The code of pagination_comments.tpl:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

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:


ecommerce development

Ecommerce Development

Take your online store to the next level with BelVG Ecommerce Development

Visit the page

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.

development_prestashop

PrestaShop Development

Take your online store to the next level with BelVG PrestaShop development

Visit the page
Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

9 Comments

  1. 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

  2. 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.

  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. Danni,

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

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

Post a new comment

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