Create an around plugin in Magento
Modify the behavior of a public method in Magento by creating an around plugin.
Fresh bytes every Thursday. No spam, ever. Unsubscribe anytime.
Join 9,000+ developers and get three free video lessons every week.
Around plugins let you wrap existing Magento functions with your own code. They're incredibly powerful - you can run code before or after the original function, modify its parameters, or even prevent it from running entirely.
Important note: Around plugins are the most powerful but also the most dangerous plugin type. They can make debugging tricky and increase the size of the call stack, which impacts performance. Before reaching for an around plugin, first check if before/after plugins or class preferences could solve your problem instead.
Let's build some functionality that blocks customer logins coming from specific email domains. We can do this by tapping into any public function that allows us to carry out our task.
We'll create a plugin for the Customer model's loadByEmail
function.
Now let's create our plugin class, naming it with a descriptive name of the action it is doing:
Our plugin method needs two or more parameters:
$subject
- The original class instance we're plugging into$proceed
- A callable that lets us execute the original function- Then, any parameters from the original function (in this case,
$email
)
Let's implement our domain blocking logic:
This code first runs the original function using$proceed
. Then it checks if the email contains "example.com". If it
does, we mark the account as inactive and increment the failure counter. We then either return the original customer
data, or the modified version with the account marked as inactive β effectively blocking these users from logging in.
Finally, we need to tell Magento about our plugin. We'll do this by adding it to our module'sdi.xml
file. Itβs also a
good idea to prefix our plugin names with our vendor and module name to avoid potential namespace collisions:
And we also need to tell Magento to recompile dependencies by either running setup:upgrade
, or removing related
classes in the generated/code
directory:
That's it! Our plugin will now block any login attempts which originate from our specified domain.