Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series.
Today, I am going to show everybody a best practice, How to override a method in Magento 2 by using the Plugin feature.
A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface.
Extensions that wish to intercept and change the behavior of a public method can create a Plugin class which is referred to as plugins.
Types Of Plugins In Magento 2
In Magento 2 we can create and use three types of Plugin.
1. Before methods
2. After methods
3. Around methods
Plugins cannot be used with any of the following:
1. Final methods
2. Final classes
3. Non-public methods
4. Static methods
6. Virtual types
7. Objects that are instantiated before MagentoFrameworkInterception is bootstrapped
8. Objects that are not instantiated by the ObjectManager (e.g. by using new directly).
To understand more about Plugin we will do a practice.
Use the plugin to update the content of the product attribute named description and short_description on the storefront. Find the text and replace it with a text link.
Okie, let’s go.
Let’s do this practice, you need to follow steps by step:
Step 1: Create a new module called PHPCuong_TextLink
– Create the namespace PHPCuong in the path appcode.
– Create the module named TextLink in the path appcodePHPCuong.
– Create the file named registration.php in the path appcodePHPCuongTextLink
– Create the file named module.xml in the path appcodePHPCuongTextLinketc
End of the step #1, I have been completed the step to create a new module called PHPCuong_TextLink.
Step 2: Declaring a plugin
– A plugin for a class object is declared in the di.xml file in the module.
– Create the new file named di.xml in the path appcodePHPCuongTextLinketcfrontend
I put the di.xml file in the folder named frontend because I want these codes only to apply on the storefront.
– You must specify these elements:
type name: A class or interface which the plugin observes.
plugin name: An arbitrary plugin name that identifies a plugin. Also used to merge the configurations for the plugin.
plugin type: The name of a plugin’s class or its virtual type. Use the following naming convention when you specify this element: VendorModulePluginModelNamePlugin.
Step 3: Defining a plugin
– A plugin is used to extend or modify a public method’s behavior by applying code before, after, or around that observed method.
– In this practice, I use the Around methods.
– Create a new file named Output.php in the path appcodePHPCuongTextLinkPluginCatalogHelperOutput.
Step 4: Test and see the results.
Run the comment lines following:
– php bin/magento setup:upgrade –keep-generated
– php bin/magento cache:flush
Go to the product page detail.
Yeah, it is working perfectly.
Thank you for your watching. If you have any questions about this practice please feel free to leave a comment below.
Don’t forget to like, comment, share and subscribe to my channel for getting the latest videos.
Please do not hesitate to contact me if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.
Get the source code of this practice in the github