3

I want to disable a specific payment method on the checkout page based on some criteria.

I use the "get_shipping_info" event in the di.xml and make in the "afterSaveAddressInformation" function a customer check and want to disable some payment methods based on the check result.

I have no idea how I can disable a specific payment method.

I tried it with:

\Magento\Framework\App\Config\ScopeConfigInterface $this->payment

$payment_methods = $this->payment->getValue("payment");
    foreach ($payment_methods as $method) {
        if($method['code'] == "checkmo")
        $method->setData("is_available", false);
    }

But with this I get only an array with all available payment methods.

3
  • Welcome to Magento StackExchange, have you tried disabling check money order through the Admin Panel? Unless you need it to disable it programatically based on certain conditions, that is the way to do it.
    – diazwatson
    Aug 8, 2017 at 9:47
  • Yes i need to do it programatically but when i try it on this way i get a error: [error] 10#10: *2803 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to a member function setData() on array Aug 8, 2017 at 10:08
  • please try this webkul.com/blog/… Aug 8, 2017 at 12:07

1 Answer 1

2

/app/code/Company/Module/etc/events.xml

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="payment_method_is_active">
        <observer name="custom_payment" instance="Company\Module\Observer\PaymentMethodAvailable" />
    </event>
</config>

app/code/Company/Module/Observer/PaymentMethodAvailable.php

<?php

namespace Company\Module\Observer;

use Magento\Framework\Event\ObserverInterface;


class PaymentMethodAvailable implements ObserverInterface
{
    /**
     * payment_method_is_active event handler.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // you can replace "checkmo" with your required payment method code
        if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo"){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
        }
    }
}

Note: After did the modifications use di:compile command and remove cache and page cache and check the same in the frontend.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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