Opencart currency refresh not working

Best Solution for Opencart currency refresh not working problem

Opencart multi currency and How it works

Opencart have a build in currency converter and it will allow users to pay with multiple currencies. How ever recently i identified that opencart currency refresh not working. From more investigations then i found that Opencart currency converter uses Yahoo Finance service and it will fetch currency rates via a csv also the currency.php file will run daily via a cron job.

Opencart Yahoo finance service stopped  now and The Solution!

Unfortunately now yahoo finance service has stopped now moreover they have given a notification to users this due to the un-ethical usage of there service. If this is the case we have to use a other financial service which can do the same job. So i thought of using google finance converter to refresh currency in Opencart. Then i changed the refresh function in that file and this worked well and you can find the changed code for the currency.php



Step 1

  • Go to
    public_html/admin/model/localisation
  • Open file from any editor
    currency.php
  • Replace the below code
<?php
class ModelLocalisationCurrency extends Model {
    public function addCurrency($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "currency SET title = '" . $this->db->escape($data['title']) . "', code = '" . $this->db->escape($data['code']) . "', symbol_left = '" . $this->db->escape($data['symbol_left']) . "', symbol_right = '" . $this->db->escape($data['symbol_right']) . "', decimal_place = '" . $this->db->escape($data['decimal_place']) . "', value = '" . $this->db->escape($data['value']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW()");

        $currency_id = $this->db->getLastId();

        if ($this->config->get('config_currency_auto')) {
            $this->refresh(true);
        }

        $this->cache->delete('currency');
        
        return $currency_id;
    }

    public function editCurrency($currency_id, $data) {
        $this->db->query("UPDATE " . DB_PREFIX . "currency SET title = '" . $this->db->escape($data['title']) . "', code = '" . $this->db->escape($data['code']) . "', symbol_left = '" . $this->db->escape($data['symbol_left']) . "', symbol_right = '" . $this->db->escape($data['symbol_right']) . "', decimal_place = '" . $this->db->escape($data['decimal_place']) . "', value = '" . $this->db->escape($data['value']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE currency_id = '" . (int)$currency_id . "'");

        $this->cache->delete('currency');
    }

    public function deleteCurrency($currency_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "currency WHERE currency_id = '" . (int)$currency_id . "'");

        $this->cache->delete('currency');
    }

    public function getCurrency($currency_id) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "currency WHERE currency_id = '" . (int)$currency_id . "'");

        return $query->row;
    }

    public function getCurrencyByCode($currency) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "currency WHERE code = '" . $this->db->escape($currency) . "'");

        return $query->row;
    }

    public function getCurrencies($data = array()) {
        if ($data) {
            $sql = "SELECT * FROM " . DB_PREFIX . "currency";

            $sort_data = array(
                'title',
                'code',
                'value',
                'date_modified'
            );

            if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
                $sql .= " ORDER BY " . $data['sort'];
            } else {
                $sql .= " ORDER BY title";
            }

            if (isset($data['order']) && ($data['order'] == 'DESC')) {
                $sql .= " DESC";
            } else {
                $sql .= " ASC";
            }

            if (isset($data['start']) || isset($data['limit'])) {
                if ($data['start'] < 0) {
                    $data['start'] = 0;
                }

                if ($data['limit'] < 1) {
                    $data['limit'] = 20;
                }

                $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
            }

            $query = $this->db->query($sql);

            return $query->rows;
        } else {
            $currency_data = $this->cache->get('currency');

            if (!$currency_data) {
                $currency_data = array();

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

                foreach ($query->rows as $result) {
                    $currency_data[$result['code']] = array(
                        'currency_id'   => $result['currency_id'],
                        'title'         => $result['title'],
                        'code'          => $result['code'],
                        'symbol_left'   => $result['symbol_left'],
                        'symbol_right'  => $result['symbol_right'],
                        'decimal_place' => $result['decimal_place'],
                        'value'         => $result['value'],
                        'status'        => $result['status'],
                        'date_modified' => $result['date_modified']
                    );
                }

                $this->cache->set('currency', $currency_data);
            }

            return $currency_data;
        }
    }

    public function refresh($force = false) {
        $currency_data = array();

        if ($force) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "'");
        } else {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' AND date_modified < '" .  $this->db->escape(date('Y-m-d H:i:s', strtotime('-1 day'))) . "'");
        }

        foreach ($query->rows as $result) {
            $currency_data[] = $this->config->get('config_currency') . $result['code'] . '=X';
            $currency_data[] = $result['code'] . $this->config->get('config_currency') . '=X';
        }

    
        foreach($query->rows as $_row)
        {
            $amount = 1;
            $cmount = $this->get_currency($this->config->get('config_currency'), $_row['code'], $amount);
            $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$cmount . "', date_modified = '" .  $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($_row['code']) . "'");
        }
        //$this->get_currency($from_Currency, $to_Currency, $amount);

        /*$line = explode("\n", trim($content));

        for ($i = 0; $i < count($line); $i = $i + 2) {
            $currency = utf8_substr($line[$i], 4, 3);
            $value = utf8_substr($line[$i], 11, 6);
            
            if ((float)$value < 1 && isset($line[$i + 1])) {
                $value = (1 / utf8_substr($line[$i + 1], 11, 6));
            }    
                        
            if ((float)$value) {
                $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$value . "', date_modified = '" .  $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($currency) . "'");
            }
        }
        */
        $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '1.00000', date_modified = '" .  $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($this->config->get('config_currency')) . "'");

        $this->cache->delete('currency');
    }

    public function getTotalCurrencies() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "currency");

        return $query->row['total'];
    }

    public function get_currency($from_Currency, $to_Currency, $amount) 
    {
        $amount = urlencode($amount);
        $from_Currency = urlencode($from_Currency);
        $to_Currency = urlencode($to_Currency);

        $url = "https://finance.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency";
        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt ($ch, CURLOPT_USERAGENT,
                 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $rawdata = curl_exec($ch);
        curl_close($ch);
        $data = explode('bld>', $rawdata);
        $data = explode($to_Currency, $data[1]);

        return round($data[0], 2);
    }
}

Step 2

Moreover Refresh the modification and try refresh currency values. It Should work and opencart currency refresh not working issue should be solved..

Note: If you are not confident please make a back up your currency.php code before make changes to the files.

You can find more fixes to the open cart from my site. Check Issues Errors and Bugs category.
Don’t forget a add a comment in here, if this worked..

More Helpful Information

2 Replies to “Opencart currency refresh not working”

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × 1 =

16 − 8 =