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
Solved! Go to Solution.
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.
Hi
Please use
var_dump($collection->count());
Hello,
thanks for your answer. Unfortunately also count() returns 0.
Any further hint?
Thanks
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.