0

[16-Jun-2020 01:01:12 UTC] PHP Fatal error: Uncaught Error: Call to a member function addCategoryFilter() on null in /home/vip/public_html/app/code/Smartwave/Filterproducts/Block/Home/LatestList.php:57 Stack trace: #0 /home/vip/public_html/generated/code/Smartwave/Filterproducts/Block/Home/LatestList/Interceptor.php(24): Smartwave\Filterproducts\Block\Home\LatestList->getProducts() #1 /home/vip/public_html/app/code/Smartwave/Filterproducts/Block/Home/LatestList.php(33): Smartwave\Filterproducts\Block\Home\LatestList\Interceptor->getProducts() #2 /home/vip/public_html/vendor/magento/module-catalog/Block/Product/ListProduct.php(187): Smartwave\Filterproducts\Block\Home\LatestList->_getProductCollection() #3 /home/vip/public_html/vendor/magento/framework/View/Element/AbstractBlock.php(666): Magento\Catalog\Block\Product\ListProduct->_beforeToHtml() #4 /home/vip/public_html/generated/code/Smartwave/Filterproducts/Block/Home/LatestList/Interceptor.php(908): Magento\Framework\View\Element\AbstractBlock->toHtml() #5 /home/vip/public_html/vendor/magento/module in /home/vip/public_html/app/code/Smartwave/Filterproducts/Block/Home/LatestList.php on line 57

1 Answer 1

0

Take the backup of your file

/app/code/Smartwave/Filterproducts/Block/Home/LatestList.php

And replace the file with the below code and check

<?php

namespace Smartwave\Filterproducts\Block\Home;

use Magento\Catalog\Api\CategoryRepositoryInterface;

class LatestList extends \Magento\Catalog\Block\Product\ListProduct {

    protected $_collection;

    protected $categoryRepository;

    protected $_resource;

    public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
            \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
            \Magento\Catalog\Model\Layer\Resolver $layerResolver,
            CategoryRepositoryInterface $categoryRepository,
            \Magento\Framework\Url\Helper\Data $urlHelper,
            \Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
            \Magento\Framework\App\ResourceConnection $resource,
            array $data = []
    ) {
        $this->categoryRepository = $categoryRepository;
        $this->_collection = $collection;
        $this->_resource = $resource;

        parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
    }

    protected function _getProductCollection() {
        return $this->getProducts();
    }

    public function getProducts() {
        $count = $this->getProductCount();
        $category_id = $this->getData("category_id");
        $collection = clone $this->_collection;
        $collection->clear()->getSelect()->reset(\Magento\Framework\DB\Select::WHERE)->reset(\Magento\Framework\DB\Select::ORDER)->reset(\Magento\Framework\DB\Select::LIMIT_COUNT)->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET)->reset(\Magento\Framework\DB\Select::GROUP);

        if(!$category_id) {
            $category_id = $this->_storeManager->getStore()->getRootCategoryId();
        }
        $category = $this->categoryRepository->get($category_id);
        if(isset($category) && $category) {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
                ->addUrlRewrite() 
                ->addCategoryFilter($category)
                ->addAttributeToSort('created_at','desc');
        } else {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
                ->addUrlRewrite()
                ->addAttributeToSort('created_at','desc');
        }

        $collection->getSelect()
                ->order('created_at','desc')
                ->limit($count);

        return $collection;
    }

    public function getLoadedProductCollection() {
        return $this->getProducts();
    }

    public function getProductCount() {
        $limit = $this->getData("product_count");
        if(!$limit)
            $limit = 10;
        return $limit;
    }
}

I hope it will work for you.

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.