I want to change the design of my store depends of the categories and subcategories.

I need to check if the current category is subcategory and if is subcategory then display something there.

I made this before in Magento 1.9 with the getLevel function:

$isLevel3 = $_category->getLevel() == 3;
if ($isLevel3) {

how I can get the Level in Magento 2 version?

Thank you


I get the subcategories level in Magento 2 like this:

$_category  = $block->getCurrentCategory();
         $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
         $categories = $categoryFactory->create()                              
                ->addFieldToFilter('is_active', '1')
                ->addFieldToFilter('include_in_menu', '1')
        $last = count($categories);
        $_category  = $block->getCurrentCategory();
        foreach ($categories as $ficategory):
                if($ficategory->getLevel() == 3) {
                    $mydiv = "subcategory";

But how I can check if the current category is Level 3?

$catLevel = $ficategory->getLevel(); //3
if($catLevel == 3) {
    Your need here.

Play a little bit around this:

public function __constructor(
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\Registry $registry
) {
    $this->request = $request;
    $this->registry = $registry;

public function isCurentCategorySubCategory() {
    if ($request->getFullActionName() !== "catalog_category_view") {
        throw new \Exception('This is not category view page')
    if ($registry->registry('current_category')->getLevel() === '3') {
        return true;

    return false;

I believe you have to do something like that

  • Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
    – Robert
    Mar 1 '19 at 22:44
  • phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2 Mar 1 '19 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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