Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Opencart location based auto currency change Tutorial

Opencart location based auto currency change Tutorial

Opencart is now one of world most famous free shopping cart system extensively used make ecommerce website. But sometime when we are developing a ecommerce website we have to face some challenges, and for this we have depend on opencart extension or if your problem not solved by extension then you have to fixed. Recently One of my client’s requirement was when a visitor come from Country India Currency will be automatically set to currency INR otherwise Currency $.

Basically we don’t get enough time to develop a plugin for this so have to purchase a extension auto IP based currency changer extension.But the extension failed to detect Indian IP , if we send ip details to the IP API provider they never update the correct details, so I later inspected the issue and find they never used api they used a SQL file so its not going to update regulary.

I had very tight timeline so I have done some googling and developed my own script and its work better. so in this post I am going share that code.

Steps for IP based Currency change

Create currency INR, if you don’t how then google it or follow this tutorial here.
Deactive all currency except $ and INR and set INR as default currency.

Next take a backup of file currency.php located at system/library/cart/ (Tested on Opencart 2.X version) and save at safe location. Now Create a file currency.php at system/library/cart/ with code given below.

 $this->db = $registry->get('db');
$this->language = $registry->get('language');
$this->request = $registry->get('request');
$this->session = $registry->get('session');

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency");

foreach ($query->rows as $result) {
$this->currencies[$result['code']] = array(
'currency_id' => $result['currency_id'],
'title' => $result['title'],
'symbol_left' => $result['symbol_left'],
'symbol_right' => $result['symbol_right'],
'decimal_place' => $result['decimal_place'],
'value' => $result['value']
);
}
$countrycode = $this->get_client_ip();
//var_dump($this->request);
//echo "Get ".$this->request->get['code']."
";
//echo "Post ".$this->request->post['code']."
";
//echo "Session ".$this->session->data['currency']."
";
//echo "Cookie ".$this->request->cookie['currency']."
";

if (isset($this->request->get['code']) && (array_key_exists($this->request->get['code'], $this->currencies))) {
$this->session->data['currency']=$this->request->get['code'];
}
elseif (isset($this->request->post['code']) && (array_key_exists($this->request->post['code'], $this->currencies))) {
unset($this->session->data['currency']);
$this->session->data['currency']=$this->request->post['code'];
}
elseif ((isset($this->request->cookie['currency'])) && (array_key_exists($this->request->cookie['currency'], $this->currencies))) {
$this->session->data['currency']=$this->session->data['currency'];
}
// elseif ((isset($this->session->data['currency'])) && (array_key_exists($this->session->data['currency'], $this->currencies))) {
// $this->session->data['currency']=$this->session->data['currency'];
// }
else
{

if($countrycode)
{

if($countrycode == 'IN')
{
$this->session->data['currency']='INR';
}
else
{
$this->session->data['currency']='USD';
}
}
else
{

$this->session->data['currency']='INR';
}
/* Modification end here */
}

}

public function get_client_ip() {
$ipaddress='';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';

$fetch=unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ipaddress));
return $fetch['geoplugin_countryCode'];
}

public function format($number, $currency, $value = '', $format = true) {

$symbol_left = $this->currencies[$currency]['symbol_left'];
$symbol_right = $this->currencies[$currency]['symbol_right'];
$decimal_place = $this->currencies[$currency]['decimal_place'];

if (!$value) {

$value = $this->currencies[$currency]['value'];

}

$amount = $value ? (float)$number * $value : (float)$number;

$amount = round($amount, (int)$decimal_place);

if (!$format) {

return $amount;

}

$string = '';

if ($symbol_left) {

$string .= $symbol_left;

}

$string .= number_format($amount, (int)$decimal_place, $this->language->get('decimal_point'), $this->language->get('thousand_point'));

if ($symbol_right) {

$string .= $symbol_right;

}

return $string;

}

public function convert($value, $from, $to) {

if (isset($this->currencies[$from])) {

$from = $this->currencies[$from]['value'];

} else {

$from = 1;

}

if (isset($this->currencies[$to])) {

$to = $this->currencies[$to]['value'];

} else {

$to = 1;

}

return $value * ($to / $from);

}

public function getId($currency) {

if (isset($this->currencies[$currency])) {

return $this->currencies[$currency]['currency_id'];

} else {

return 0;

}

}

public function getSymbolLeft($currency) {

if (isset($this->currencies[$currency])) {

return $this->currencies[$currency]['symbol_left'];

} else {

return '';

}

}

public function getSymbolRight($currency) {

if (isset($this->currencies[$currency])) {

return $this->currencies[$currency]['symbol_right'];

} else {

return '';

}

}

public function getDecimalPlace($currency) {

if (isset($this->currencies[$currency])) {

return $this->currencies[$currency]['decimal_place'];

} else {

return 0;

}

}

public function getValue($currency) {

if (isset($this->currencies[$currency])) {

return $this->currencies[$currency]['value'];

} else {

return 0;

}

}

public function has($currency) {

return isset($this->currencies[$currency]);

}

}

Do you have any Bugs, Errors, Issues or want to customize your Opencart Website or want to develop new Opencart based Website ?
I am opencart expert with 5+ years of experience, also developed custom theme and opencart extension. You can outsource you project to us on Fiverr just click the link below
https://www.fiverr.com/share/a76EPG