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

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-
<?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>
In the di.xml file inside app/code/Webkul/Module/etc/adminhtml/
As we can see that we have provided a path, so we create a file Attributes.php in the folder app/code/Webkul/Module/Ui/DataProvider/Product/Form/Modifier/. In file Attributes.php –
You can see that we have put ‘disabled’ as true. This will disable the attribute.
<?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;
}
}
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 the attribute will be hidden.
I hope this helps.
Happy coding 🙂
$attribute = ‘price’;
$path = $this->arrayManager->findPath($attribute, $meta, null, ‘children’);
$meta = $this->arrayManager->set(
“{$path}/arguments/data/config/disabled”,
$meta,
true
);