1

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');
4
  • Check if collection is coming correctly. Commented Oct 16, 2019 at 7:19
  • Yes, if i comment "->addAttributeToSort('created_at','DESC')" it's working properly. Commented Oct 16, 2019 at 7:25
  • Try this code if it works: ->setOrder('created_at', 'DESC') Commented Oct 16, 2019 at 7:40
  • not working, I have tried "->setOrder('created_at','DESC');" not working same type of error giving me.. Commented Oct 16, 2019 at 7:45

1 Answer 1

1

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');
2
  • addAttributeToFilter is working for in m2.3.3 when we will change ->addAttributeToFilter('is_saleable', 1, 'left'); to ->addAttributeToFilter('is_saleable', [1], 'left'); changed 1 ==> [1] then it's worked but my issue is not worked ->addAttributeToSort('created_at','DESC') Commented Oct 21, 2019 at 13:25
  • 1
    It is not working because you are chaining these methods. And the chaining is only possible when the previous function return the object so that the next function has an object to work with. And the current magento 2.3.3 version has a bug with that function. It does not return $this. When you then call the next function addAttributeToSort it has no object to work with and this results in your error above. We had the same problem on our side and fixed it like I said. Commented Oct 21, 2019 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.