Post by maffe » Sun Jun 15, 2025 6:08 pm

Hello,
I have Opencart 3.0.4.0 and with php 8.x i get error with the default extension Google Base Feed:
Unknown: html_entity_decode(): Passing null to parameter #1 ($string) of type string is deprecated in \catalog\controller\extension\feed\google_base.php on line 39.
Do we have i little fix for that please?

Active Member

Posts

Joined
Sun Jun 12, 2011 5:21 am
Location - Lelystad

Post by by mona » Sun Jun 15, 2025 6:58 pm

There is no php8.x
If you are trying to use php8.4 you need to downgrade.

php 8 has strict usage of empty strings being passed these now need to be explicit.

Code: Select all

html_entity_decode($string ?? '');
html_entity_decode($string ?? '', ENT_QUOTES, 'UTF-8');

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 JNeuhoff » Sun Jun 15, 2025 8:08 pm

Try replacing this line from

Code: Select all

$output .= '<g:brand><![CDATA[' . html_entity_decode($product['manufacturer'], ENT_QUOTES, 'UTF-8') . ']]></g:brand>';
to

Code: Select all

$output .= '<g:brand><![CDATA[' . html_entity_decode((string)$product['manufacturer'], ENT_QUOTES, 'UTF-8') . ']]></g:brand>'; 
See also this change on github for OC 3.0.x.x.
Last edited by JNeuhoff on Sun Jun 15, 2025 9:28 pm, edited 1 time in total.

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 by mona » Sun Jun 15, 2025 8:34 pm

@JNeuhoff
Can you go into detail why this and not that ?
https://stackoverflow.com/questions/717 ... eter-error
(going down to the bit about strict and Rector) I am still none the wiser - but you probably are ...
Thanks in advance

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 maffe » Sun Jun 15, 2025 9:29 pm

JNeuhoff wrote:
Sun Jun 15, 2025 8:08 pm

Code: Select all

$output .= '<g:brand><![CDATA[' . html_entity_decode((string)$product['manufacturer'], ENT_QUOTES, 'UTF-8') . ']]></g:brand>'; 
Yes worked. One other question:
The Google Base feed does not include a currency.
Google Merchant thinks we're using dollars (by default?), I guess...
But we live in Europe, so the currency should be EUR. Is this easy to adjust?

Now the feed gives:
<g:price>24.5</g:price>
But should be:
<g:price>24.5 EUR</g:price>

Active Member

Posts

Joined
Sun Jun 12, 2011 5:21 am
Location - Lelystad

Post by JNeuhoff » Sun Jun 15, 2025 9:34 pm

by mona wrote:
Sun Jun 15, 2025 8:34 pm
@JNeuhoff
Can you go into detail why this and not that ?
https://stackoverflow.com/questions/717 ... eter-error
(going down to the bit about strict and Rector) I am still none the wiser - but you probably are ...
Thanks in advance
We could have used the Null Coalescing Operator, too, but I think in this case it's easier just to typecast the null into an empty string. I just tested the change, and it work fine.

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 JNeuhoff » Sun Jun 15, 2025 9:35 pm

maffe wrote:
Sun Jun 15, 2025 9:29 pm
JNeuhoff wrote:
Sun Jun 15, 2025 8:08 pm

Code: Select all

$output .= '<g:brand><![CDATA[' . html_entity_decode((string)$product['manufacturer'], ENT_QUOTES, 'UTF-8') . ']]></g:brand>'; 
Yes worked. One other question:
The Google Base feed does not include a currency.
Google Merchant thinks we're using dollars (by default?), I guess...
But we live in Europe, so the currency should be EUR. Is this easy to adjust?

Now the feed gives:
<g:price>24.5</g:price>
But should be:
<g:price>24.5 EUR</g:price>
Good point. What's your OpenCart default currency? I think it uses the configured default currency in the feed.

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 by mona » Sun Jun 15, 2025 10:02 pm

JNeuhoff wrote:
Sun Jun 15, 2025 9:34 pm
by mona wrote:
Sun Jun 15, 2025 8:34 pm
@JNeuhoff
Can you go into detail why this and not that ?
https://stackoverflow.com/questions/717 ... eter-error
(going down to the bit about strict and Rector) I am still none the wiser - but you probably are ...
Thanks in advance
We could have used the Null Coalescing Operator, too, but I think in this case it's easier just to typecast the null into an empty string. I just tested the change, and it work fine.
I understand both, yet I am still none the wiser. My preference would always be to use Null Coalescing Operator, yours I understand would be to typecast the null into an empty string - I have no reason for my preference.


This is some interesting points from a conversation from the stackoverflow linked above
The quick fix in most cases is to use the null coalescing operator to provide a default value as appropriate, so you don't need a long null check around every use. For instance, htmlspecialchars($something) can be replaced with htmlspecialchars($something ?? '')

Rector has the rule NullToStrictStringFuncCallArgRector to fix this:

- mb_strtolower($value);
+ mb_strtolower((string) $value);

I like that better than $value ?? '' –

Depends. One should be aware with the string cast that it turns arrays into the string "Array" while giving a warning (notice before PHP 8) ref. So you have to consider the appropriate handling beforehand (or at least should when applying automated changes to the overall code-base), to not run into the next maintenance problem. Rector likely does it this way because this is what would happened before, so it preserves the original behaviour. –
hakre

A big difference is that if you are running with strict_mode=1, then htmlspecialchars(42) is already an error; htmlspecialchars(42 ?? '') retains that error, htmlspecialchars((string)42) suppresses it. Similarly, (string)$object will call $object->__toString(), $object ?? '' will not. On the other hand, htmlspecialchars($mistypedVar ?? '') suppresses a Warning which htmlspecialchars((string)$mistypedVar) does not, so neither is a perfect replacement. –
IMSoP

My strong educated guess in context of the question is that htmlspecialchars() and trim() is a signal that not strict types but "string" types is what OP is aiming for, most likely code related to output (template etc.). This is not what strict_mode is of a particular benefit of, so would only add another error as false positive (even a real one, not a deprecation warning). The reason why the internal functions were typed was a different one than the strict_mode IIRC, and I have some sympathy for the cause here, but fear it's not only null but also the 42. –

this is a great way to hide genuine TypeErrors in the code... use ??'' instead. –


I'd like (as an addition, existing answers have my upvotes) to paint a different picture on how to see and tackle with such "problems". It does not make the outlined approaches less right or wrong and is merely an additional view which hopefully is of mutual benefit. And every project is different.

Cheat-Sheet

(string)$mixed - previous behaviour
$mixed ?? '' - error suppression for TypeError on null only
@ - full error suppression. you should document in/for your code-base where it is applicable to use.
@@ - if this comes up, it is likely an interesting spot to look into.
empty($mixed) ? '' : xxx($mixed) - carry the trash out, typical empty paralysis / mixed confusion, look for clusters, there is a chance the code-base can be greatly simplified. migrate to scalar types (PHP 7), pull in strict type handling from most-inner in outwards direction, use both PHP "classic" and "strict" type handling where applicable. PHP 7.0 assertions and PHP 8.1 deprecation messages can support here well.

I would be interested to know if anyone has an opinion, method or logic to share ?

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 maffe » Tue Jun 17, 2025 4:18 am

JNeuhoff wrote:
Sun Jun 15, 2025 9:35 pm
Good point. What's your OpenCart default currency? I think it uses the configured default currency in the feed.
My default currency in the shop is Euro.
I even completely removed the Dollar. But Google Merchant still shows prices in USD.
However, if we include 'EUR' in the feed (like in the example), it works correctly.
I think this needs to be adjusted in the default OpenCart Google Base extension?
Does anyone have a quick fix for this?

Active Member

Posts

Joined
Sun Jun 12, 2011 5:21 am
Location - Lelystad

Post by JNeuhoff » Tue Jun 17, 2025 3:40 pm

maffe wrote:
Tue Jun 17, 2025 4:18 am
JNeuhoff wrote:
Sun Jun 15, 2025 9:35 pm
Good point. What's your OpenCart default currency? I think it uses the configured default currency in the feed.
My default currency in the shop is Euro.
I even completely removed the Dollar. But Google Merchant still shows prices in USD.
However, if we include 'EUR' in the feed (like in the example), it works correctly.
I think this needs to be adjusted in the default OpenCart Google Base extension?
Does anyone have a quick fix for this?
I think you are right! Looking at the docs in https://support.google.com/merchants/answer/6324371 the currency should indeed be included, such as

Code: Select all

<g:price>15.00 USD</g:price>
I'll submit a bugfix to the github repository.

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

Who is online

Users browsing this forum: Amazon [Bot] and 78 guests