In our daily coding routine we need to customization with core files. For that we need to overriding or rewrite the core classes because modify the core classes is not good practice. If you modify the code classes when you update the new version your code will be loss. So today we learn Magento 2 overriding rewriting model. In start we are overriding core Product model. We are jut checking our rewriting model is working or not. You can modify the model as you want by following this tutorial. But first you need to know How to create module in Magneto 2.For any suggestions & question, please feel free to drop a comment.
Overriding or Rewrite Magento 2 Model
Magento 2 Models play an even bigger role, as they typically contain the “Business Logic” that’s often relegated to the Controller or Helper methods in other PHP MVC frameworks. Now we start to overriding or rewriting core Product model. The model we are overriding is Product Model.
Create a di.xml file in a following directory QaisarSatti/HelloWorld/etc/
<preference for="Magento\Catalog\Model\Product" type="QaisarSatti/HelloWorld/Model/Rewrite/Catalog/Product" />
The step2 to overriding or rewriting Magento2 model is to create a Product.php helper file in the following directory QaisarSatti/HelloWorld/Model/Rewrite/Catalog
* Catalog Product Rewrite Model
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
class Product extends \Magento\Catalog\Model\Product
public function __construct()
echo "Overrding/ Rewrite Working"; die();
As following code you can rewrite or override any Magento 2 model using the same method.