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!
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.
yes, you can go with plugin.
– kunj
Aug 30 at 6:54