Magento 2: How to disable or remove Product SKU field in the admin panel see image below for more clarification.
2 Answers
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!!!
-
facing an issue when i run that command php bin/magento module:enable Vendor_Module. The error coming is There are no commands defined in the "module" namespace. Commented Sep 13, 2019 at 13:41
-
From where you've run the command?? Follow this guide to enable the module on the website devdocs.magento.com/guides/v2.3/extension-dev-guide/build/…– SumitCommented Sep 13, 2019 at 13:55
-
I run from Magento root directory and other commands not working as well like (php bin/magento cache:flush) when i delete that folder(Vendor) from app/code then everything working fine and commands also working as well. Commented Sep 13, 2019 at 14:00
-
OK, you need to change the Vendor and module folder name. Also in the file I've shared– SumitCommented Sep 13, 2019 at 14:02
-
1Yes, Brother, I tried and it's working fine. I have one more question about how we can write a custom CSS for admin panel? For example, I want to hide add product button how I can hide using CSS. Commented Sep 16, 2019 at 10:26
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');
}
}
-
-
Yes exactly, you can add this in your existing module or create new one. Commented Sep 13, 2019 at 13:30