2

I am new in Magento 2 and I want to change product visibility after saving product from backend using plugin.

For this I have created custom module and plugin, but I am not sure that we can achieve this using of plugin or not? Or Should I go with Observer?

I am trying something like : -

app/code/Custom/HelloWorld/etc/di.xml

<type name="Magento\Catalog\Model\Product">
        <plugin name="Custom-HelloWorld-product-model" type="Custom\HelloWorld\Plugin\ProductPlugin" sortOrder="1" />
    </type>   

app/code/Custom/HelloWorld/Plugin/ProductPlugin.php

namespace Custom\HelloWorld\Plugin;

class ProductPlugin
{    
    public function beforeSetVisiblity(\Magento\Catalog\Model\Product $subject, $visiblity)
    {
        ....
    } 
}

Please suggest. Thanks!

3
  • yes, you can go with plugin.
    – kunj
    Aug 30, 2018 at 6:54
  • 1
    Thanks for your suggestion. I also have to update DB value as well. Can you please give me any doc links so I can get more information and implement this in our module.
    – Mr. Nanhe
    Aug 30, 2018 at 7:10
  • Sure, you can learn more from here : mageplaza.com/magento-2-module-development/…
    – kunj
    Aug 30, 2018 at 7:10

1 Answer 1

5

You should use plugin not observer, there is something need to understand that where to use plugin and where to use observers :

Use Plugins : If you are going to change input, output ( i.e set / save data ) of some function or changing flow of request then should use plugin.

User Observer : If you are not going to change the flow of request and intended to perform some other action on event then use Observer.

1
  • 1
    Thanks for your kind suggestion. I have to update DB value as well. Can you please give me any doc links so I can get more information and implement this in our module.
    – Mr. Nanhe
    Aug 30, 2018 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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