How to Override Core Files in app/code/Magento Folder in Magento 2.2.2

magento2.2.2override-modeloverrides

How do I override core files in app/code/Magento folder in Magento 2?

I want to change core functionality so I need to override core files in the app/code/Magento folder instead of directly changing that core files.

Core file:

vendor/magento/module-catalog-import-export/Model/Export/Product.php

Overriden at:

app/code/Magento/CatalogImportExport/Model/Export/Product.php

But it is not working. I don't know how to implement this task. Can you please help me?

Best Answer

You can override model using plugin features in Magento2

for that Create a custom module and in di.xml put code like below

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogImportExport\Model\Export\Product">
        <plugin name="override-module-export-product" type="Nimesh\HelloWorld\Plugin\Export\Product" sortOrder="1" />
    </type>
</config>

You can override model function using before, after & around method. In below example, We can override export function behavior using before and after method

Create class file like this (app/code/Nimesh/HelloWorld/Plugin/Export/Product.php)

namespace Nimesh/HelloWorld/Plugin/Export;

class Product
{   

    public function beforeExport(\Magento\CatalogImportExport\Model\Export\Product $product)
    {
        /******put your logic *******/            
    }


    public function afterExport(\Magento\CatalogImportExport\Model\Export\Product $product)
    { 
        /********put your logic ********/   
    }

}
?>
Related Topic