📜 ⬆️ ⬇️

Do not rewrite classes in Magento

For about two years now I have been working closely with the Magento system. During this time, it was possible to work with foreign modules and projects, and I realized that the code in our company is at a good level. Probably because we narrowly specialize in Magento.

So, it is often necessary to observe errors and compatibility problems in foreign modules. The specific problem of most modules will be discussed in this article.

If you are new to Magento and want to somehow change or expand its basic functionality, then on the Internet you will most likely be advised:
1. make changes directly to core files;
2. copy the class file with folder structure from app / code / core to app / code / local and make changes to the local copy;
3. rewrite the class through the XML configuration in its module.
')
The first option should not be considered at all. The second is in rare cases if you really need to rewrite some abstract class or library class. The third is if you rewrite the class of a model, block, helper, or controller.

I offer the fourth option:
4. Do not rewrite classes in Magento!


Why the fourth option? The first two options are bad because they are not compatible with upgrading the kernel. And this is a weighty problem, since Magento develops quickly and versions come out consistently often. The option of rewriting via XML configuration allows you to inherit the rewritable class, so it is not so tied to the kernel version. But there is a significant disadvantage of this approach - if the same class rewrites another module, then only one version will be used, yours or someone else's. The priority is alphabetic and will depend on the name of your module. That is, if your module is called Abc_ModuleOne, and the foreign one is Zyx_ModuleTwo, then, alas, your rewrite will not be used. And even if you call your Zzzzzz_Zzz module, the problem will not go away - after installing your module, someone will break something and the store owners will blame you in the end.

The alternative is the use of event observers, the benefit of events in Magento is quite a lot. Do not be lazy, look for an event in the core that you would be comfortable using. If no such event was found, try using the one closest in the course of the program. In many cases, the task can be solved using event handlers. How to use observers in Magento can be easily found on the Internet, but I can prepare an article here if it is interesting.

For some problems, there are specific solutions. For example, if you somehow need to change the method of calculating the total amount of the order or some component of this amount. All this in Magento is called totals. In the basic delivery there are subtotal, shipping, tax, discount, grandtotal, which are performed in a specific order. But you can easily add your handler anywhere on the totals chain via XML. Payment methods and shipping methods are also easily added. If you wish to comment, I can highlight these topics in more detail.

Source: https://habr.com/ru/post/126887/


All Articles