Request Quote

Disable attribute in product edit admin panel in Magento 2

Disable attribute in product edit admin panel in Magento 2

Sometimes we might to disable attribute in product edit admin panel in Magento 2 like this –

Attribute disabled

In the screenshot we can see that the attribute has been disabled. We can make it hidden too. To achieve this, use the following code-

In di.xml file inside app/code/Webkul/Module/etc/adminhtml/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="testAttribute" xsi:type="array">
                    <item name="class" xsi:type="string">Webkul\Module\Ui\DataProvider\Product\Form\Modifier\Attributes</item>
                    <item name="sortOrder" xsi:type="number">1000</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

As we can see that we have provided a path, so we create a file Attributes.php in folder app/code/Webkul/Module/Ui/DataProvider/Product/Form/Modifier/. In file Attributes.php –

<?php

namespace Webkul\Module\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Framework\Stdlib\ArrayManager;

class Attributes extends AbstractModifier
{
    /**
     * @var Magento\Framework\Stdlib\ArrayManager
     */
    private $arrayManager;

    /**
     * @param ArrayManager $arrayManager
     */
    public function __construct(
        ArrayManager $arrayManager
    ) {
        $this->arrayManager = $arrayManager;
    }

    /**
     * modifyData
     *
     * @param array $data
     * @return array
     */
    public function modifyData(array $data)
    {
        return $data;
    }

    /**
     * modifyMeta
     *
     * @param array $data
     * @return array
     */
    public function modifyMeta(array $meta)
    {
        $attribute = 'test_attribute';
        $path = $this->arrayManager->findPath($attribute, $meta, null, 'children');
        $meta = $this->arrayManager->set(
            "{$path}/arguments/data/config/disabled",
            $meta,
            true
        );

        return $meta;
    }
}

You can see that we have put ‘disabled’ as true. This will disable the attribute.

If you want to hide the attribute then in place of ‘disabled’ write ‘visible’ and in place of ‘true’ write ‘false’ so that visible becomes false and attribute will be hidden.

I hope this helps.

Happy coding 🙂

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close