How to get current category in magento2?

How can I get the current category in magento2 ?

I want to get the category name and category id in a custom phtml file.

+7
magento2
source share
7 answers

Try this code. It will definitely help you.

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category echo $category->getId(); echo $category->getName(); ?> 
+7
source share

Magento sets up a registry for accessing categories. So, to get the currenct category, use the following method:

 /** * @param \Magento\Framework\Registry $registry */ protected $_registry; public function __construct( \Magento\Framework\Registry $registry ) { $this->_registry = $registry; } 

and then use:

 $category = $this->_registry->registry('current_category');//get current category 

Now you can access the collection and receipt data, such as $ category-> getName ()

+13
source share

The above seemed correct, but I think jumping directly into the registry is not the best approach. Magento provides a Layer Resolver that already encapsulates this functionality. (See TopMenu block in catalog plugins)

I suggest introducing the \ Magento \ Catalog \ Model \ Layer \ Resolver class and use it to get the current category. Here is the code:

 <?php namespace FooBar\Demo\Block; class Demo extends \Magento\Framework\View\Element\Template { private $layerResolver; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\Layer\Resolver $layerResolver, array $data = [] ) { parent::__construct($context, $data); $this->layerResolver = $layerResolver; } public function getCurrentCategory() { return $this->layerResolver->get()->getCurrentCategory(); } } 

This is what the getCurrentCategory () method does in the Resolver class.

 public function getCurrentCategory() { $category = $this->getData('current_category'); if ($category === null) { $category = $this->registry->registry('current_category'); if ($category) { $this->setData('current_category', $category); } else { $category = $this->categoryRepository->get($this->getCurrentStore()->getRootCategoryId()); $this->setData('current_category', $category); } } return $category; } 

As you can see, it still uses the registry, but it provides a backup in the event of a failure.

+6
source share

Add XML Code Theme / Namespace / Magento_Catalog / templates / product / view

 <block class="Magento\Catalog\Block\Product\View" name="product.info.category" after="product.price.final" template="product/view/current_category.phtml" /> 

Create a new file theme / namespace / Magento_Catalog / templates / product / view

 <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); $categories = $product->getCategoryIds(); /*will return category ids array*/ foreach($categories as $category){ $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category); echo $cat->getName(); } ?> 
0
source share

Fatal error: Uncaught TypeError: Argument 1 passed to Bofy \ Priceslider \ Block \ FilterRenderer :: __ construct () must be an instance of Magento \ Framework \ Registry, an instance of Magento \ Framework \ View \ Element \ Template \ Context, which is called in /home/183334.cloudwaysapps.com/eutfhyvueu/public_html/generated/code/Bofy/Priceslider/Block/FilterRenderer/Interceptor.php on line 14 and defined in /home/183334.cloudwaysapps.com/eutfhyvueu/public_ricpric /Block/FilterRenderer.php:26 Stack trace: # 0 / home / 183334.cloudwaysapps.com / eutfhyvueu / public_html / generated / code / Bofy / Priceslider / Block / FilterRenderer / Interceptor.php14) \ Priceslider \ Block \ FilterRenderer-> __ construct (Object (Magento \ Framework \ View \ Element \ Template \ Context), Array) # 1 / home / 183334.cloudwaysapps.com / eutfhyvueu / public_html / vendor / magento / framework / ObjectManager / Factory / AbstractFactory.php (111): Bofy \ Priceslider \ Block \ FilterRenderer \ Interceptor-> __ construct (Object (Magento \ Framework \ View \ Element \ Template \ Context), Array) # 2 / hom e / 183334.cloudwaysapps in /home/183334.cloudwaysapps.com/eutfhyvueu/public_html/app/code/Bofy/Priceslider/Block/FilterRenderer.php on line 26

what is a mistake

0
source share

There is no need to use the object manager or enter a class. You can use the built-in helper class Magento\Catalog\Helper\Data as follows.

 <?php $catalogHelperData = $this->helper('Magento\Catalog\Helper\Data'); $categoryObject = $catalogHelperData->getCategory(); $categoryId = $categoryObject->getId(); $categoryName = $categoryObject->getName(); ?> 

This code snippet should work for any phtml (embedded or user-defined) file associated with the product listing page or product details page.

0
source share
 <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category $catId = $category->getId(); $catId = $category->getName(); ?> 
-one
source share

All Articles