1

The following code, returns an empty collection in most categories, even if the category indeed has products:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
$category = $categoryFactory->create()->load($categoryId);
$collection = $category->getProductCollection()
             ->addAttributeToSelect('*');

Am I missing something?

2
  • Please check my answer. Oct 19, 2018 at 10:54
  • Is it working for you or not? Oct 19, 2018 at 13:05

3 Answers 3

1

try below code

protected $_productCollectionFactory;
protected $_categoryFactory;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Framework\View\Element\Template\Context $context,
) {
    $this->_categoryFactory = $categoryFactory;
    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct($context);
}

put below code in your method

$categoryId = 'yourcategoryid';
    $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;
1

You can try with below codes it's working on my end.

$categoryId = 21; // YOUR CATEGORY ID
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');


$category = $categoryFactory->create()->load($categoryId);
$categoryProducts = $category->getProductCollection()
                             ->addAttributeToSelect('*');
foreach ($categoryProducts as $product) {
    //print_r($product->getData());
    // printing category name and url
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

Please used the Dependency Injection method instead of Object manager. Reference link : http://blog.chapagain.com.np/magento-2-get-all-products-of-a-category/

4
  • your code seems correct, but it's not working on my side
    – zekia
    Oct 19, 2018 at 10:40
  • Any error message or you have set valid category ? Oct 19, 2018 at 10:54
  • Yes category is valid. There's no error, just returning empty collection
    – zekia
    Oct 19, 2018 at 11:34
  • Please check product options likes enable, in stock, Proper categories assign. Also do the indexing process so search product available in front end. Oct 19, 2018 at 11:38
0

Try to use below ways :

EDIT 1 :

$categoryID = '2';
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')->getCollection()->addAttributeToSelect('*')
->addCategoriesFilter(['eq' => $categoryID]);

EDIT 2 :

$categoryId = '2';
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->load($categoryId);
$collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')->load($categoryId);
$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;

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.