How to override model in Magento 2?

by aniya.jaskolski , in category: PHP CMS , 2 years ago

How to override model in Magento 2?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by deron , a year ago

@aniya.jaskolski 

To override a model in Magento 2, you will need to create a custom module and use a preference to specify your custom model class in the di.xml file.


Here's an example of how to do this:

  1. First, create a custom module by creating the following directory structure in your Magento installation: app/code/YourCompany/YourModule.
  2. Create a registration.php file in the module root directory with the following code:
1
2
3
4
5
6
<?php
MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'YourCompany_YourModule',
    __DIR__
);


  1. Create a module.xml file in the etc directory with the following code:
1
2
3
4
5
6
7
8
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="YourCompany_YourModule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>


  1. Create a di.xml file in the etc directory with the following code:
1
2
3
4
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="MagentoCatalogModelProduct" type="YourCompanyYourModuleModelProduct" />
</config>


  1. Create a Product.php file in the Model directory with the following code:
1
2
3
4
5
6
7
<?php
namespace YourCompanyYourModuleModel;

class Product extends MagentoCatalogModelProduct
{
    // your custom code goes here
}


This will override the MagentoCatalogModelProduct model with your custom model class. You can now add your custom code to the Product class and it will be used instead of the original model class.


Remember to run the following commands to enable the module and clear the cache:

1
2
3
php bin/magento module:enable YourCompany_YourModule
php bin/magento setup:upgrade
php bin/magento cache:clean


Member

by addison , 10 months ago

@aniya.jaskolski 

To override a model in Magento 2, follow these steps:

  1. Create a new module: Create the necessary files and folders for your module (registration.php, etc.). Enable the module by running the following commands in the root directory of your Magento installation: bin/magento module:enable Namespace_Module bin/magento setup:upgrade
  2. Create a di.xml file: In your module's etc folder, create a di.xml file. In the di.xml file, define the preference for the model you want to override. For example:
  3. Create your overridden model: In your module's Model folder, create a new file with the same name and namespace as the original model you want to override. Extend your new model class from the original model class. For example:
  4. Flush Magento cache: Run the following command in the root directory of your Magento installation to flush the cache: bin/magento cache:flush


After following these steps, Magento 2 will use your overridden model instead of the original model. You can add your custom code or override methods in the new model according to your requirements.