Question

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!

Was it helpful?

Solution

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;
    }
    
  • Check result before try my solution enter image description here

  • Check result after try my solution enter image description here

Let me know if you have any question !

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top