Post by scottyboyyy » Mon Sep 02, 2024 11:05 pm

Hello,

I just checked error logs on one of my websites and it has GBs of this notice for undefined variable / index:

2024-09-02 15:01:59 - PHP Notice: Undefined variable: currency in /home/website/public_html/system/library/cart/currency.php on line 25
2024-09-02 15:01:59 - PHP Notice: Undefined index: in /home/website/public_html/system/library/cart/currency.php on line 25
2024-09-02 15:01:59 - PHP Notice: Undefined variable: currency in /home/website/public_html/system/library/cart/currency.php on line 26
2024-09-02 15:01:59 - PHP Notice: Undefined index: in /home/website/public_html/system/library/cart/currency.php on line 26
2024-09-02 15:01:59 - PHP Notice: Undefined variable: currency in /home/website/public_html/system/library/cart/currency.php on line 27
2024-09-02 15:01:59 - PHP Notice: Undefined index: in /home/website/public_html/system/library/cart/currency.php on line 27
2024-09-02 15:01:59 - PHP Notice: Undefined variable: currency in /home/website/public_html/system/library/cart/currency.php on line 30
2024-09-02 15:01:59 - PHP Notice: Undefined index: in /home/website/public_html/system/library/cart/currency.php on line 30

Any ideas what's causing this or what needs changed?

Thank you :)

Active Member

Posts

Joined
Fri Apr 07, 2017 2:36 am

Post by JNeuhoff » Tue Sep 03, 2024 12:20 am

Please read the forum rules, and then try again with some more details if you want some help here. Line 30 in the system/library/cart/currency.php is empty, hence you either are not using the latest OpenCart 3.0.4.0 version, or something else been modified.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by scottyboyyy » Tue Sep 03, 2024 12:58 am

Sorry! Version 3.0.3.6

Code: Select all

<?php
namespace Cart;
class Currency {
	private $currencies = array();

	public function __construct($registry) {
		$this->db = $registry->get('db');
		$this->language = $registry->get('language');

		$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']
			);
		}
	}

	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;
	}

	...
}

Active Member

Posts

Joined
Fri Apr 07, 2017 2:36 am

Post by by mona » Tue Sep 03, 2024 7:18 am

Did you delete USD ?
Do you have payment extensions ?

NB: error reporting should be turned off everywhere for live sites

Code: Select all

25              $symbol_left = $this->currencies[$currency]['symbol_left'];
26		$symbol_right = $this->currencies[$currency]['symbol_right'];
27		$decimal_place = $this->currencies[$currency]['decimal_place'];
28
29		if (!$value) {
30			$value = $this->currencies[$currency]['value'];
31		}

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by paulfeakins » Tue Sep 03, 2024 7:32 pm

scottyboyyy wrote:
Mon Sep 02, 2024 11:05 pm
error.log full of cart / currency issues
You need a code audit: https://www.antropy.co.uk/opencart-serv ... cal-audit/

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by nonnedelectari » Thu Sep 05, 2024 10:00 am

scottyboyyy wrote:
Tue Sep 03, 2024 12:58 am
Sorry! Version 3.0.3.6

Code: Select all

<?php
namespace Cart;
class Currency {
	private $currencies = array();

	public function __construct($registry) {
		$this->db = $registry->get('db');
		$this->language = $registry->get('language');

		$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']
			);
		}
	}

	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;
	}

	...
}
Somewhere you are calling the method $this->currency->format($number, $currency, $value = '', $format = true) without passing a $currency variable.
Hence the "Undefined variable: currency" notice and the "Undefined index" notice for these references: $this->currencies[$currency].... where the undefined variable $currency is the array index.

so search for a statement like $this->currency->format(
and look for something like $this->currency->format(100), i.e. with only the $number parameter.

Active Member
Online

Posts

Joined
Thu Mar 04, 2021 6:34 pm
Who is online

Users browsing this forum: nonnedelectari and 21 guests