Brief Summary
This video explains how to modify Magento's core functionality without directly altering the core files, which is crucial for maintaining upgrade compatibility. It covers three primary methods: plugins, events/observers, and preferences, outlining their use cases and providing practical examples. The video emphasizes using plugins as the preferred method, followed by events/observers, and finally, preferences as a last resort.
- Plugins: Best for modifying public methods.
- Events/Observers: Useful when plugins can't be used.
- Preferences: A last resort for changing class behavior.
Introduction
The video addresses the common need to customize Magento's functionality and highlights the dangers of directly modifying the core code. Direct modifications can be overwritten during Magento upgrades, causing loss of functionality. The tutorial introduces three methods to safely alter Magento's behavior: plugins, events/observers, and preferences.
Demonstration Setup
The presenter sets up a demo Magento store with sample products and a custom module. The goal is to modify the product names on the listing page based on their price: products under $70 will have "so cheap" appended to their name, while those over $70 will have "so bloody expensive" added. The video starts by illustrating the incorrect approach of directly changing the core files.
What Not to Do: Direct Core Modification
The video shows the temptation of directly modifying the getName
function in the Product.php
file within Magento's core catalog module. By changing the function to return "blah blah," the presenter demonstrates how easy it is to alter the displayed product names. However, this approach is strongly discouraged due to the risk of losing changes during upgrades.
Safe Modification Methods: Plugins, Events, and Preferences
The video introduces three safe methods for modifying Magento's functionality: plugins, events/observers, and preferences. Plugins are the preferred method for modifying public methods of classes. Events and observers are used when plugins are not suitable. Preferences are considered a last resort when other methods fail.
Plugins Explained
Plugins can modify public methods of classes, with three types available: "before," "after," and "around." "Before" plugins execute before the original method, "after" plugins execute after, and "around" plugins execute both before and after. The "around" plugin uses a callable method to control the execution of the original function. For the product name modification, an "after" plugin is chosen to append text to the existing name.
Implementing a Plugin
The presenter creates a di.xml
file in the module's frontend
area to register the plugin. The type name
attribute specifies the class to be modified (Magento\Catalog\Model\Product
), and the plugin name
and type
attributes define the plugin's class and name. The plugin class (Codilar\Demo\Plugin\Product
) contains the afterGetName
function, which takes the product object and the original name as parameters, appends the appropriate text based on the price, and returns the modified name.
Testing the Plugin
After implementing the plugin, the product names on the listing page are updated to include "so cheap" or "so bloody expensive" based on the product price, demonstrating the successful modification of the core functionality using a plugin.
Events and Observers Explained
Events and observers allow intercepting Magento's flow by "observing" events fired during the application's execution. For example, the customer_login
event can be observed to trigger actions after a customer logs in. In this demonstration, events and observers are used to modify product names, similar to the plugin example.
Implementing Events and Observers
The presenter identifies the catalog_block_product_list_collection
event, which is fired after the product collection is initialized. An events.xml
file is created to register an observer for this event. The observer class (Codilar\Demo\Observer\Product
) implements the ObserverInterface
and contains the execute
function. This function retrieves the product collection from the event data, iterates through the products, and modifies the names based on the price.
Testing Events and Observers
After implementing the event and observer, the product names are again updated to include "so cheap" or "so bloody expensive," demonstrating the successful modification using events and observers.
Preferences Explained
Preferences allow replacing a class with a custom class. When Magento creates an object of the original class, it will instead create an object of the preferred class. This method can change almost any functionality in Magento.
Implementing Preferences
A preference is set in the di.xml
file, mapping the Magento\Catalog\Model\Product
class to a custom class (Codilar\Demo\Model\Product
). The custom class extends the original product class and overrides the getName
function. The overridden function calls the parent's getName
function to get the original name and then appends the appropriate text based on the price.
Testing Preferences
The product names are updated once more, demonstrating the successful modification using preferences. The presenter reiterates the order of preference for these methods: plugins, events/observers, and preferences.
Conclusion
The video concludes by summarizing the three methods for modifying Magento's core functionality without directly altering core files. Plugins are the preferred method, followed by events/observers, and preferences as a last resort.