Page 1 of 2

OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Wed Jan 08, 2020 4:24 pm
by supak111
Can someone please help me fix this error, it's so annoy. I get it dozens of times per day and I can't even figure out why or when it happens.

PHP Warning: A non-numeric value encountered in ***/public_html/system/library/cart/currency.php on line 69

This thread talk about it but its not a solution viewtopic.php?t=207997

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Wed Jan 08, 2020 6:25 pm
by paulfeakins
Can you tell us what extensions you have and what you're doing when the error occurs?

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Wed Jan 08, 2020 9:18 pm
by IP_CAM

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Wed Jan 08, 2020 10:58 pm
by by mona
It might be because of the update auto currency.

ADMIN => SETTINGS => local tab
and turn off auto update

if that is your problem
viewtopic.php?t=202301

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 1:00 am
by letxobnav
I don't think that uses the convert function in currency.
It is used in many payment extensions.

just add some traces before line 69 to see which value is the issue which perhaps can give a clue where it comes from.

Code: Select all

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

// tracing start
		error_log('value: '.$value);
		error_log('to: '.$to);
		error_log('from: '.$from);
// tracing end
		return $value * ($to / $from);
	}

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 11:10 am
by supak111
by mona wrote:
Wed Jan 08, 2020 10:58 pm
It might be because of the update auto currency.

ADMIN => SETTINGS => local tab
and turn off auto update

if that is your problem
viewtopic.php?t=202301
I have just disabled the auto currency update in settings and will see if that resolves it.

I do have an extension for manually updating currency, it uses http://alphavantage.co API.

I also don't know when it happens, it seems to be random. I've done testing to try to reproduce it but it never happens when I'm poking around.

I will also add those traces, can you explain what that will tell me?

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 11:58 am
by letxobnav
the convert function in the currency class converts currency values.
input are the value, from currency and to currency.
based on the input it uses the from/to exchange rates and the value to produce a new value using the calculation $value * ($to / $from).

php is complaining that one of those is not a number as calculating with a non-number is not so good.
so the error_log statements print out the contents of $value, $to and $from before it does the calculation so you can see which is not a number.

perhaps better to replace the entire function with:

Code: Select all

	public function convert($value, $from, $to) {
		if (isset($this->currencies[$from])) {
			$from_value = $this->currencies[$from]['value'];
		} else {
			$from_value = 1;
		}

		if (isset($this->currencies[$to])) {
			$to_value = $this->currencies[$to]['value'];
		} else {
			$to_value = 1;
		}

// tracing start
		if (!is_numeric($value)) error_log('CONVERT NON-NUMERIC VALUE: '.$value);
		if (!is_numeric($to)) error_log('CONVERT NON-NUMERIC TO_VALUE: '.$this->currencies[$to].' '.$to_value);
		if (!is_numeric($value)) error_log('CONVERT NON-NUMERIC FROM_VALUE: '.$this->currencies[$from].' '.$from_value);
// tracing end
		return $value * ($to_value / $from_value);
	}
that will print to the php error_log only if one of those values is not numeric along with the currency code.
I had to rename some of the variables to retain the currency reference as the original overwrites it's own input variables which is rather dumb.

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 4:15 pm
by supak111
Tried your first code and got the same error just on different line since I added 9 extra lines
PHP Warning: A non-numeric value encountered in public_html/system/library/cart/currency.php on line 77

I'll try using your new code and see what happens.
PS turning off currency update in setting didn't help I guess since I got the error again

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 4:38 pm
by letxobnav
it does not prevent the warning, it prints the values which cause the warning in your error log.
So what is in your error log (the php error log).

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 4:45 pm
by letxobnav
as far as I can tell, only these default controller/models use the currency->convert function:

Code: Select all

openbay
voucher
amazon pay
klarna
auspost
ec ship
fedex
usps
google shopping
unless you have an additional extension installed which uses it.

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 9:50 pm
by supak111
letxobnav wrote:
Thu Jan 09, 2020 11:58 am
the convert function in the currency class converts currency values.
input are the value, from currency and to currency.
based on the input it uses the from/to exchange rates and the value to produce a new value using the calculation $value * ($to / $from).

php is complaining that one of those is not a number as calculating with a non-number is not so good.
so the error_log statements print out the contents of $value, $to and $from before it does the calculation so you can see which is not a number.

perhaps better to replace the entire function with:

Code: Select all

	public function convert($value, $from, $to) {
		if (isset($this->currencies[$from])) {
			$from_value = $this->currencies[$from]['value'];
		} else {
			$from_value = 1;
		}

		if (isset($this->currencies[$to])) {
			$to_value = $this->currencies[$to]['value'];
		} else {
			$to_value = 1;
		}

// tracing start
		if (!is_numeric($value)) error_log('CONVERT NON-NUMERIC VALUE: '.$value);
		if (!is_numeric($to)) error_log('CONVERT NON-NUMERIC TO_VALUE: '.$this->currencies[$to].' '.$to_value);
		if (!is_numeric($value)) error_log('CONVERT NON-NUMERIC FROM_VALUE: '.$this->currencies[$from].' '.$from_value);
// tracing end
		return $value * ($to_value / $from_value);
	}
that will print to the php error_log only if one of those values is not numeric along with the currency code.
I had to rename some of the variables to retain the currency reference as the original overwrites it's own input variables which is rather dumb.
added the code above and now got his:

2020-01-09 6:34:05 - PHP Notice: Array to string conversion in /public_html/system/library/cart/currency.php on line 76
2020-01-09 6:34:05 - PHP Notice: Array to string conversion in /public_html/system/library/cart/currency.php on line 77
2020-01-09 6:34:05 - PHP Warning: A non-numeric value encountered in/public_html/system/library/cart/currency.php on line 79

which would be

Code: Select all

76:  if (!is_numeric($to)) error_log('CONVERT NON-NUMERIC TO_VALUE: '.$this->currencies[$to].' '.$to_value);
77:  if (!is_numeric($value)) error_log('CONVERT NON-NUMERIC FROM_VALUE: '.$this->currencies[$from].' '.$from_value);
// tracing end
79:  return $value * ($to_value / $from_value);

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 10:00 pm
by supak111
letxobnav wrote:
Thu Jan 09, 2020 4:45 pm
as far as I can tell, only these default controller/models use the currency->convert function:

Code: Select all

openbay
voucher
amazon pay
klarna
auspost
ec ship
fedex
usps
google shopping
unless you have an additional extension installed which uses it.
Since I don't use any of the above, only voucher (which I don't even need), I went digging and round it!!! If I try to purchase a voucher with the "Amount" field empty I get this error to show up every time!

NOW is there any way to edit the voucher module so if someone leave the field empty I don't keep seeing the error?

PS I just tried disabling the Voucher extension but I can still access it under ***.com/index.php?route=account/voucher

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Thu Jan 09, 2020 10:35 pm
by supak111
Lastes errors are:

Code: Select all

2020-01-09 8:18:46 - PHP Notice:  Array to string conversion in/public_html/system/library/cart/currency.php on line 76

[09-Jan-2020 08:18:46 America/Chicago] CONVERT NON-NUMERIC TO_VALUE: Array 1.00000000
[09-Jan-2020 08:18:46 America/Chicago] CONVERT NON-NUMERIC TO_VALUE: Array 1.00000000

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Fri Jan 10, 2020 12:40 am
by letxobnav
first change the function to this to get rid of the notices about the conversions

Code: Select all

	public function convert($value, $from, $to) {
		if (isset($this->currencies[$from])) {
			$from_value = $this->currencies[$from]['value'];
		} else {
			$from_value = 1;
		}

		if (isset($this->currencies[$to])) {
			$to_value = $this->currencies[$to]['value'];
		} else {
			$to_value = 1;
		}

// tracing start
		if (!is_numeric($value)) error_log('CONVERT NON-NUMERIC VALUE: '.$value);
		if (!is_numeric($to_value)) error_log('CONVERT NON-NUMERIC TO_VALUE: '.$to.' '.$to_value);
		if (!is_numeric($from_value)) error_log('CONVERT NON-NUMERIC FROM_VALUE: '.$from.' '.$from_value);
// tracing end
		return $value * ($to_value / $from_value);
	}

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Fri Jan 10, 2020 7:10 am
by supak111
So I gave it some hours and it looks like its not voucher page issue. Its something else that constantly giving me these errors. Error I get in storage/logs/oc_error.log

Code: Select all

2020-01-09 12:24:00 - PHP Notice:  Array to string conversion in /public_html/system/library/cart/currency.php on line 76
2020-01-09 12:24:00 - PHP Notice:  Array to string conversion in /public_html/system/library/cart/currency.php on line 77
2020-01-09 12:24:00 - PHP Warning:  A non-numeric value encountered in /public_html/system/library/cart/currency.php on line 79
I really wanna know what is causing this damn error so I can fix it properly hopefully

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Fri Jan 10, 2020 8:42 am
by sw!tch
supak111 wrote:
Fri Jan 10, 2020 7:10 am
So I gave it some hours and it looks like its not voucher page issue. Its something else that constantly giving me these errors. Error I get in storage/logs/oc_error.log

Code: Select all

2020-01-09 12:24:00 - PHP Notice:  Array to string conversion in /public_html/system/library/cart/currency.php on line 76
2020-01-09 12:24:00 - PHP Notice:  Array to string conversion in /public_html/system/library/cart/currency.php on line 77
2020-01-09 12:24:00 - PHP Warning:  A non-numeric value encountered in /public_html/system/library/cart/currency.php on line 79
I really wanna know what is causing this damn error so I can fix it properly hopefully
Interesting in voucher.php.. I can't test this live at the moment, but looks like there is no numeric check. It's using the currency class to validate.

Untested, but you can try :
catalog/controller/account/voucher.php

Replace:

Code: Select all

if (($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) < $this->config->get('config_voucher_min')) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) > $this->config->get('config_voucher_max'))) {
with

Code: Select all

if ((!isset($this->request->post['amount'])) || (!is_numeric($this->request->post['amount'])) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) < $this->config->get('config_voucher_min')) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) > $this->config->get('config_voucher_max'))) { 
Someone else can refine if needed. Also you may need to restore you currency class changes.

Edit:
Updated to also include the !isset($this->request->post['amount'] check as mentioned by @straightlight below, appears that was also missing in the validate method.

This should resolve the non-numeric value in currency from vouchers. My guess is bots are hitting that page and is the source of your error, unless you are using some other extension not listed above.

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Fri Jan 10, 2020 8:52 am
by straightlight

Code: Select all

if ((!is_numeric($this->request->post['amount'])) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) < $this->config->get('config_voucher_min')) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) > $this->config->get('config_voucher_max'))) {
for:

Code: Select all

if ((!isset($this->request->post['amount'])) || (!is_numeric($this->request->post['amount'])) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) < $this->config->get('config_voucher_min')) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) > $this->config->get('config_voucher_max'))) {

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Fri Jan 10, 2020 10:58 am
by supak111
I also think a opencart spam bot might be hitting my voucher page since it can't be turned OFF in extensions completely.

The code you guys suggested above, one code has 1 "{" at the end and the other code has 2 "{ {" at the end, which is correct? To me it looks like 1 "{" but I'm pretty bad at coding

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Fri Jan 10, 2020 11:16 am
by sw!tch
See my revised post. Its just one bracket

Re: OC 3.0.3.2 PHP Warning: A non-numeric value encountered system/library/cart/currency.php on line 69

Posted: Fri Jan 10, 2020 11:21 am
by supak111
sw!tch wrote:
Fri Jan 10, 2020 11:16 am
See my revised post. Its just one bracket
Awesome thanks, its seems to work perfect. I hope I never see that darn error ever again.

One other question, what do you mean by: "Also you may need to restore you currency class changes"