Question

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?

Was it helpful?

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

OTHER TIPS

I used @Raza's code to build it as an override to the FrontController with the purpose to not only display it in product.tpl but on all shop pages:

  1. in override > classes > controller > create file FrontController.php
  2. put following code

    class FrontController extends FrontControllerCore
    { 
        public function initContent()
        {
        parent::initContent();
    
        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()); 
    
        $this->context->smarty->assign('country_name', $record->country_name); //change country_name to code etc
    
        }
    }
    

@Raza: using {$country_name} in the .tpl-file displays the country name allways in English. Any idea how to use the PS's core translations to the country in the active shop language?

thanks

@nortonOn: You could try with id_lang from the cookie, this way, it should display the country name in the shop language. Should look like : $country->name[intval($this->context->cookie->id_lang)];

I hope it will help you :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top