0

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!

1

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 !

| improve this answer | |
  • Thanks for your advice. But for this we will have to make on core level changes that is not a good practice. – Mr. Nanhe Aug 31 '18 at 3:57
  • You can overwrite this model file by di.xml in your custom module, then it will not related with core level – Magetop E-commerce Aug 31 '18 at 6:49
  • Ok, you can vote and accept my question, thanks – Magetop E-commerce Sep 1 '18 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.