Thursday, 23 June 2016

how to get current category Id on Magento 2

<?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();
?>

2 comments:

  1. string
    Fatal error: Uncaught Error: Call to a member function getId() on null in /var/www/html/trainees/pooja_new/Magento22/app/code/Mageplaza/HelloWorld/Block/Rewrite/Product/ListProduct.php:100 Stack trace: #0 /var/www/html/trainees/pooja_new/Magento22/app/code/Mageplaza/HelloWorld/Block/Rewrite/Product/ListProduct.php(156): Mageplaza\HelloWorld\Block\Rewrite\Product\ListProduct->initializeProductCollection() #1 /var/www/html/trainees/pooja_new/Magento22/vendor/magento/module-catalog/Block/Product/ListProduct.php(189): Mageplaza\HelloWorld\Block\Rewrite\Product\ListProduct->_getProductCollection() #2 /var/www/html/trainees/pooja_new/Magento22/vendor/magento/framework/View/Element/AbstractBlock.php(666): Magento\Catalog\Block\Product\ListProduct->_beforeToHtml() #3 /var/www/html/trainees/pooja_new/Magento22/vendor/magento/framework/View/Layout.php(557): Magento\Framework\View\Element\AbstractBlock->toHtml() #4 /var/www/html/trainees/pooja_new/Magento22/vendor/magento/framework/View/Layout.php(533): Magento\Framework\View\Layout in /var/www/html/trainees/pooja_new/Magento22/app/code/Mageplaza/HelloWorld/Block/Rewrite/Product/ListProduct.php on line 100

    ReplyDelete