How to Get Product Collection by Category ID in Magento 2

You don’t know how the clients’ requirements can shoot out of nowhere. As a developer, you need to keep your tricks handy to satisfy the clients’ demands.

Here’s one such trick to get product collection by category ID in Magento 2. The solution given in the post lets you get the product collection with details like product name, price, etc. using the category ID.

I have used the below solution in my store to create a category of freebies, i.e., Free Magento Extensions!

You can use it to create a special category to feature a lifestyle or brand that is a curated collection of products from different categories. Or, filter products based on its type from the category.

Method to Get Product Collection by Category ID in Magento 2:

That’s it.

Please use the Comments section below to mention any doubts on the topic.

I’d be very grateful if you helped share this helpful post on social media to fellow developers!



(based on 20 Reviews)
How to Get Product Collection by Category ID in  Magento 2Author Magento Badge

Sanjay Jethva

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.


  • Ahmad

    If we are going to show these products in a template file, shall we write the above code in a block?

  • jitendra

    How to get product category name?

    • Sanjay Jethva

      Hello Jitendra,
      Please use the below code for your solution:
      $categoryFactory = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Model\CategoryFactory');
      $catids = $product->getCategoryIds();

      if(sizeof($catids) ) {
      foreach ($catids as $id){
      $catName = $categoryFactory->create()->load($id)->getName();
      echo $catName;


Leave a Reply

Your email address will not be published.