0

I am trying to fetch current subcategory name of specific parent category which id is 5 for example my category is Brand and subcategory is Brand1/Brand2/brand3 so i want fetch current subcategory category on product page .

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $current_category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
          $parent_category = $current_category->getParentCategory();
          echo $current_category->getName();
          echo $parent_category->getName(); ?>

enter image description here

1 Answer 1

0

To get sub-category colletion for specific category, you need to used following code.

$categoryId = 5;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$current_category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);

// Get Sub-Category For Current Categories

$subcategories = $parent_category->getChildrenCategories();
foreach($subcategories as $subcategorie) 
{
   echo $subcategorie->getName();
}
8
  • Dhiren Vasoya@ I am trying to print category name on product details page,I have brand and brand have many subcategory, when user click on brand subcategory then need to show only current subcategory.?
    – sanjay
    Jul 4, 2017 at 11:05
  • @sanjay you need product category? Check your question code, you are trying to get category from Registry, and it return when you are on category page. Jul 4, 2017 at 11:07
  • Above code need on;y perform of specific category which ID is 5? can you help me
    – sanjay
    Jul 4, 2017 at 11:08
  • Can you clerify your requirement in detail. What you actualy want? Jul 4, 2017 at 11:09
  • @sanjay are you looking to get sub-categories of main category where main category Id is 5? something like this? Jul 4, 2017 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.