Latest Blog Posts

Editing the OpenCart Homepage
29.09.2025 2666 3 44 min

Editing the OpenCart homepage, customizing H1 and Title text, displaying products and modu..

How to Uninstall Powered by OpenCart
03.09.2025 4137 3 20 min

Remove Powered by OpenCart and customize footer: edit template, use theme editor or OCMOD ..

How to remove modules in OpenCart
28.08.2025 2117 3 12 min

Step-by-step instructions for deleting modules in OpenCart 3 via the admin panel, manually..

Which hosting to choose for OpenCart
25.08.2025 1436 3 19 min

Find out which hosting to choose for an OpenCart online store. Requirements, selection cri..

OpenCart modifier for updating exchange rates

2743 2 5 min
OpenCart modifier for updating exchange rates

To create a modifier in OpenCart to update the exchange rate, you can use the following steps:

Create a new file in the catalog/model/tool/ folder named currency.php .

Defining a Currency class that will contain a method for updating currency rates.

In the method, determine the URL from where you can get new exchange rates and send a request to the server to receive the data.

Parse the received response and update the exchange rates in the database.

Use the created method in the desired place in the code, for example, in the catalog/controller/common/header.php file, to update the exchange rates every time the site is opened.

An example of a modifier code for updating the exchange rate in OpenCart:

 <?php
class ModelToolCurrency extends Model {
 
 public function updateCurrencyRates() {
 $url = 'https://www.example.com/currency_rates.json'; // replace with real URL
 
 $data = file_get_contents($url);
 $json = json_decode($data, true);
 
 if (!$json) {
 return false;
 }
 
 foreach ($json as $code => $rate) {
 $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$rate . "' WHERE code = '" . $this->db->escape($code) . "'");
 }
 
 return true;
 }
 
}
?>

In this example, the updateCurrencyRates() method sends a GET request to the specified URL, receives a JSON response, and updates the values in the database for each currency. To use the method, you need to call it from the right place in the site code, for example, from the index() method in the catalog/controller/common/header.php file:

 public function index() {
 $this->load->model('tool/currency');
 $this->model_tool_currency->updateCurrencyRates();
 
 // rest of the code
}

The exchange rate will be updated every time the site page is loaded.

Now let’s look at an example of the code that will update the rate using the PrivatBank Ukraine API:

To update the exchange rate via the PrivatBank API, you can supplement the previous code as follows:

 <?php
class ModelToolCurrency extends Model {
 
 public function updateCurrencyRates() {
 $url = 'https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11';
 
 $data = file_get_contents($url);
 $json = json_decode($data, true);
 
 if (!$json) {
 return false;
 }
 
 foreach ($json as $currency) {
 $code = $currency['ccy'];
 $rate = $currency['buy'];
 $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$rate . "' WHERE code = '" . $this->db->escape($code) . "'");
 }
 
 return true;
 }
 
}
?>

In this example, the updateCurrencyRates() method sends a GET request to the PrivatBank API, receives a JSON response, and updates the values in the database for each currency. The request contains the following parameters: exchange - request for exchange rates, json - response format, coursid - rate ID (set to 11).

To use the method, you need to call it from the right place in the site code, for example, from the index() method in the catalog/controller/common/header.php file:

 public function index() {
 $this->load->model('tool/currency');
 $this->model_tool_currency->updateCurrencyRates();
 
 // rest of the code
}

The exchange rate will be updated every time the site page is loaded. Please note that to use the PrivatBank API you must obtain an API key from them.

In this case, you can reduce the rate update frequency to 1 time per hour as follows:

 <?php
class ModelToolCurrency extends Model {
 
 public function updateCurrencyRates() {
 $update_frequency = 3600; // time in seconds
 $last_update_time = $this->config->get('config_currency_last_updated');
 if (!$last_update_time || (time() - $last_update_time > $update_frequency)) {
 $url = 'https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11';
 
 $data = file_get_contents($url);
 $json = json_decode($data, true);
 
 if (!$json) {
 return false;
 }
 
 foreach ($json as $currency) {
 $code = $currency['ccy'];
 $rate = $currency['buy'];
 $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$rate . "' WHERE code = '" . $this->db->escape($code) . "'");
 }
 
 $this->config->set('config_currency_last_updated', time());
 
 return true;
 }
 
 return false;
 }
 
}
?>

This example adds variables to set the currency rate update frequency ( update_frequency ) and last update time ( config_currency_last_updated ), which are stored in the OpenCart configuration file. Each time the updateCurrencyRates() method is called, it checks to see if the currency rate has been updated in less than the specified time (update_frequency), and if not, the method exits without performing any further action. If enough time has passed, the method updates exchange rates, saves the update time and returns true.

This code represents a modifier for CMS OpenCart, which updates currency rates via the PrivatBank API and limits the update frequency to no more than 1 time per hour. The modifier consists of the updateCurrencyRates() method, which sends a GET request to the PrivatBank API, receives a JSON response and updates the values in the database for each currency, and also adds a check for the time of the last update of the currency rate and saving this time in the OpenCart configuration file. This modifier allows you to automatically maintain the current exchange rate on the site and reduces the number of requests to the PrivatBank API.

Products related to this post

Currency update through the National Bank of Moldova
699 грн.499 грн.
Yahoo API no longer provides a service to update currencies, so at the moment OpenCart does not automatically update the currency ..
Currency update through Privatbank
699 грн.499 грн.
Yahoo API no longer provides a service to update currencies, so at the moment OpenCart does not automatically update the currency ..
Currency update through the National Bank of Ukraine
699 грн.499 грн.
Yahoo has closed its API since 2017 and now does not provide up-to-date exchange rates, respectively, exchange rates are no longer..
ChatGPT Consultant

Comments

Leave your comment or question
Есть готовый модуль?
Answer Admin

Есть модификаторы для Обновление курса валют.

Для монобанка можете сделать?
Answer Admin

Да, можем.

Write Comment

Popular