Magento 2: How to disable or remove Product SKU field in the admin panel
-
13-03-2021 - |
Solution
You can do it by creating a custom module.
Create below required files for the module under below path:
app/code/Vendor/Module/registration.php
app/code/Vendor/Module/etc/module.xml
Create di.xml
file under app/code/Vendor/Module/etc/adminhtml
with below content.
<?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="sku" xsi:type="array">
<item name="class" xsi:type="string">Vendor\Module\Ui\DataProvider\Product\Form\Modifier\Attributes</item>
<item name="sortOrder" xsi:type="number">1000</item>
</item>
</argument>
</arguments>
</virtualType>
</config>
And create Attributes.php
file under app/code/Vendor/Module/Ui/DataProvider/Product/Form/Modifier/
with below content.
<?php
namespace Vendor\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 = 'sku'; //attribute code to disable
$path = $this->arrayManager->findPath($attribute, $meta, null, 'children');
$meta = $this->arrayManager->set(
"{$path}/arguments/data/config/disabled",
$meta,
true
);
return $meta;
}
}
This will disable the attribute.
After placing module in your Magento directory, you need to run the below commands to enable the module.
php bin/magento module:enable Vendor_Module
php bin/magento s:up
php bin/magento s:s:d
php bin/magento c:f
Hope it helps!!!
OTHER TIPS
Yes you can make it not Editable as:
Create file in Vendor/Module/etc/adminhtml/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_edit_action">
<observer name="vendor_admin_lock" instance="Vendor\Module\Observer\ProductLockAttributes" />
</event>
</config>
Create file in Vendor/Module/Observer/ProductLockAttributes.php:
class ProductLockAttributes implements ObserverInterface
{
/**
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$event = $observer->getEvent();
$product = $event->getProduct();
$product->lockAttribute('sku');
$product->lockAttribute('sku_type');
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange