1

This is my block

 <?php 
namespace Xlabs\Ajaxscroll\Block\Product;

class Ajaxscroll extends \Magento\CatalogWidget\Block\Product\ProductsList 
{
    protected $productCollectionFactory;
    protected $_categoryFactory;

    public function __construct(
            \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
            \Magento\Catalog\Model\CategoryFactory $categoryFactory,
            array $data = []
        ){
        $this->productCollectionFactory = $productCollectionFactory;
        $this->_categoryFactory = $categoryFactory;

        }

    public function getProductList(){

        $categoryId = 12;
        $category = $this->_categoryFactory->create()->load($categoryId);   
        $collection = $this->productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addCategoryFilter($category);
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

    return $collection;
    }




}

Blockquote

and i am getting the error

Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/sample221/vendor/magento/framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/sample221/generated/code/Techievolve/Ajaxscroll/Block/Product/Ajaxscroll/Interceptor.php(830): Magento\Framework\View\Element\AbstractBlock->toHtml() #1 /var/www/html/sample221/vendor/magento/framework/View/Layout.php(558): Techievolve\Ajaxscroll\Block\Product\Ajaxscroll\Interceptor->toHtml() #2 /var/www/html/sample221/vendor/magento/framework/View/Layout.php(534): Magento\Framework\View\Layout->_renderBlock('Ajaxscroll_bloc...') #3 /var/www/html/sample221/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('Ajaxscroll_bloc...') #4 /var/www/html/sample221/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('Ajaxscroll_bloc...') #5 /var/www/html/sample221/generated/code/Magento/Framework/View/Layout/Intercep in /var/www/html/sample221/vendor/magento/framework/View/Element/AbstractBlock.php on line 652
1

I have used this code which is working for me. Try this -

<?php
namespace Custom\Homeproducts\Block;

use Magento\Catalog\Api\CategoryRepositoryInterface;

class Homeproducts 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);
    }

    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);
        } else {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
                ->addUrlRewrite();
        }

        $collection->addAttributeToFilter('status', array('eq' => 1));
        $collection->getSelect()
            ->order('rand()')
            ->limit($count);

        return $collection;
    }

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

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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