cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3.1 Product Collection Returns empty.

SOLVED

Magento 2.3.1 Product Collection Returns empty.

Hello Techies,

Developing Custom block in our themes, We need category based products fetch in the certain module.

In our whole site we're having only one product. Even though its single we can't get it from the product collection factory.

 

Can please anyone share your ideas?

 

Code currently I'm using,

<?php

namespace WebMDT\Emtheme\Product;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as CF;

class ProductsList extends \Emthemes\FilterProduct\Block\Product\ProductsList{

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
     */
    protected $_categoryFactory;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Catalog\Model\Category
     */
    protected $cat;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    public $logger;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
     */
    protected $coreProductCollectionFactory;

    /**
     * @param \Magento\Catalog\Block\Product\Context $context
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
     * @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
     * @param \Magento\Framework\App\Http\Context $httpContext
     * @param \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder
     * @param \Emthemes\FilterProduct\Model\Rule $rule
     * @param \Magento\Widget\Helper\Conditions $conditionsHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Emthemes\FilterProduct\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Emthemes\FilterProduct\Model\ResourceModel\Bestsellers\CollectionFactory $bestSellerCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
        \Emthemes\FilterProduct\Model\Rule $rule,
        \Magento\Widget\Helper\Conditions $conditionsHelper,
        \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        \Magento\Review\Model\ReviewFactory $reviewFactory,
        \Emthemes\FilterProduct\Block\ImageBuilderFactory $customImageBuilderFactory,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryFactory,
        \Magento\Catalog\Model\Category $cat,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = [],
        CF $coreProductCollectionFactory
        )
    {
        $this->productCollectionFactory = $productCollectionFactory;
        $this->bestSellerCollectionFactory = $bestSellerCollectionFactory;
        $this->catalogProductVisibility = $catalogProductVisibility;
        $this->httpContext = $httpContext;
        $this->sqlBuilder = $sqlBuilder;
        $this->rule = $rule;
        $this->urlHelper = $urlHelper;
        $this->conditionsHelper = $conditionsHelper;
        $this->imageHelperFactory = $imageHelperFactory;
        $this->_categoryFactory = $categoryFactory;
        $this->cat = $cat;
        $this->_storeManager = $storeManager;
        $this->coreProductCollectionFactory = $coreProductCollectionFactory;
        parent::__construct(
            $context,
            $productCollectionFactory,
            $bestSellerCollectionFactory,
            $catalogProductVisibility,
            $httpContext,
            $sqlBuilder,
            $rule,
            $conditionsHelper,
            $imageHelperFactory,
            $urlHelper,
            $reviewFactory,
            $customImageBuilderFactory,
            $data
        );
    }

    public function getProductCollection()
    {
        $categoryName = $this->getData('category_name');
        $category = $this->_categoryFactory->create()->addAttributeToFilter('name',$categoryName)->addIsActiveFilter()->setPageSize(1);
        $storeId = $this->_storeManager->getStore()->getId();
        $categorySet = $this->_storeManager->getStore($storeId)->getRootCategoryId();
        if($category->getSize()){
                $categorySet = $this->cat->load($category->getFirstItem()->getId());
        }
        $collection = $this->coreProductCollectionFactory->create();                  $collection->addAttributeToSelect('*')
            ->addCategoryFilter($categorySet)
            ->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds())          ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
        ->setPageSize(50);
        print_r((new \Magento\Framework\DataObject(['items' => $collection]))->debug());
        return $collection;
    }

    /**
     * Prepare and return product collection
     *
     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
     */
    public function createCollection()
    {
        $displayType = $this->getDisplayType();
        $collection = null;
        switch($displayType)
        {
            case 'all_products': $collection = $this->_getAllProductProductCollection();break;
            case 'bestseller_products': $collection = $this->_getBestSellerProductCollection();break;
            case 'category_filter': $collection = $this->getProductCollection();break;
        }
        $sort = explode(' ', $this->getData('order_by'));
        $collection->addAttributeToSort($sort[0],$sort[1]);
        $this->reviewFactory->create()->appendSummary($collection);
        return $collection;
    }
}

Single Product available in our site is,

Screenshot from 2019-05-09 18-46-06.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2.3.1 Product Collection Returns empty.

It is working fine and it is getting product collection as we expected. Actual problem is we didn't mentioned template files in the extended class. Once after the template included it is getting results as accurate.

View solution in original post

1 REPLY 1

Re: Magento 2.3.1 Product Collection Returns empty.

It is working fine and it is getting product collection as we expected. Actual problem is we didn't mentioned template files in the extended class. Once after the template included it is getting results as accurate.