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.

















































Comments