cancel
Showing results for 
Search instead for 
Did you mean: 

product collection

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

product collection

hello,

using magento 2.2.1. Trying to get product collection, I'm doing this:

 

<?php
namespace Hoop\Util\Block;
class CategoryCollect extends \Magento\Framework\View\Element\Template
{
    protected $_categoryHelper;
    protected $categoryFlatConfig;
    protected $topMenu;
    protected $_productCollectionFactory;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Catalog\Helper\Category $categoryHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
        \Magento\Theme\Block\Html\Topmenu $topMenu,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
    )
    {
        $this->_categoryHelper = $categoryHelper;
        $this->categoryFlatConfig = $categoryFlatState;
        $this->topMenu = $topMenu;
        $this->_productCollectionFactory = $productCollectionFactory;

        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        // $collection->setPageSize(3);
        var_dump($collection->getSize());
        parent::__construct($context);
    }

but the getSize() returns always 0, but I have 16 products in admin.

What am I doing wrong?

thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions

Re: product collection

Your constructor object is working fine for me, I have check in one my extension and all product count display perfect.

 

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Helper\Category $categoryHelper,
    \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
    \Magento\Theme\Block\Html\Topmenu $topMenu,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
)
{
    $this->_categoryHelper = $categoryHelper;
    $this->categoryFlatConfig = $categoryFlatState;
    $this->topMenu = $topMenu;
    $this->_productCollectionFactory = $productCollectionFactory;

    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    var_dump($collection->getSize());
    parent::__construct($context);
}

Might be you have to check your all product is available and all of the product is enable.

Please do using reindex command might be indexing cause the count as 0 so you need to check using indexing and get perfect product count for your code.

So you havent problem with code.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

3 REPLIES 3

Re: product collection

Hi
Please use

var_dump($collection->count());
---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: product collection

Hello,

thanks for your answer. Unfortunately also count() returns 0.

Any further hint?

 

Thanks

Re: product collection

Your constructor object is working fine for me, I have check in one my extension and all product count display perfect.

 

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Helper\Category $categoryHelper,
    \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
    \Magento\Theme\Block\Html\Topmenu $topMenu,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
)
{
    $this->_categoryHelper = $categoryHelper;
    $this->categoryFlatConfig = $categoryFlatState;
    $this->topMenu = $topMenu;
    $this->_productCollectionFactory = $productCollectionFactory;

    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    var_dump($collection->getSize());
    parent::__construct($context);
}

Might be you have to check your all product is available and all of the product is enable.

Please do using reindex command might be indexing cause the count as 0 so you need to check using indexing and get perfect product count for your code.

So you havent problem with code.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial