1

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

2 Answers 2

2

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

1
  • thanks for the reply marius , can you please check the question again i have updated my di.xml file thanks
    – Raghu
    Commented Jan 23, 2018 at 9:24
2

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>
5
  • thanks for the reply Kumar, can you please check the question again i have updated my di.xml file thanks
    – Raghu
    Commented Jan 23, 2018 at 9:24
  • I have updated my answer see above.
    – Kumar M
    Commented Jan 23, 2018 at 9:38
  • If you want to do some changes via virtual type in your classes for order item and order class then you can keep it otherwise just remove it because it's already been defined in Magento it's self.
    – Kumar M
    Commented Jan 23, 2018 at 9:50
  • ok but iam facing errors, actually my intension is to get item ordered product image, I did it in vendor files and result is prnt.sc/i4c51b but unable to override to custom module
    – Raghu
    Commented Jan 23, 2018 at 9:59
  • What error you are getting
    – Kumar M
    Commented Jan 23, 2018 at 10:28

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.