3

I want to get sub category id when I click on main category id. For ex. I Click on "Gear" category...Then It will also display sub category's id..

This is block file function :

public function getCurrentCategory(){        
$currentId  = $this->_registry->registry('current_category'); 
return $currentId; // it will return current category id}

This is phtml file :

    <?php 
$currentCategory = $block->getCurrentCategory();//I want to here subcategory id?>

enter image description here

0

2 Answers 2

6

To get child-category of sub-category Try below code :

public function __construct(
    ...
    \Magento\Catalog\Model\CategoryRepository $categoryRepository
    ...
) {
    ...
    $this->categoryRepository = $categoryRepository;
    ...
}

public function getSubcategories()
{   
    $currentId  = $this->_registry->registry('current_category'); 
    $categoryObj = $this->categoryRepository->get($currentId->getId());
    $subcategories = $categoryObj->getChildrenCategories();

    //Print suncategory Ids
    foreach($subcategories as $subcategorie) {
        //echo $subcategorie->getId();
        if($subcategorie->hasChildren()) {
        $childCategoryObj = $this->categoryRepository->get($subcategorie->getId());
        $childSubcategories = $childCategoryObj->getChildrenCategories();
        foreach($childSubcategories as $childSubcategorie) {
            echo $childSubcategorie->getId();
        }
     }    
   }

    return $subcategories;
}

As its category collection so ,You can also use $categoryObj->getPathId() This function return all subcategory Ids of category.

1

Inject Magento\Catalog\Model\CategoryRepository class to get subcategories

public function __construct(
    ...
    \Magento\Catalog\Model\CategoryRepository $categoryRepository
    ...
) {
    ...
    $this->categoryRepository = $categoryRepository;
    ...
}

public function getSubcategories()
{   
    $currentId  = $this->_registry->registry('current_category'); 
    $categoryObj = $this->categoryRepository->get($currentId->getId());
    $subcategories = $categoryObj->getChildrenCategories();

    foreach($subcategories as $subcategorie) {
        //echo $subcategorie->getId();
        if($subcategorie->hasChildren()) {
            $childCategoryObj = $this->categoryRepository->get($subcategorie->getId());
            $childSubcategories = $childCategoryObj->getChildrenCategories();
            foreach($childSubcategories as $childSubcategorie) {
                echo $childSubcategorie->getId();
            }
        }    
    }

    return $subcategories;
}
5
  • Yes....Your answer working for me....but it will give only last value.. May 5, 2017 at 13:14
  • 2
    See my commented foreach in answer it display multiple subcategory ids May 5, 2017 at 13:21
  • There is one problem also..If I select "Men" Category then it will return only sub-category...That doesn't return child-category of sub-category. May 5, 2017 at 13:33
  • Do you know how to get child-category of sub-category? May 5, 2017 at 13:58
  • how to get subcategory image url in custom module block means i am calling block in phtml file and here i am trying to get subcategory image url magento 2 Nov 12, 2018 at 15:23

Your Answer

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

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