Magento 2 Warning – Illegal String Offset ‘itemID’

magento2php-7

this is the error I get with my Magento 2 module, in my logs:

Warning: Illegal string offset 'itemID' in
/a/www/vhosts/domain.com/mage2.domain.com/deploy003/app/code/Maurisource/Lightspeed/Helper/Catalog/Product/Configurable.php
on line 68

This is line:68 it's referring to:

foreach ($rawData['Items']['Item'] as $item) {
    $simpleProductLightspeedId[] = $item['itemID'];
}

This is the output of debug.log

$this->_logger->debug(json_encode($rawData['Items']['Item']));

[2017-11-22 21:43:36] main.DEBUG:
[{"itemID":"14","systemSku":"210000000020","defaultCost":"0","avgCost":"0","discountable":"true","tax":"true","archived":"false","itemType":"default","serialized":"false","description":"manesh-matrix-color
a b
c","modelYear":"0","upc":"","ean":"","customSku":"","manufacturerSku":"","createTime":"2016-05-02T18:33:46+00:00","timeStamp":"2016-06-02T19:22:26+00:00","publishToEcom":"false","categoryID":"2","taxClassID":"1","departmentID":"0","itemMatrixID":"3","itemAttributesID":"3","manufacturerID":"5","noteID":"16","seasonID":"0","defaultVendorID":"0","itemECommerceID":"0","Prices":{"ItemPrice":[{"amount":"788","useTypeID":"1","useType":"Default"},{"amount":"788","useTypeID":"2","useType":"MSRP"},{"amount":"788","useTypeID":"3","useType":"Online"}]}},

I think this error suggests that $rawData['Items']['Item'] is not an array. But I'm not sure what's wrong here, any hints?

Best Answer

before the foreach loop I used the following code to fix the issue:

if(!isset($rawData['Items']['Item'][0]))
    $rawData['Items']['Item'][] = $rawData['Items']['Item'];
Related Topic