1

When overriding a class with a custom one using the di.xml, are there any restrictions on what class you can override?

I have tried overriding a class of a third party module, but it does not appear to work. This class does not implement any interface, so I thought that maybe it's only interface implementations that can be used here?

Example:

<?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="Amasty\Feed\Model\Export\Adapter\Csv" type="Engmaso\Feed\Model\Export\Adapter\Csv" />
</config>

So here I'm trying to simply add my class in place of the Amasty one, but my class is never loaded.

  • abstatact class ?? – Rk Rathod May 11 at 9:42
  • If interface implemented with class then, it would be great you caninterface override using preference in di.xml file. – Pratik Mehta May 11 at 10:37
0

When you override the class make sure below changes are verified.

<config>
    <preference for="Magento\Catalog\Api\Data\ProductInterface" type="Test\Catalog\Model\Product" />
</config>

and your new class should extend existing class. please see below example

<?php
namespace Test\Catalog\Model;

class Product extends \Magento\Catalog\Model\Product
{
// code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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