Magento 2 - How to use plugin to change product visiblity after saving product from backend?

Magento 2 - How to use plugin to change product visiblity after saving product from backend?



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="MagentoCatalogModelProduct">
<plugin name="Custom-HelloWorld-product-model" type="CustomHelloWorldPluginProductPlugin" sortOrder="1" />
</type>



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


namespace CustomHelloWorldPlugin;

class ProductPlugin

public function beforeSetVisiblity(MagentoCatalogModelProduct $subject, $visiblity)

....




Please suggest. Thanks!





yes, you can go with plugin.
– kunj
Aug 30 at 6:54





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 at 7:10





Sure, you can learn more from here : mageplaza.com/magento-2-module-development/…
– kunj
Aug 30 at 7:10




1 Answer
1



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.





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 at 7:04




Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)