Search code examples
prestashopgeoipsmarty3prestashop-1.5

Geoip in prestashop smarty template


I need to work with visitor country code in product.tpl. Is there any prestashop global variable with country code, which I can use in smarty template? If not, where I can add it?


Solution

  • To do this first go to Preferences>>Geolocation and enable it also download .dat file

    after this add this code in controllers >> front >> ProductController.php before line 238

    include_once(_PS_GEOIP_DIR_.'geoipcity.inc');
    $gi = geoip_open(realpath(_PS_GEOIP_DIR_.'GeoLiteCity.dat'), GEOIP_STANDARD);
    $record = geoip_record_by_addr($gi, Tools::getRemoteAddr()); 
    

    add this code in smarty assign around line 260

    'country_name' => $record->country_name //change country_name to code etc
    

    Use $country_name in product.tpl where ever you want :)

    tested in ps-1.5.6