pluginy magento 2

Magento, as one of the world's most popular e-commerce platforms, offers advanced capabilities for customizing and extending the functionality of online stores. One of the key mechanisms that enable flexibility and extensibility in Magento is plugins. In this article, we will explore what plugins are in Magento, how they work, and why they are incredibly useful.

What Are Magento Plugins?

Plugins in Magento are a mechanism that allows you to execute your custom code before or after calling a specific function (method) in the application. This allows for the modification or extension of the behavior of a function without the need to modify its original code.

Plugins are defined in the etc/di.xml file, and if you define a plugin in a specific area, it will only work there, such as in the web API zone.

When Can Plugins Be Used?

Magento plugins have certain limitations, and they cannot be used in every case. Here are a few cases where plugins cannot be used:

  • When the method or class has a "final" specifier.
  • When the function is not public.
  • When the function is static.
  • On constructors and destructors of classes.
  • If the function is a virtual type.
  • When the object implements the Magento\Framework\ObjectManager\NoninterceptableInterface.
  • If the object instance is created before the Magento\Framework\Interception is executed.

In Magento 2, there are three main types of plugins:

  • Before
    "Before" plugins allow you to execute your custom code before calling a specific function. They can modify the arguments passed to the function or even prevent the function from executing if necessary. They are used when you want to introduce changes or validations before performing a specific operation.
  • Around
    "Around" plugins allow you to control the invocation of a function. This means you can execute your custom code both before and after calling the function, or even completely replace the function with your own implementation. This is useful when you need full control over the function's behavior.
  • After
    "After" plugins allow you to execute your custom code after a specific function has completed. They are used when you need to take actions after a particular operation, such as modifying the function's result or performing additional operations based on the result.

Here are a few examples of how plugins can be used:

In the plugin tag, you can add attributes (they are optional):

  • sortOrder - controls the execution order of plugins. First, those without a defined sortOrder are executed, and then in ascending order based on the value provided in the attribute.
  • disabled - this attribute defines whether the plugin is enabled or not, typically used when there is a need to disable a plugin.

Plugins have a specific naming convention, which means you write a function name starting with the plugin type (before/around/after), followed by the function name it applies to, following the camelCase rules. For example, beforeSave, aroundSave, afterSave.

Example of a "before" plugin:

In this example, we see a plugin that will execute before the order is saved. The first parameter ($subject) is mandatory, and subsequent parameters appear if the function is executed with this parameter (all the parameters with which the function is executed can be included in this case, such as save).

Example of an "around" plugin:

We can see here a classic example of an "around" type plugin. The mandatory variables are $subject and $proceed, and the subsequent attributes are the parameters of the function that the plugin intercepts. The key in this type of plugin is the $proceed parameter, which triggers the base method.

Example of an "after" plugin:

In this example, we have presented a plugin that executes after the getTotals function. We can see an additional parameter ($result) next to $subject. It holds the value returned by the function, allowing us to modify, for example, the returned value of the function.

Conclusion

In summary, plugins are an incredibly important element of the Magento ecosystem, enabling the customization, extension, and modification of e-commerce store functionalities. They empower developers to easily introduce their own solutions before or after existing operations, enhancing the platform's flexibility and efficiency. The value of plugins in Magento lies mainly in their ability to provide nearly limitless possibilities for store development and personalization.