Magento 2 - How to hide/remove custom attribute from backend product edit page?
-
24-01-2021 - |
سؤال
I've created a custom attribute yes/no with default value no.
If I update the product with custom attribute value yes then attribute should not be visible on the edit page, if with no value then should be visible.
How can we achieve this?
Thanks!
المحلول
This is very simple, follow steps below :
Go to the file
/vendor/magento/module-catalog/Ui/DataProvider/Product/Form/ProductDataProvider.php
Then you can remove/hide product attribute by function getMeta() in ProductDataProvider.php file on line 67
public function getMeta() { $meta = parent::getMeta(); /** @var ModifierInterface $modifier */ foreach ($this->pool->getModifiersInstances() as $modifier) { $meta = $modifier->modifyMeta($meta); } return $meta; }
Try code below for hide/remove any attribute you want, you can check condition before hide/remove your attribute for case : (If I update the product with custom attribute value yes then attribute should not be visible on edit page, if with no value then should be visible), my example is price attribute
unset($meta['product-details']['children']['container_price']['children']['price']);
Full code
public function getMeta() { $meta = parent::getMeta(); /** @var ModifierInterface $modifier */ foreach ($this->pool->getModifiersInstances() as $modifier) { $meta = $modifier->modifyMeta($meta); } //unset price attribute of product unset($meta['product-details']['children']['container_price']['children']['price']); return $meta; }
Let me know if you have any question !