PHP 7.2 error "[] operator not supported for strings"

  • Posts: 164
  • Thank you received: 64
  • Hikashop Business
1 month 3 weeks ago #300868

-- HikaShop version -- : 4.0.1
-- Joomla version -- : 3.9.0
-- PHP version -- : 7.2

Hi,

we tried updating our PHP version from 7.0 to 7.2, but we receive a fatal error "[] operator not supported for strings" on every product listing page where a product has variants, also with the latest Hikashop version 4.0.1 [1811151423] and with default protostar template.
The call stack indicates the error occurs in the administrator\components\com_hikashop\classes\currency.php file on line 956.

We haven't made any changes or customisations, but it seems slightly strange why no one else would have noticed this before. Thanks for looking into it and for any help.

Kind regards,

Please Log in or Create an account to join the conversation.

  • Posts: 23202
  • Thank you received: 3592
  • MODERATOR
1 month 3 weeks ago #300906

Hello,

Line 956 is

$products[$k]->variant_ids[] = $variant->product_id;
But the variable $products is an array and the entry variant_ids is also an array.
Unfortunately, I don't know how you have a string while you need to have an array.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 164
  • Thank you received: 64
  • Hikashop Business
1 month 3 weeks ago #300933

Hi,

ok, but clearly something is not working correctly and like I stated, we haven't made any customizations in any core files, in fact we just updated from 3.5.1 to 4.0.1 so all files are as delivered by the update. So what can we do to figure out what is causing this?

Kind regards,

Please Log in or Create an account to join the conversation.

  • Posts: 23202
  • Thank you received: 3592
  • MODERATOR
1 month 3 weeks ago #300934

Hello,

You can activate the debug mode in order to have the full call stack ; it will help to understand what function called the currency class and maybe understand why the content is not what it should.
Afterwards, it could be useful to perform a print of the products array, to see what is the content of the variant_ids.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 164
  • Thank you received: 64
  • Hikashop Business
1 month 3 weeks ago #300995

Hi,

attached is a screenshot of the full call stack, hopefully it can provide some additional insight into the problem.
If we print the variant_ids they appear normal to us, for example: 220,221,222,223
However, if we print $products[$k]->variant_ids[] it appears the first element is null/empty and we think this is the cause of the error, because it falls through the check on line 954 since it is set, only null, and so on line 956 you assume it is an array while it is not, thus causing the error.

We have no idea so far why this first element is empty, and why strangely enough we seem to be the only one having this issue even though we don't have any customizations and use exactly the same code as everybody else, but if we change line 954 in currency.php from:

if(!isset($products[$k]->variant_ids))
      $products[$k]->variant_ids = array();
to:
if(!isset($products[$k]->variant_ids) || $products[$k]->variant_ids==null)
      $products[$k]->variant_ids = array();

this will also re-initialise the empty variant_ids as an array and everything seems to work ok.
However, this seems more of a hack and doesn't fix the cause of the problem, so any further help with that is much appreciated. Thanks.

Kind regards

Attachments:
Last Edit: 1 month 3 weeks ago by GW.

Please Log in or Create an account to join the conversation.

  • Posts: 23202
  • Thank you received: 3592
  • MODERATOR
1 month 3 weeks ago #301002

Hello,

In such case, I would suggest the usage of

if(empty($products[$k]->variant_ids))
      $products[$k]->variant_ids = array();

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 164
  • Thank you received: 64
  • Hikashop Business
1 month 3 weeks ago #301073

Ok, will this also be added on your end, or do we have to re-insert this code every time we update in future?

Thanks again.

Please Log in or Create an account to join the conversation.

  • Posts: 64484
  • Thank you received: 9313
  • MODERATOR
1 month 2 weeks ago #301078

Hi,

If you can confirm that the suggestion proposed by Jerome solved the issue for you, we'll be happy to add it on our end for the next version.

Please Log in or Create an account to join the conversation.

  • Posts: 164
  • Thank you received: 64
  • Hikashop Business
1 month 2 weeks ago #301097

That's great, yes Jerome's suggestion works fine, no more PHP errors anywhere.

The following user(s) said Thank You: nicolas

Please Log in or Create an account to join the conversation.

Time to create page: 0.093 seconds
Powered by Kunena Forum