Magento 2 API: Get Categories

In this article, we will be looking into how we can get the Categories in Magento 2 using its API. Get All Categories Get Category by category id Search Categories Anonymouse APIs Restriction The REST endpoints to get the stores’ information falls under the anonymous APIs category. However, in the recent Magento version, those API … Read more

Magento 2: Search Repository using SearchCriteriaBuilder, Filter & FilterGroup

This article shows how we can search Magento Repositories using SearchCriteriaBuilder that implements SearchCriteriaInterface. An example is provided of how we can perform search on Product repository in Magento. Similarly, we can search for other entities like Category, Customer, Order, Invoice, CMS Page, CMS Block, etc. We can add different conditions to build custom search … Read more

Magento 2: Get all Categories of any/current Product

This article shows how we can get list of all categories from current product or any particular product as well. Both ways (Dependency Injection & Object Manager way) are shown below: Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory, \Magento\Catalog\Model\ProductRepository and \Magento\Framework\Registry classes … Read more

Magento 2: Get list of all Categories & Store Categories

This article shows how we can get list of all categories and all category of current store in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory and \Magento\Catalog\Helper\Category classes in the constructor of my module’s block class. app/code/Chapagain/HelloWorld/Block/HelloWorld.php <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template … Read more

Magento 2: Get parent category, children categories & product count

This article shows how we can get parent category, children categories and total number of products in a category in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\CategoryFactory class in the constructor of my module’s block class. Objects of class \Magento\Catalog\Helper\Category and \Magento\Catalog\Model\CategoryRepository as also … Read more

Magento 2: Get all Products of a Category

This article shows how we can get all products of a particular category in Magento 2. Both Dependency Injection and Object Manager way of coding is shown below. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\CategoryFactory class in the constructor of my module’s … Read more

Magento 2: Get Current Category & Current Product

This article shows how we can get current category and current product data in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Framework\Registry class in the constructor of my module’s block class. app/code/Chapagain/HelloWorld/Block/HelloWorld.php <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_registry; public function __construct( … Read more