How to Override Core Block, Model & Controller in Magento 2

- Fire development, Magento 2

Magento 2 Development; Magento 2 tutorial

If you don’t know how to override a core model Block and controller in Magento 2. You’ve come to the right place. Although, it is a bad idea to override core classes in Magento 2, there are multiple ways to achieve this goal.

'

Let’s take an example of a list toolbar. You need to add a new sort option called “sort by most popular”. Magento 2 allows leveraging the concept of plugins to implement the improvement.

With the help of plugins, you can do whatever you want “after and before” the core function as well as “around”.

Create a di.xml file under Mymodule/etc/di.xml:

It illustrates Product Model and Product View Block.

Use “around” in the Product View block:

In the model, use “before and after”:

Thus, you can retain the old code so every Magento core code update provides you with the new code and your custom logic.

Check this article for further details. We hope this post was useful. It is based on the StackExchange question by Pradeep Kumar and answers by Pradeep Kumar. For more useful articles, check out our Cookbook.

'