0

I have to add a work around fix for a payment method by adding a line of code to vendor/magento/module-payment/Helper/Data.php.

I've tried my fix by adding it directly to that file and it's working, but since files in the vendor folder shouldn't be modified, I'll have to put that fix in app/code.

I've tried adding a copy of the file along with my modified code in app/code/Magento/Payment/Helper/Data.php instead. Nothing happens and the bug is still there and I can't find any documentation on this.

How do I override that specific file?

Dhaduk Mitesh
1,6951 gold badge13 silver badges29 bronze badges
asked Jun 12, 2018 at 8:46

2 Answers 2

1

To override a specific class one should use Dependency Injection.

More specifically di.xml and the preference node.

Examples and docs

https://devdocs.magento.com/guides/v2.0/extension-dev-guide/build/di-xml-file.html http://www.coolryan.com/magento/2016/01/22/preferences-in-magento-2/

Another good explication can be found here https://alanstorm.com/magento_2_object_manager_preferences/

answered Jun 12, 2018 at 9:29
0
1

Create your own module and add below code to,

app/code/YourCompany/YourModule/etc/di.xml

<?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\Payment\Helper\Data" type="YourCompany\YourModule\Helper\Payment\Data" />
</config>

Now create your helper file Data.php under below location,

app/code/YourCompany/YourModule/Helper/Payment/Data.php

And the below code to Data.php

<?php
namespace YourCompany\YourModule\Helper\Payment;
class Data extends \Magento\Payment\Helper\Data
{ 
 ...Your Code...
}
?>

Now add your modified code to that file.

Dhaduk Mitesh
1,6951 gold badge13 silver badges29 bronze badges
answered Jun 12, 2018 at 10:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.