Magento - Check if specific category is active

Magento - Check if specific category is active

July 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Check if specific category is active

Checking for Active Categories in Magento: A Comprehensive Guide

In Magento, determining the active status of a specific category can be achieved using built-in functions and object managers. This is particularly useful when you want to conditionally show or hide navigation links based on category activity.

Understanding Category Activity in Magento

Magento marks categories as active or inactive for display purposes. An active category appears in the frontend, while an inactive one doesn’t. By default, the is_active flag in the database table catalog_category_entity determines this status.

Retrieving Category Information Using Object Manager

To check if a specific category is active, we can use the Magento object manager to retrieve the category model and access its properties. Here’s an example function that implements this logic:

<?php
namespace LaravelCompany\Module\Model;

use Magento\Framework\App\Request\Http;
use Magento\Catalog\Model\CategoryFactory;

class CategoryChecker
{
    protected $request;
    protected $_categoryFactory;

    public function __construct(
        Http $request,
        CategoryFactory $_categoryFactory
    ) {
        $this->request = $request;
        $this->_categoryFactory = $_categoryFactory;
    }

    public function isCategoryActive($categoryId)
    {
        try {
            $category = $this->_categoryFactory->create()->load($categoryId);
            return $category->getIsActive();
        } catch (\Exception $e) {
            // Handle exception logic here
            return false;
        }
    }
}

In this code, we inject the request object and the category factory through dependency injection. The isCategoryActive function loads the specified category by ID using the factory and returns its active status.

Using the CategoryChecker Class in Your Code

To use this class in your controller or block, you can type-hint it as a constructor argument:

<?php
namespace LaravelCompany\Module\Controller;

use Magento\Framework\App\Action\Context;
use LaravelCompany\Module\Model\CategoryChecker;

class MyController extends \Magento\Framework\App\Action\Action
{
    private $categoryChecker;

    public function __construct(
        Context $context,
        CategoryChecker $categoryChecker
    ) {
        parent::__construct($context);
        $this->categoryChecker = $categoryChecker;
    }

    public function execute()
    {
        if ($this->categoryChecker->isCategoryActive(90)) {
            // Specific category is active, do something
        }
    }
}

Best Practices and Alternatives

While the above solution works, it’s important to note that using the object manager directly can be considered an anti-pattern in modern Magento development. It’s recommended to use Magento’s built-in dependency injection system whenever possible.

For simple cases like this one, you may also consider checking the category active status directly in your layout XML or template files:

<!-- app/design/frontend/<Vendor>/<Theme>/Magento_Theme/layout/default.xml -->
<referenceContainer name="header.links">
    <block class="Magento\Theme\Block\Html\Topmenu" name="top.menu" before="-">
        <action method="setUseMenuContainer">
            <argument name="flag" xsi:type="boolean">false</argument>
        </action>

        <!-- Check category activity in the template -->
        <action method="checkCategoryActivity">
            <argument name="categoryId" xsi:type="number">90</argument>
        </action>
    </block>
</referenceContainer>

In this layout XML, we define a new top menu block and use an action to check if the category with ID 90 is active. The actual implementation of the checkCategoryActivity method would depend on your theme’s template.

Conclusion

Checking for active categories in Magento involves retrieving the category model using the object manager or directly in your layout XML. By leveraging these built-in functionalities, you can easily conditionally display navigation links based on category activity.

Remember to consider best practices and use dependency injection where possible to ensure a clean and maintainable codebase. If you’re looking for more advanced customizations and extensions, be sure to explore the Laravel Company blog for in-depth tutorials and guides on Magento development.

Catalog Database Extensions

Our network