Blog Page

blog details

12 Sep 2022

How to get Product Grid by Category ID Magento2

With Object manager you can get Products from a Specific Category and show in Grid like following: Typical implementation of Dependency Manager. Enables Inversion of Control support for Magento 2 Object Manager.
<?php
/*
* Company Name: Cozmot Inc
* Website: https://cozmot.com
* Writer Name: Zaheer Ahmed
* Contact: [email protected]
* Number: +92-301-1000201
*/
$_category = $block->getCategory(); //or any ID like 2 or 3 if you already have ?>
            <?php
            $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
            $categorysFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
            ?>
            <?php foreach($_category as $category): ?>
            <?php
            $cat = $category->getID();
            ?>
            <div class="category-name-<?= $cat;?>">
                <a href="<?php echo $category->getUrl(); ?>">
                    <span><?php if($block->getData('title')){echo $block->getData('title');}else{echo $category->getName();} ?></span>
                </a>
            </div>
            <div class="category-more-<?= $cat;?>">
                <a href="<?php echo $category->getUrl(); ?>">
                    <span>See More<?php echo $category->getID(); ?></span>
                </a>
            </div>
            <?php
            $categoryId = $cat;
            $cate = $categorysFactory->create()->load($categoryId);
            if($block->getData('filter-selection') == '0') {
                $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('created_at','desc');;
            } elseif($block->getData('filter-selection') == '1') {
                    $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('special_price', ['neq' => '']);
                } elseif($block->getData('filter-selection') == '2') {
                    $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_featured', '1');
                }
            $block->getProductCollectionFromCategory($cat);?>
            <div id="homepage-wiget-slider" class="owl-side-arrow" style="margin: 0 -10px">
                <div class="owl-carouel owl-thme" style="display: flex;">
                    <?php
                    $counter = 5;
                    foreach ($categoryProducts as $product) {
                        if($counter > 0){
                            $counter--;
                        $imagewidth=400;
                        $imageheight=400;
                        $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
                        $image_url = $imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl();
                        ?>
                        <div class="iem homeage-widget-slider-content-wrapper">
                            <a href="<?= $product->getProductUrl();?>">
                                <img src="<?php echo $image_url; ?>" alt="" />
                                <h4 class="price"><?php echo $product->getFormattedPrice();?></h4>
                                <h3 class="title"><?php echo $product->getName();?></h3></a>
                        </div>
                    <?php }else{
                            break;
                        } } ?>

                </div>
            </div>
            <?php endforeach; ?>
Another solution if you only wanna fetch Products 
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryId = 4; // could be any of yours
$category = $categoryFactory->create()->load($categoryId);

$categoryProducts = $category->getProductCollection()
    ->addAttributeToSelect('*');

foreach ($categoryProducts as $product) {
    // geting Product object
    print_r($product->getData());

    echo $product->getName();
    echo $product->getProductUrl();
}
Object Manager Usage Good and Bad: