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?

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/

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.

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.