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!

2 Answers 2

4

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 !

3
  • 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, 2018 at 3:57
  • You can overwrite this model file by di.xml in your custom module, then it will not related with core level Aug 31, 2018 at 6:49
  • Ok, you can vote and accept my question, thanks Sep 1, 2018 at 7:17
0

For people coming here later, I think following solution is better than editing core files. And if you prefer after plugin that is also great and I think more suitable even than this solution.

My product attribute code was 'disable_by_brand'

etc\di.xml

<preference for="Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider" type="Jf\Brand\Ui\DataProvider\Product\Form\ProductDataProvider" />

Jf\Brand\Ui\DataProvider\Product\Form\ProductDataProvidore.php

namespace Jf\Brand\Ui\DataProvider\Product\Form;

class ProductDataProvider extends \Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider
{

    public function getMeta()
    {
        $meta = parent::getMeta();
        //unset disabled by brand attribute so that it won't appear on backend edit page
        unset($meta["product-details"]["children"]["container_disable_by_brand"]);
        return $meta;
    }
}

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.