2

I am trying to override Magento Payment Helper data in my extension but it's not working.

I have overriden by using the preference method.

I have put below code in app/code/Demo/PaymentTest/etc/di.xml

<preference for="Magento\Payment\Helper\Data" type="Demo\PaymentTest\Helper\Data" />

Here is my Helper code

<?php
namespace Demo\PaymentTest\Helper;
class Data extends \Magento\Payment\Helper\Data
{
    public function getStoreMethods($store = null, $quote = null)
    {
    }
}

I am trying to override getStoreMethods method in my helper but it's not calling it.

Can you please tell me what am I doing wrong?

4
  • What are you trying to achieve ? Depending on the reason why you want to rewrite that method using plugins would be more appropriate Jan 2, 2017 at 11:30
  • Please share code how can i rewrite that method by using plugins? I want to restrict payment method. Jan 2, 2017 at 11:33
  • 2
    @SanjayJethva, what Magento version do you use? Since Magento 2.1.3 \Magento\Payment\Helper\Data::getStoreMethods is deprecated and doesn't call anymore, use \Magento\Payment\Model\PaymentMethodList::getActiveList instead
    – joni jones
    Jan 3, 2017 at 8:36
  • Hello @jonijones, Its working and thanks for your answer. Jan 3, 2017 at 10:42

1 Answer 1

0

If you want to rewrite payment method, I suggest you use a plugin.

Create the following di.xml in the etc folder of your module:

<config>
    <type name="Magento\Payment\Helper\Data">
        <plugin name="restrictPaymentMethods" type="Demo\PaymentTest\Plugin\RestrictPaymentMethods" sortOrder="1" />
    </type>
</config>

Then create Plugin\RestrictPaymentMethods.php :

<?php

namespace Demo\PaymentTest\Plugin;

class RestrictPaymentMethods
{

    public function afterGetStoreMethods(\Magento\Payment\Helper\Data $subject, $result)
    {
        // $result will contain the original result
        // You can modify the $result array to restrict the payment methods
    }
}
1
  • Hello @raphael-at-digital-pianism, Thanks for your answer. I have tried your code but its not working. File is calling but method is not calling. Thanks Jan 3, 2017 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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