Monday 15 July 2013

How to display facebook likes count on each prestashop page?

In this short tutorial i will show you how to easily display  Facebook likes count for each page in your prestashop store. The most important thing in this case is fact, that i will not use any external plugin from Facebook – just one simple request to Facebook graph api.


like-facebook-png


What you need?


A little knowledge about FTP service (to upload changes to the server), no programming skills needed, but of course it will be nice if you know what the PHP is and how Prestashop smarty works.


 


Okay, so for the first I have to edit PrestaShop controller – FrontController.  We will not change the code – we will add one simple function to it. So don’t worry, everything will be fine :)


 


Edit the PrestaShop FrontController


open the file: classes/controllers/FrontController.php   -  into the class body – put code that i pasted below:





public static function fbcount(){


  $ch = curl_init("http://graph.facebook.com/?ids=http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]);


  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


  $raw = curl_exec($ch); curl_close($ch);


  $data = json_decode($raw);


  if (isset($data->likes)){


    return $data->likes;


  } else {


    if (isset($data->shares)){


      return $data->shares;


    } else {


      return "0";


    }


  }

}



Code that i pasted above is a static front controller function. Function returns number of likes / shares from page, where you will use it. As I said before this script use facebook graph api to get the number of likes or shares. Now you probably want to know how to use this function? It’s very easy. You can use it in each .tpl file in your store.


 


For example you can use footer.tpl file located in your theme directory. Just use this code:


{FrontController::fbcount()}

anywhere you want. When you will use it – you will see the number of likes for page that you’re browsing in your store. Remember that you can use this code anywhere you want, for example in product.tpl, modules .tpl files etc. You can also stylize number (with css styles) to display the number of likes in an attractive form.





How to display facebook likes count on each prestashop page?

No comments:

Post a Comment