1

In the magento 1 I can move a class from /core to /local. Then used the file from /local.

How to make this in Magento 2?

3 Answers 3

3

In Magento 2, you have to use preferences which is the closest of Magento 1 class rewrite. It is a way to provide a default implementation to classes.

For instance, if you want to rewrite the Magento\Catalog\Model\Product class in your module, you can create the following di.xml file:

<?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\Catalog\Model\Product" type="Vendor\ModuleName\Model\Product" />  

</config>

This way, your class Vendor\ModuleName\Model\Product will be used instead of the core Magento\Catalog\Model\Product class.

Your model should look like this:

<?php
namespace Vendor\ModuleName\Model;
class Productextends \Magento\Catalog\Model\Product
{
    public function theFunctionYouWantToRewrite()
    {
        // Do your custom stuff
    }
}

Don't forget that Magento 2 also benefits from plugins, which are suggested if you're not working on the entire class level but more on a method level:

It allows you to execute code before, around, or after methods from the class you're hooking onto. Your plugin class does not replace the target class, and it is not an instance of it. You just have methods before{method}, around{method}, after{method} which get executed at the appropriate time in respect to {method} on the target class.

More information can be found here about the differences between the two: Magento2: what is the basic difference between plugin and preference?

2
  • Yes, but if I have a lot of class inherits class A, I do not want to rewrite them all.
    – gebuket
    Mar 21, 2016 at 10:00
  • 1
    @gebuket Unfortunately, apart from plugins and preferences, Magento 2 does not provide the old Magento 1 "copy to local folder" method to rewrite a class. Mar 21, 2016 at 10:01
2

There are no code pools like (core, community and local) in Magento 2 any more. If you need to replace a class a preference would be a way to go about it.

However before using preferences I'd suggest checking if you could achieve your goal by using a plugin if your changes are only on the method level.

Again depending on your specific circumstances a viable option could also be to only swap out the class in one location (checkout type definitions in di.xml)

0

Both Plugins and Preferences are helpful in overriding the classes. However, Plugins are preferable than Preferences since plugins do not override the class logically instead it hooks our logic into the available classes.

So, to modify or extend any existing business logic, it is better to use the plugins. Preferences are not recommended unless or otherwise there is no other option.

source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.