Skip to content

Magento 2.3.0: getSize call on configurable collection leads to exception, if no product filters are applied #22127

@jhruehl

Description

@jhruehl

Preconditions (*)

  1. Magento 2.3.0
  2. NewRelic module enabled (and configured)
    (you can cheat around enabling the module, if set the newrelic enabled setting to true in the core_config_table and flush the cache afterwards)

Steps to reproduce (*)

  1. Run magento_newrelicreporting_cron cron job

Expected result (*)

  1. magento_newrelicreporting_cron cron job successful

Actual result (*)

  1. magento_newrelicreporting_cron throws error "Warning: Invalid argument supplied for foreach() in /var/www/shop_test/src/www/vendor/magento/module-configurable-product/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php on line 83"

Explanation

The newrelic cron tries to get the amount of configurable products (or at least the given children) calling the getCount function of the ConfigurableProductManagementInterface.
In this process a configurable collection object is created (magento/module-configurable-product/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php).
There the getSize function (in AbstractDB parent class) is called and as a consequence the _renderFilters function in the magento/module-configurable-product/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php class.

Since 2.3.0 the product filter functionality seems to have been partly moved there with the probable intention to enable several product filters to be applicable at the same time.

Here the _renderFilters() function code:

protected function _renderFilters()
    {
        parent::_renderFilters();
        $metadata = $this->getProductEntityMetadata();
        $parentIds = [];
        foreach ($this->products as $product) {
            $parentIds[] = $product->getData($metadata->getLinkField());
        }

        $this->getSelect()->where('link_table.parent_id in (?)', $parentIds);

        return $this;
    }

The cause of the problem should be quite clear, if you look at the code. If no product filter is applied to the configurable product collection $this-products is null, but without any checks it is used in the given foreach-loop.

Proposed fix:

protected function _renderFilters()
    {
        parent::_renderFilters();
        if ($this->products) {
            $metadata = $this->getProductEntityMetadata();
            $parentIds = [];
            foreach ($this->products as $product) {
                $parentIds[] = $product->getData($metadata->getLinkField());
            }
            $this->getSelect()->where('link_table.parent_id in (?)', $parentIds);
        }
        return $this;
    }

A simple and reasonable if-clause does the trick without touching the changed (or "enhanced") product filter functionality.

Metadata

Metadata

Assignees

Labels

Component: NewRelicReportingFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions