magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Invalid argument supplied for foreach() in vendor/magento/module-catalog/Model/Category/DataProvider.php on line 398 #31888

Closed Ckaneriya closed 3 years ago

Ckaneriya commented 3 years ago

Preconditions (*)

  1. Magento v 2.3.6
  2. PHP version 7.2.30
  3. Hosting Provider: Nexcess

we have multistore setup

Steps to reproduce (*)

  1. Upgrade the Magento version from 2.3.1 to 2.3.6
  2. After upgrading the version go to admin and open the Category page. (Catalog -> Categories)

Expected result (*)

  1. Should be able to view all the categories in the admin

Actual result (*)

  1. Saw below error on category page in the admin

` 1 exception(s): Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in vendor/magento/module-catalog/Model/Category/DataProvider.php on line 398

#1 Magento\Catalog\Model\Category\DataProvider->getAttributesMeta() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#2 Magento\Catalog\Model\Category\DataProvider\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#3 Magento\Catalog\Model\Category\DataProvider\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#4 Magento\Catalog\Model\Category\DataProvider\Interceptor->___callPlugins() called at [generated/code/Magento/Catalog/Model/Category/DataProvider/Interceptor.php:65]
#5 Magento\Catalog\Model\Category\DataProvider\Interceptor->getAttributesMeta() called at [vendor/magento/module-catalog/Model/Category/DataProvider.php:299]
#6 Magento\Catalog\Model\Category\DataProvider->prepareMeta() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#7 Magento\Catalog\Model\Category\DataProvider\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#8 Magento\Catalog\Model\Category\DataProvider\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#9 Magento\Catalog\Model\Category\DataProvider\Interceptor->___callPlugins() called at [generated/code/Magento/Catalog/Model/Category/DataProvider/Interceptor.php:39]
#10 Magento\Catalog\Model\Category\DataProvider\Interceptor->prepareMeta() called at [vendor/magento/module-catalog/Model/Category/DataProvider.php:231]
#11 Magento\Catalog\Model\Category\DataProvider->getMeta() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#12 Magento\Catalog\Model\Category\DataProvider\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#13 Magento\Catalog\Model\Category\DataProvider\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#14 Magento\Catalog\Model\Category\DataProvider\Interceptor->___callPlugins() called at [generated/code/Magento/Catalog/Model/Category/DataProvider/Interceptor.php:26]
#15 Magento\Catalog\Model\Category\DataProvider\Interceptor->getMeta() called at [vendor/magento/framework/View/Element/UiComponentFactory.php:318]
#16 Magento\Framework\View\Element\UiComponentFactory->mergeMetadata() called at [vendor/magento/framework/View/Element/UiComponentFactory.php:218]
#17 Magento\Framework\View\Element\UiComponentFactory->create() called at [vendor/magento/framework/View/Layout/Generator/UiComponent.php:140]
#18 Magento\Framework\View\Layout\Generator\UiComponent->generateComponent() called at [vendor/magento/framework/View/Layout/Generator/UiComponent.php:103]
#19 Magento\Framework\View\Layout\Generator\UiComponent->process() called at [vendor/magento/framework/View/Layout/GeneratorPool.php:81]
#20 Magento\Framework\View\Layout\GeneratorPool->process() called at [vendor/magento/framework/View/Layout.php:352]
#21 Magento\Framework\View\Layout->generateElements() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:89]
#22 Magento\Framework\View\Layout\Interceptor->generateElements() called at [vendor/magento/framework/View/Layout/Builder.php:129]
#23 Magento\Framework\View\Layout\Builder->generateLayoutBlocks() called at [vendor/magento/framework/View/Page/Builder.php:55]
#24 Magento\Framework\View\Page\Builder->generateLayoutBlocks() called at [vendor/magento/framework/View/Layout/Builder.php:65]
#25 Magento\Framework\View\Layout\Builder->build() called at [vendor/magento/framework/View/Layout.php:259]
#26 Magento\Framework\View\Layout->build() called at [vendor/magento/framework/View/Layout.php:884]
#27 Magento\Framework\View\Layout->getBlock() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:414]
#28 Magento\Framework\View\Layout\Interceptor->getBlock() called at [vendor/magento/module-backend/Model/View/Result/Page.php:26]
#29 Magento\Backend\Model\View\Result\Page->setActiveMenu() called at [generated/code/Magento/Backend/Model/View/Result/Page/Interceptor.php:24]
#30 Magento\Backend\Model\View\Result\Page\Interceptor->setActiveMenu() called at [vendor/magento/module-catalog/Controller/Adminhtml/Category/Edit.php:113]
#31 Magento\Catalog\Controller\Adminhtml\Category\Edit->execute() called at [generated/code/Magento/Catalog/Controller/Adminhtml/Category/Edit/Interceptor.php:24]
#32 Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->execute() called at [vendor/magento/framework/App/Action/Action.php:108]
#33 Magento\Framework\App\Action\Action->dispatch() called at [vendor/magento/module-backend/App/AbstractAction.php:248]
#34 Magento\Backend\App\AbstractAction->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#35 Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#36 Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->Magento\Framework\Interception\{closure}() called at [app/code/WeltPixel/Backend/Plugin/Utility.php:76]
#37 WeltPixel\Backend\Plugin\Utility->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#38 Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/module-backend/App/Action/Plugin/Authentication.php:143]
#39 Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#40 Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#41 Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->___callPlugins() called at [generated/code/Magento/Catalog/Controller/Adminhtml/Category/Edit/Interceptor.php:39]
#42 Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->dispatch() called at [vendor/magento/framework/App/FrontController.php:186]
#43 Magento\Framework\App\FrontController->processRequest() called at [vendor/magento/framework/App/FrontController.php:119]
#44 Magento\Framework\App\FrontController->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#45 Magento\Framework\App\FrontController\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#46 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#47 Magento\Framework\App\FrontController\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]
#48 Magento\Framework\App\FrontController\Interceptor->dispatch() called at [vendor/magento/framework/App/Http.php:116]
#49 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
#50 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
#51 Magento\Framework\App\Bootstrap->run() called at [index.php:39]
` 2. Noticed the same error in `var/log/system.log` and `var/log/debug.log` files We didn't face any issues during the version upgrade. Could this be a PHP version issue? I think the Magento 2.3.6 should work on PHP 7.2, right? Tried to find the solution on StackOverflow but didn't any specific solution or even a workaround.
m2-assistant[bot] commented 3 years ago

Hi @Ckaneriya. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

mrtuvn commented 3 years ago

Have you try update your version php to 7.3+. It's hard to tell exactly problem without see real data Theory v 2.3.6 should work with php7.2

Ckaneriya commented 3 years ago

@mrtuvn No, I haven't tried to update the PHP version. I thought 2.3.6 should work with 7.2. Should I change the version?

mrtuvn commented 3 years ago

I'm not sure. Maybe some data related with your category save before not work after upgrade. You can see this repo for reference php version. Try switch your version to 7.3 https://github.com/navarr/magento2-supported-versions

m2-assistant[bot] commented 3 years ago

Hi @engcom-Delta. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Delta commented 3 years ago

Hi @Ckaneriya thank you for your report. Do you try to upgrade according docs?

Ckaneriya commented 3 years ago

@engcom-Delta Yes, I have upgraded by following the mentioned doc.

mackieee commented 3 years ago

I came across the very same error today after installing Magento 2.4.2 on PHP 7.4. Opening Categories page led to this PHP Warning call stack. Disabled any releavant third party modules that were mentioned in the Call stack (Like OP's WeltPixel) and still produced the same.

Looking at the file in question: /vendor/magento/module-catalog/Model/Category/DataProvider.php

if ($attribute->usesSource()) {
        $source = $attribute->getSource();
        $currentCategory = $this->getCurrentCategory();
        if ($source instanceof SpecificSourceInterface && $currentCategory) {
                $options = $source->getOptionsFor($currentCategory);
        } else {
                $options = $source->getAllOptions();
        }
        // Line 434 
        foreach ($options as &$option) {
                $option['__disableTmpl'] = true;
        }
        $meta[$code]['options'] = $options;
}

It appears $options is NULL when debugging - wrapping the foreach( $options as &$options ) with a check (!empty) passed this error and allowed the categories to load. As this was a fresh Magento install of Community, I only had one category currently in the store - Default category.

mrtuvn commented 3 years ago

Maybe some data in categories or some module integrate with categories cause this. You should recheck that

m2-assistant[bot] commented 3 years ago

Hi @engcom-Echo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Echo commented 3 years ago

Hi @Ckaneriya @MackieeE ,

The issue is not producible based on steps given . Can you please try to test the issue on latest magento branch i.e. 2.4-develop or kindly provide more information regarding the issue so that we can verify the same at our end.

Thanks

m2-assistant[bot] commented 3 years ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 3 years ago

Hello @Ckaneriya,

We have tried to reproduce the issue, with upgrading Magento from 2.3.1 to 2.3.6, using this devdocs link, but unable to reproduce the issue. Please find below screenshot for reference:

  1. Before Upgrade:

Before-Default-Category-ID-2-Categories-Inventory-Catalog-Magento-Admin

  1. After Upgrade:

After-Default-Category-ID-2-Categories-Inventory-Catalog-Magento-Admin

You can try again to upgrade and let us know if you are still facing the issue.

Thanks

engcom-Hotel commented 3 years ago

Hello @Ckaneriya,

It seems that your issue has been resolved. We are not able to hear from you for the last 14 days so we are closing this issue for now.

If you are still facing the issue you can reopen it.

Thanks

Ckaneriya commented 3 years ago

@engcom-Hotel I have checked your last reply. I noticed that you have only one category. That is the default category in your environment. when we had upgraded our environment, we had so many real categories with real data.

mackieee commented 3 years ago

I’m almost certain this is happening when:

Both me and OP had third party modules, this I’m sure can’t be replicated from a fresh install & upgrade.

Ckaneriya commented 3 years ago

@mackieee Yes, you are right. we have developed one custom module which adds on attribute in all categories and we are indeed having the install script in the custom module. Do you have any solution for this scenario?

mrtuvn commented 3 years ago

i think in this case you can check from your end with custom module for work with magento. I'm not sure this is magento bug or not without module check. You can share your custom module if possible. Without module it's hard to tell exactly problem. Maybe we still need fix from both magento end and custom module too. This issue probably only able reproduce with old store with some data instead clean instance