I'm trying to make product SKU attribute not editable in Admin Products->Inventory->Catalog.
I'm using this previous response as an example: Magento read-only and hidden product attributes
I've created an Observer to use lockAttribute("attribute_code") method.
Here is the 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>
And in 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');
}
}
The event gets called but in the admin interface when editing a product the SKU is still editable.
Can anyone tell me what is wrong with my code?
Thanks.