Magento – Warning: Invalid argument supplied for foreach() when viewing shipment

magento-2.1shipmentstatus

While I'm trying to view the status of a shipment.
It will shows Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in vendor/magento/module-shipping/view/adminhtml/templates/order/packaging/packed.phtml on line 12

Magento version : 2.1.4
But it also happens under 2.1.3

full error message :

1 exception(s):
Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-shipping/view/adminhtml/templates/order/packaging/packed.phtml on line 12

Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-shipping/view/adminhtml/templates/order/packaging/packed.phtml on line 12
#0 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-shipping/view/adminhtml/templates/order/packaging/packed.phtml(12): Magento\Framework\App\ErrorHandler->handler(2, 'Invalid argumen...', '/home/wwwroot/s...', 12, Array)
#1 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/home/wwwroot/s...')
#2 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Shipping\Block\Adminhtml\Order\Packaging\Interceptor), '/home/wwwroot/s...', Array)
#3 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/Template.php(279): Magento\Framework\View\Element\Template->fetchView('/home/wwwroot/s...')
#4 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-backend/Block/Template.php(104): Magento\Framework\View\Element\Template->_toHtml()
#5 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Backend\Block\Template->_toHtml()
#6 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml()
#7 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(518): Magento\Framework\View\Layout->_renderBlock('shipment_packed')
#8 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('shipment_packed')
#9 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/AbstractBlock.php(499): Magento\Framework\View\Layout->renderElement('shipment_packed', true)
#10 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-shipping/view/adminhtml/templates/view/form.phtml(109): Magento\Framework\View\Element\AbstractBlock->getChildHtml('shipment_packed')
#11 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/home/wwwroot/s...')
#12 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Shipping\Block\Adminhtml\View\Form), '/home/wwwroot/s...', Array)
#13 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/Template.php(279): Magento\Framework\View\Element\Template->fetchView('/home/wwwroot/s...')
#14 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-backend/Block/Template.php(104): Magento\Framework\View\Element\Template->_toHtml()
#15 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Backend\Block\Template->_toHtml()
#16 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml()
#17 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(518): Magento\Framework\View\Layout->_renderBlock('form')
#18 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('form')
#19 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/AbstractBlock.php(499): Magento\Framework\View\Layout->renderElement('form', true)
#20 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-backend/Block/Widget/Form/Container.php(173): Magento\Framework\View\Element\AbstractBlock->getChildHtml('form')
#21 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-backend/view/adminhtml/templates/widget/form/container.phtml(15): Magento\Backend\Block\Widget\Form\Container->getFormHtml()
#22 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/home/wwwroot/s...')
#23 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Shipping\Block\Adminhtml\View), '/home/wwwroot/s...', Array)
#24 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/Template.php(279): Magento\Framework\View\Element\Template->fetchView('/home/wwwroot/s...')
#25 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/module-backend/Block/Template.php(104): Magento\Framework\View\Element\Template->_toHtml()
#26 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Backend\Block\Template->_toHtml()
#27 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml()
#28 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(518): Magento\Framework\View\Layout->_renderBlock('sales_shipment_...')
#29 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('sales_shipment_...')
#30 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('sales_shipment_...')
#31 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('content')
#32 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('content')
#33 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('content')
#34 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('main.col')
#35 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('main.col')
#36 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('main.col')
#37 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('admin.scope.col...')
#38 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('admin.scope.col...')
#39 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('admin.scope.col...')
#40 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.main.conta...')
#41 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('page.main.conta...')
#42 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('page.main.conta...')
#43 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.content')
#44 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('page.content')
#45 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('page.content')
#46 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.wrapper')
#47 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('page.wrapper')
#48 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('page.wrapper')
#49 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('backend.page')
#50 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('backend.page')
#51 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout->renderElement('backend.page')
#52 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('root')
#53 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout->renderNonCachedElement('root')
#54 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Layout.php(938): Magento\Framework\View\Layout->renderElement('root')
#55 /home/wwwroot/shopkey.doyustudio.com/var/generation/Magento/Framework/View/Layout/Interceptor.php(37): Magento\Framework\View\Layout->getOutput()
#56 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Result/Page.php(243): Magento\Framework\View\Layout\Interceptor->getOutput()
#57 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/View/Result/Layout.php(164): Magento\Framework\View\Result\Page->render(Object(Magento\Framework\App\Response\Http\Interceptor))
#58 /home/wwwroot/shopkey.doyustudio.com/var/generation/Magento/Backend/Model/View/Result/Page/Interceptor.php(24): Magento\Framework\View\Result\Layout->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#59 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/App/Http.php(139): Magento\Backend\Model\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#60 /home/wwwroot/shopkey.doyustudio.com/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#61 /home/wwwroot/shopkey.doyustudio.com/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#62 {main}

I tried to view the database.
And I found the difference between those shipment which failed and works.
The column "packaged" is different.
For those works, shows a:0:{}
For those fails, shows s:6:"a:0:{}";

This error not happened for all orders.
For newer order, it works fine.
But few days passed, I'm not sure what changes.
It goes wrong.

We moved our site to another server recently.
And this error is start to happen after that.
But I don't know what can I provide for more info.
I really have no idea about this issue 🙁
If need any extra info, could you please tell me?

Thank you, every body 🙂

Best Answer

The issue here is with the return value of Magento\Shipping\Block\Adminhtml\Order\Packaging. That is the class that is being used by the template as its block. Here are the contents of line 12 in the template:

<?php foreach ($block->getPackages() as $packageId => $package): ?>

What's happening is that a string value is likely being set to the packages data key in the Shipment model, Magento\Sales\Model\Order\Shipment. You need to ensure that an array is returned from that method, even if it is an empty array.

Now, before you make changes to the template or model, look at this: the serialized data in the packaged column reflects that: a:0:{} is a serialized empty array, while s:6:"a:0:{}"; is a serialized string that contains the string for a serialized empty array.

Somehow, that value is being serialized an extra time. You need to ensure that it is not reserialized if it is already serialized. That may fix the whole problem for you, as I'd guess that the issue is likely happening only with orders that are somehow being resaved, as that would explain that value being serialized a second time.

Please let me know if you have any other questions!

Related Topic