Magento – After Upgrade Magento 2.3.2 to 2.3.3 getting error

magento-upgrademagento2magento2.3magento2.3.3

Fatal error: Uncaught Error: Call to a member function addAttributeToSort() on null in /app/code/Sm/FilterProducts/Block/FilterProducts.php:394 Stack trace: #0 /app/code/Sm/FilterProducts/Block/FilterProducts.php(193): Sm\FilterProducts\Block\FilterProducts->_newProducts() #1 /app/code/Sm/FilterProducts/Block/FilterProducts.php(442): Sm\FilterProducts\Block\FilterProducts->_getProducts() #2 /generated/code/Sm/FilterProducts/Block/FilterProducts/Interceptor.php(102): Sm\FilterProducts\Block\FilterProducts->getLoadedProductCollection() #3 /app/design/frontend/Sm/market/Sm_FilterProducts/templates/default-sidebar.phtml(15): Sm\FilterProducts\Block\FilterProducts\Interceptor->getLoadedProductCollection() #4 /vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/home/www/onlyt…') #5 /vendor/ in /app/code/Sm/FilterProducts/Block/FilterProducts.php on line 394

my collection code as :

$collection->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
            ->addUrlRewrite()
            ->setStoreId($this->_storeId)
            ->addAttributeToFilter('is_saleable', [1], 'left')
            ->addAttributeToSort('created_at','DESC');

Best Answer

It is a bug in magento. The method "addAttributeToFilter" does not return the object if you are using method chaining. This bug is fixed in the 2.3-develop branch.

As workaround you can call the collection like this:

$collection->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
        ->addUrlRewrite()
        ->setStoreId($this->_storeId)
        ->addAttributeToFilter('is_saleable', [1], 'left');
$collection->addAttributeToSort('created_at','DESC');
Related Topic