Magento 2 How to disable category attribute on condition?

Magento 2 How to disable category attribute on condition?

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 How to disable category attribute on condition?

Disabling Magento 2 Category Attributes Based on Conditions

Magento 2 offers robust control over your store’s catalog structure through categories and attributes. However, sometimes you may need to enable or disable specific category attributes based on certain conditions for better management and performance.

The Scenario: Enabling a Category Attribute Only for L2 Categories

Let’s say you have an attribute named custom_product_type that you want to enable only for the second-level (L2) categories in your store. For all other categories, this attribute should remain disabled.

Step 1: Understand Magento 2 System Configurations

Magento 2 uses system configurations and configuration paths to manage settings across the platform. To disable an attribute in a category, you need to modify the attribute_manage option for that specific category under the catalog_category scope.

The relevant configuration path is:

catalog/category/attribute/manage

Step 2: Creating a New Attribute

If the custom_product_type attribute doesn’t exist yet, create it by following these steps:

  • Go to Admin > Stores > Attributes > Product.
  • Click on “Add new attribute”.
  • Enter custom_product_type in the Attibute code field.
  • Select the appropriate input type (e.g., Text Field).
  • Set other required fields and click Save.

Step 3: Enabling the Attribute for L2 Categories

Now, let’s enable this attribute only for our second-level categories. For this example, I’ll assume you have a category with ID 10 that contains your L2 subcategories.

Method 1: Using Magento Admin Panel

  • Go to Admin > Stores > Attributes > Product.
  • Search for the custom_product_type attribute.
  • Click on “Manage Categories”.
  • Select the checkboxes corresponding to the L2 categories under category ID 10.
  • Save your changes.

For larger stores, using code is more efficient. You can create an observer for the catalog_category_save_before event and conditionally enable/disable the attribute based on the parent category’s ID.

Create a file named di.xml in your custom module:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Event\Observer">
        <argument name="observers" xsi:type="array">
            <item name="category_attribute_manager" instance="Vendor\ModuleName\Model\Observer\CategoryAttributeObserver" shared="false"/>
        </argument>
    </type>
</config>

Next, create the observer file CategoryAttributeObserver.php:

namespace Vendor\ModuleName\Model\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class CategoryAttributeObserver implements ObserverInterface
{
    protected $attributeRepository;

    public function __construct(\Magento\Eav\Model\AttributeRepository $attributeRepository)
    {
        $this->attributeRepository = $attributeRepository;
    }

    public function execute(Observer $observer)
    {
        $category = $observer->getCategory();
        if ($category->getParentId() == 10) { // Assuming ID of your top-level category
            $customTypeAttributeCode = 'custom_product_type';
            $this->attributeRepository->save($this->attributeRepository->get($customTypeAttributeCode));
        }
    }
}

This script will trigger whenever a category is saved and enable the custom_product_type attribute if the parent ID matches.

Step 4: Test Your Changes

  • Save a new category under your L2 category.
  • Check whether the custom_product_type attribute appears in the “Manage Categories” section for this newly created category.

Remember, changes made through code require you to run the setup:upgrade and setup:di:compile commands after each modification. You can find more information about Magento 2 development best practices on Laravel Company’s Official Blog.

Conclusion

Disabling category attributes based on conditions is a powerful feature in Magento 2 that allows you to maintain clean and organized product catalogs. Whether using the admin panel or custom code, the approach remains straightforward once you understand the system configuration and event management processes.

By following this guide, you can now effectively manage your category attributes based on specific parent categories or any other condition required for your business operations.

Magento 2 Catalog Performance Admin

Our network