Can anyone help me how to override the following files to custom module
vendor/magento/module-sales/Model/Order/Item.php
vendor/magento/module-sales/Api/Data/OrderItemInterface.php
thanks
Create your own module.
In the di.xml
file of your module add this
<preference for="Magento\Sales\Api\Data\OrderItemInterface" type="[Vendor]\[Module]\Sales\Order\Item" />
<preference for="Magento\Sales\Model\Order\Item" type="[Vendor]\[Module]\Sales\Order\Item" />
Then create the class [Vendor]\[Module]\Sales\Order\Item
that extends Magento\Sales\Model\Order\Item
and implements Magento\Sales\Api\Data\OrderItemInterface
.
But I doubt you need both of them in di.xml
. I have a feeling that the second one only will solve your problem
Use preference
in Magento 2 to override any class but you can use plugins
to inject your method to after/around/before
any method for any class method. Please donot confuse plugins as extension, in Magento 2 plugin (a class ) is a way to inject your method to another method.
See preference examples in this Magento 2 documentation: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/build/di-xml-file.html
You just need to define preference for
and type
where for represent source class and type represent target class.
Updated: You have to define metadata class object so that your orderRepository can get it. So please use this as below: Define just after preference in your XML.
<type name="Ewall\Sales\Model\OrderRepository">
<arguments>
<argument name="metadata" xsi:type="object">orderMetadata</argument>
</arguments></type>