2

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

Edit:

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()                              
                ->addAttributeToSelect('*')
                ->setStore($store)
                ->addFieldToFilter('is_active', '1')
                ->addFieldToFilter('include_in_menu', '1')
                ->addAttributeToFilter('level',3);
        $last = count($categories);
        $_category  = $block->getCurrentCategory();
        foreach ($categories as $ficategory):
                if($ficategory->getLevel() == 3) {
                    $mydiv = "subcategory";
                } 
        endforeach;

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

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

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

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.