$categoryId = '3'; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $productFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $category = $categoryFactory->create()->load($categoryId); $collection = $productFactory->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)->load(); $collection->setPageSize(5); foreach($collection as $products){ print_r($products->getData()); }
Magento – Magento-2 get product collection from category id on home page
category-products
Best Answer
Use this code in your block :
Now, Get data in phtml file
Using Object Manager :
Hope this helps. Thanks.
Note :- Do not use Object Manager Directly.