---
title: "How to Get Product Collection by Category ID in Magento 2"
url: "https://magecomp.com/blog/get-product-collection-category-id-magento-2/"
date: "2020-07-01T19:36:54+00:00"
modified: "2025-02-20T10:03:06+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "General"
  - "Magento 2"
word_count: 461
reading_time: "3 min read"
summary: "Hello Magento Friends?,"
description: "With the help of this article, you can easily get the product collection with all the information like product price, name, etc. by using the category ID."
keywords: "General, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "B2B eCommerce: Definition, Types &amp; Examples (2026)"
    url: "https://magecomp.com/blog/guide-to-b2b-ecommerce/"
  - title: "Top 10 Shopify Plus Agency in USA"
    url: "https://magecomp.com/blog/shopify-plus-agency-usa/"
  - title: "Upgrade Magento 2 with Composer"
    url: "https://magecomp.com/blog/upgrade-magento-2-composer/"
---

# How to Get Product Collection by Category ID in Magento 2

_Published: July 1, 2020_  
_Author: Dhiren Vasoya_  

![How to Get Product Collection by Category ID in Magento 2](https://magecomp.com/blog/wp-content/uploads/2020/07/How-to-Get-Product-Collection-by-Category-ID-in-Magento-2-1024x512.webp)

**Hello Magento Friends?,**

How’s the weekend going on? As always welcome to Magento Tutorial Blog. Today I am going to explain to you How to Get Product Collection by Category ID in Magento 2. Also, go through our previous blog article about [**Magento 2: How to Get System Configuration Value Into JS**](https://magecomp.com/blog/magento-2-get-system-configuration-value-js/). Let’s Dive In?
> [**OPEN NEW SALES CHANNEL** for your Magento store in **3 EASY STEPS!** ](https://magecomp.com/magento-2-mobile-app-builder?utm_source=magecomp_blog&utm_medium=mobile_app_banner&utm_campaign=magento_2_mobile_app)

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-5.webp)](https://magecomp.com/services/hyva-theme-development/)

## Introduction:
The client is like a small child. We have to satisfy all the needs of our clients. Developers should take care of all the requirements of the client’s demands. For that, I am going to illustrate to you all how to Get Product Collection by Category ID in Magento 2. In this rapid development in the online business, there are mandatory features required like a product collection option with various filters applied to it. Else the products are filtered on the basis of its category.
With the help of this article you can easily get the product collection with all the information like product price, name, etc. by using the category ID. For your illustration visit [Magento 2 Extensions](https://magecomp.com/magento-2-extensions.html) where I have implemented
## Steps to Get Product Collection by Category ID in Magento 2:
### Method 1: Using Class
Create one block file on our custom extension
add Blockname.php in the following path

app\code\Vendor\Extension\Block\Blockname.php

now add following code

```
<?php
namespace VendorExtensionBlock;

use MagentoFrameworkViewElementTemplate;

class Blockname extends Template
{

	protected $categoryFactory;

	public function __construct(
		MagentoBackendBlockTemplateContext $context,
		MagentoCatalogModelCategoryFactory

	)
	{
		$this->categoryFactory = $CategoryFactory;
		parent::__construct($context);
	}

	public function getProductCollectionFromCategory($categoryId) {
		$category = $this->categoryFactory->create()->load($categoryId);
		return $category->getProductCollection()->addAttributeToSelect('*');

	}



}
```

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

After this you need to phtml file and add the following code into a file:

```
$block->getProductCollectionFromCategory(4); // YOUR CATEGORY ID

foreach ($categoryProducts as $product) {
    // get Product data
    print_r($product->getData());

    echo $product->getName();
}
```

### Method 2: Using Object Manager
```
$objectManager =  MagentoFrameworkAppObjectManager::getInstance();
$categorysFactory = $objectManager->get('MagentoCatalogModelCategoryFactory');

$categoryId = 4; // YOUR CATEGORY ID
$category = $categorysFactory->create()->load($categoryId);

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

foreach ($categoryProducts as $product) {
    // get Product data
    print_r($product->getData());

    echo $product->getName();
}
```

That’s It You are done.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-3.webp)](https://magecomp.com/magento-performance-optimization.html)

**Note:** Using ObjectManager for Magento development is not recommended by us and Magento. This is just for Knowledge purposes.

## Over to You:
Therefore, after you implement the above step you will easily be able to get Product Collection by Category ID in Magento 2. If any difficulties then do comment down in the comment section below. Share the article with your Magento developer friends and help them to get Product Collection by Category ID in Magento 2.
**Happy Coding!**

[![](https://magecomp.com/blog/wp-content/uploads/2022/05/Mobile-App.png)](https://magecomp.com/magento-2-mobile-app-builder)


---

_View the original post at: [https://magecomp.com/blog/get-product-collection-category-id-magento-2/](https://magecomp.com/blog/get-product-collection-category-id-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-25 08:50:24 UTC_  
