Magento 2 | How to use plugin in magento 2 | "Full Practical" | Start to End

Channel:
Subscribers:
714
Published on ● Video Link: https://www.youtube.com/watch?v=1eFj79Z9aHI



Duration: 14:42
1,422 views
0


In this video 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.

In Magento2 we can create and use three types of Plugin.
1.Before listener Plugin
2.After listener Plugin
3.Around listener Plugin

Limitations

Plugins can not be used on following:

1.Final methods
2.Final classes
3.Non-public methods
4.Class methods (such as static methods)
5.__construct
6.Virtual types
7.Objects that are instantiated before Magento\Framework\Interception is bootstrapped

To understand more about plugin we will do a practice.

Lets do this practically, you need to follow step by step

Step 1: Create a new module called Techone_PluginTutorial
- Create the namespace Techone in the path app\code.
- Create the module name PluginTutorial in the path app\code\Techone.
- Create the file named registration.php in the path app\code\Techone\PluginTutorial

- Create the file name module.xml in the path app\code\Techone\PluginTutorial\etc

End of step #1, I have been completed the step to create new module called
Techone_PluginTutorial

Step 2: Declaring a plugin
- A plugin for class object is declared in the di.xml file in the module.
- Create the new file named di.xml in the path app\code\Techone\PluginTutorial\etc\frontend
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: \Vendor\Module\Plugin\ClassName.

Step 3: Defining a plugin
- A pugin is used to extend or modify a public method's behaviour by applying code
before, after, or around that observed method.
- In this practice, I use the Before method
- Create a new file named Cart.php in the path app\code\Techone\PluginTutorial\Model\Cart

Before listener is called by adding prefix 'before' to the method name and setting first letter
of original method to capital.Now method addProduct will become beforeAddProduct.

The before listener methods do not need to have a return value.
Here we are changing parameters. We set quantity to 10.
Now it will always add 10 quantities of the product whenever we add product to cart.
So we will use before listener when we want to change parameter of an method.

Step 4: Test and see the results
Run the command lines following:
- php bin/magento setup:upgrade --keep-generated
- php bin/magento cache:flush
Go to the product page detail.

Thank you for your watching video. if you have any questions about this practice please feel free to leave a comment below.

Get Source code : https://github.com/rajupndt06/magento2-plugintutorial/tree/master

magento 2 plugins,
how to use plugin in magento 2,
plugin in magento 2,magento 2 plugins before,
magento 2 plugins after,magento 2 plugins around,
magento 2 plugin development tutorial,
magento 2 plugin development,
magento 2 plugin installation,
magento 2 create plugin




Other Videos By Raju Pandit


2020-05-01How to create certificate design in ms word | step by step | Easy Tutorial | Complete Guide
2020-04-30How to create custom admin menu in magento 2 step by step | Easy Tutorial | Complete Guide
2020-04-29Magento 2 Create : View | Blocks, Template, Layout | Learn In Simpleway | Full Guide
2020-04-28Error Free | Installation Magento 2.3 | Start to End | Xampp | Localhost
2020-04-26How To Create The Matrix Rain In Command Prompt | step by step | Complete Tutorial
2020-04-26How to upload project or files to Github Repo with Git Bash | step by step | Complete Tutorial
2020-04-25How to import sql in phpmyadmin using command line | XAMPP |step by step | Complete Tutorial
2020-04-25How to export sql in phpmyadmin using command line | XAMPP | step by step | Complete Tutorial
2020-04-24Magento 2 | How to create controller in magento 2 | "Full Practical" | Start to End
2020-04-23Magento 2 | How to use AJAX in magento 2 | "Full Practical" | Start to End
2020-04-21Magento 2 | How to use plugin in magento 2 | "Full Practical" | Start to End
2020-04-20"READ More!" - Warren Buffett | How reading book helps in technical life | Reading books Benefits
2020-04-19PHP 7 | PHP String | Explained Simpleway | FULL PRACTICAL !!
2020-04-17PHP 7 | PHP Data types | Explained Simpleway | FULL PRACTICAL !!
2020-04-16PHP 7 | PHP Comment | PHP Variables | PHP echo & print | FULL PRACTICAL !! | Explained in Simpleway
2020-04-16PHP 7 | PHP Syntax | Everyone Should know !!| New release PHP
2020-04-15What's new in PHP 7 | What Can PHP 7 Do | Everyone Should know !!| New release PHP
2020-04-14Create Simple Login Page with PHP and MySql | Full Source Code | Complete Practical | Tutorial
2020-04-10How to add function in module step by step | Complete Tutorial
2020-04-09How to create module in magento 2 step by step | Easy Tutorial | Module Development
2020-04-08How to change localhost to custom domain name | Tutorial Simple way | Localhost | Xampp



Tags:
magento 2 plugins
how to use plugin in magento 2
plugin in magento 2
magento 2 plugins before
magento 2 plugins after
magento 2 plugins around
magento 2 plugin development tutorial
magento 2 plugin development
magento 2 plugin installation
magento 2 create plugin