Welcome to MyPresta.eu – Your reliable partner in the e-commerce world. Find innovative modules that will take your business to a new level.
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).

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 ;)
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!

Automatic Bank of Spain (Banco de España - BdE) currency rates integrator. The module guarant...
19.99 €

The Currency Exchange Module offers unparalleled flexibility in managing international sales. Regard...
19.99 €

Integrate your store with the Central Bank of the Republic of Turkey (TCMB) using this module. It au...
19.99 €

Automatic National Bank of Ukraine (NBU) currency rates integrator. The module guarantees precise pr...
19.99 €