Unleash the Full Potential of Your PrestaShop Store

Welcome to MyPresta.eu – Your reliable partner in the e-commerce world. Find innovative modules that will take your business to a new level.

PrestaShop product page visits counter

In this tutorial i want to show you how to create visits counter on each product page in your store based on PrestaShop engine. To achieve it we have to modify two things, FrontController and your theme template file. This feature uses default prestashop stats engine, so it mean, that it's necessary to turn on datamining module and enable all features for this addon (default module in your prestashop, you can turn on its features on module configuration page).

 

product page views count tutorial

 

Front Controller modification

As i said before it's necessary to modify the FrontController.php file. You can find it in this directory: classes/controllers/ Open this file and before last closing bracket paste this code:

public static function getTotalViewed($id_product){ 
  $view1 = Db::getInstance()->getRow('SELECT pv.counter AS total FROM '._DB_PREFIX_.'page_viewed pv 
  LEFT JOIN '._DB_PREFIX_.'page p ON pv.id_page = p.id_page 
  LEFT JOIN '._DB_PREFIX_.'page_type pt ON p.id_page_type = pt.id_page_type 
  WHERE pt.name = \'product\' AND p.`id_object` = '.intval($id_product).''); 
  return isset($view1['total']) ? $view1['total'] : 0; 
}

Main function of this code is possibility to get information about visits from datamining module database. This function returns number of visits for product, query is based on product id number.

 

Theme template file modification

Now we have to modify theme template file. In this case please open product.tpl file located in your theme directory. For default prestashop template path to file looks like: themes/default-bootstrap/product.tpl . All what we have to do now is just little code paste, to display product page views counter use just this code:

    {FrontController::getTotalViewed(Tools::getValue('id_product'))}

You can use it anywhere you want in product.tpl file. You can also stylize it with css styles like other elements. Voila ;)

Zdjęcie autora: Milosz Myszczuk

Artykuł napisany przez Milosza Myszczuka, eksperta PrestaShop i oficjalnego moderatora społeczności PrestaShop. CEO i założyciel agencji interaktywnej VEKIA. Dowiedz się więcej.

If you like this article, support our work!

Comments