Programmatically change 'is_active' of category in Magento 2

Programmatically change 'is_active' of category in Magento 2

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically change 'is_active' of category in Magento 2

Programmatically Changing ‘is_active’ of Category in Magento 2: A Comprehensive Guide

The Magento 2 framework allows for extensive customization through its observer system. However, when attempting to change certain attributes like ‘is_active’, you might encounter challenges due to the nature of Magento’s data structure and API limitations.

In your provided code snippet, you’re on the right track by using the Magento\Catalog\Model\ResourceModel\Category\CollectionFactory and Magento\Catalog\Api\CategoryRepositoryInterface. These are powerful tools for working with categories in Magento. However, there is a subtle issue that prevents the ‘is_active’ attribute from being updated as expected.

The challenge lies in the way Magento handles its core attributes. While you can set any value on a category object using setIsActive(true), this change isn’t reflected back into the database because the repository interface doesn’t support updates for all attributes out of the box.

To resolve this, we need to interact with the category model at a lower level and use the resource model directly. Here’s how you can achieve this:

class Observer implements ObserverInterface
{
    /**
     * @var \Magento\Catalog\Model\ResourceModel\Category
     */
    protected $_categoryResource;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Category $categoryResource
    ) {
        $this->_categoryResource = $categoryResource;
    }

    /**
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        $categoryCollection = $this->_categoryCollectionFactory->create();
        $categoryCollection->addAttributeToSelect('*');
        $categoryCollection->addAttributeToFilter('name', array('eq' => 'test'));

        if ($category = $categoryCollection->getFirstItem()) {
            $category->setData('is_active', true); // Set the value directly
            $this->_categoryResource->save($category);
        }
    }
}

In this updated code, we’re using the Magento\Catalog\Model\ResourceModel\Category resource model to save the category object. The key change is that we are setting the ‘is_active’ data directly on the category object before saving it.

This approach bypasses the limitations of the repository interface and allows you to update any attribute, including the core ‘is_active’ flag.

Remember, when working with Magento’s core data models, it’s crucial to understand the relationship between the model, resource model, and database table. The Magento\Catalog\Model\ResourceModel\Category interacts directly with the catalog_category_entity table in the database, which is where the ‘is_active’ flag resides.

Always ensure you have a proper backup before making changes at this level to prevent data corruption. Also, consider using Magento’s built-in data validation and exception handling mechanisms to make your code more robust.

In conclusion, changing the ‘is_active’ attribute programmatically in Magento 2 requires a direct interaction with the category model at a lower level than the repository interface provides. By using the resource model and setting the data directly on the object before saving it, you can successfully update this core attribute without any limitations.

For more advanced customizations and best practices in Magento development, I recommend exploring the extensive resources available at Laravel Company. They provide detailed guides and tutorials that can help you master the intricacies of Magento 2 programming.

Magento 2 Catalog Database API

Our network