Magento get current category details


Magento get current category details in 1.x and 2.x both version: Here in this tutorial, we are going to cover all details about the current category such as category name, category id, category url etc. We will explain how to get details in Magento 1 and Magneto 2. Here are the things which we will cover in this tutorial.

  • Id
  • Name
  • Url
  • Description
  • Level
  • Image Url
  • Thumbnail Image Url
  • Product Collection

Magento get current category Details | Magento 2

There are followings conditions to get category details –
1 – You want to get current selected category detail.
2 – Or You want load category detail by Category Id.

Let us start with above both conditions –


Get Details in Magento 2

In Magento 2 you can get the details simly as below-


Magento 2 get current category Id

You can use object managet to get the current category id in Magento 2.

Get current Category Id In Magento 2 :
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
   
 $catId = $category->getId(); 

Get Details in Magento 1

In Magento 1 you can get the details simly as below-


Magento get current category Id

Syntax to get current Category Id :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 

Magento Load Category By Id


Now we will use the above category id to load category details and other information-

Load Category By Id :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $category = Mage::getModel('catalog/category')->load($catId); 

Now you can get category details such as name, url etc.

Magento get category Name By Category Id

Syntax to get Category Name Id :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $category = Mage::getModel('catalog/category')->load($catId); 
 $catName = $category->getName(); 

Magento get category Url By Category Id

Syntax to get Category Url :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $category = Mage::getModel('catalog/category')->load($catId); 
 $catUrl = $category->getUrl(); 

Magento get category Description By Category Id

Syntax to get Category Description :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $category = Mage::getModel('catalog/category')->load($catId); 
 $catDesc = $category->getDescription(); 

Magento get category Level By Category Id

Syntax to get Category Level :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $category = Mage::getModel('catalog/category')->load($catId); 
 $catLevel = $category->getLevel(); 

Magento get category Image Url By Category Id

Syntax to get Category Image Url :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $category = Mage::getModel('catalog/category')->load($catId); 
 $catImage = $category->getImageUrl(); 

Magento get category Thumbnail Image Url By Category Id

Syntax to get Category Thumbnail Image Url :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $category = Mage::getModel('catalog/category')->load($catId); 
 $catThumbnail = $category->getThumbnail(); 

Magento Load Category Products Collection

If you want to load products in individual category by id you can get all products as below .


Magento Load Category Products Collection By Category Id

Category Products Collection :
 $catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 $categoryProducts = Mage::getModel('catalog/category')->load($catId)
                     ->getCollection()
                     ->addAttributeToSelect('*'); 
 foreach( $categoryProducts as $product){
 echo $product->getId()."<br>";
 echo $product->getName()."<br>";
 echo $product->getPrice()."<br>";

}
 

The above example will load all products of the category.

Note : In the above example we have selected all attributes
->addAttributeToSelect(‘*’);. We suggest not to select all attributes while getting the products collection, select the attributes which you need to display because it may slow down the system performance.

Advertisements

Add Comment

📖 Read More