3

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?

4
  • check my answer and let me know if any issue. May 23, 2018 at 12:48
  • have you checked my solution ? @dileep
    – Divyesh
    May 23, 2018 at 13:13
  • Hi @ Divyesh, I have checked your solution It is not working. Can you please help me? May 28, 2018 at 5:09
  • Hi @ Divyesh, Please check my screenshot. prnt.sc/jng8ek May 28, 2018 at 5:11

4 Answers 4

0

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 ********/   
    }

}
?>
19
  • Hi @ Nimesh, Actually my issue is when I export products I got the error message "Please correct the data sent value." Because of this error "Call to a member function getName() on null in vendor/magento/module-catalog-import-export/Model/Export/Product.php". So I don't want to change core files. Please let me know the solution for this post without modifying core files May 24, 2018 at 6:34
  • Hi @ Nimesh, Can you please help me? May 24, 2018 at 6:59
  • Yes sure. I am honored to help you.
    – Nimesh
    May 24, 2018 at 8:39
  • I think you got getName() on null in "initCategories()" function so my suggestion is thet use beforeInitCategories() and set the logic as per that.
    – Nimesh
    May 24, 2018 at 8:44
  • HI @ Nimesh, it is not working. May 24, 2018 at 9:59
1

Create simple module and override model file.

Step 1.

Create a di.xml file in a following directory Company/CatalogImportExport/etc/

<?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="Magento\CatalogImportExport\Model\Export\Product" type="Company\CatalogImportExport\Model\Magento\CatalogImportExport\Export\Product" />

</config>

Step 2

<?php

namespace Company\CatalogImportExport\Model\Magento\CatalogImportExport\Export;


class Product extends \Magento\CatalogImportExport\Model\Export\Product
{
        public function __construct()
        {
            echo "Model Rewrite Working"; die();
        }
}
2
  • Hi @ Nikunj Vadariya , I have followed your steps but that override file is not affecting and the response came from core file only. May 23, 2018 at 13:04
  • Hi @ Nikunj Vadariya, Hi @ Nimesh, I want override core files like magento1 in magento2.In magento1 override root folder is app/code/local like that, Is it an option to override core files. May 25, 2018 at 5:09
1

it can easy

  1. Move/Copy to core module to - app/code/Magento/Wishlist
  2. List item edit composer.json and add

"replace": { "magento/module-wishlist": "*" },

more info by link - https://www.integer-net.com/removing-unused-core-modules-from-magento-2-the-right-way/

  1. run command SSH - composer update
  2. run command SSH - php bin/magento setup:upgrade
  3. check and remove module in vendor/magento/module-wishlist

Now, you can edit/remove logic of Magento standard modules

:)

-1

You have to create one new module in that "code" folder and override that Model's Product.php in that module.

3
  • Hi @ H.K Suthar, Is it mandatory to create the separate module for overriding above core file? I want to use default app/code/Magento to override. May 23, 2018 at 12:40
  • Yes its mandetory
    – H.K.Suthar
    May 23, 2018 at 14:14
  • Hi @ H.K.Suthar, Hi @ Nimesh, I want override core files like magento1 in magento2.In magento1 override root folder is app/code/local like that, Is it an option to override core files. May 25, 2018 at 5:09

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .