In this article, you going to see a discussion regarding Magento 2 Disable Payment Method Programmatically in a detail and step by step with a code snippet.
If you looking for this topic then you are at right place.
Let’s start with the topic for Magento 2 Disable Payment Method Programmatically.
There many approach to disable payment in Magento 2.
One method to disable the payment method is log in to the admin panel.
In general configuration section for payment method set the status to no.
If in the admin section there is no option is given then you can set the payment module from 1 to 0 in the config file.
This file is present under app/etc
folder.
Example to disable the payment module/extension "VendorName_ModuleName" = 0 from 1
.
Now we going see how to do programmatically.
We going to use event method.
Using event you can disable the payment method programmatically.
In the module create a file named event.xml file under module folder.
Below is path for event file.
app/code/Techone/Todisablemodule/etc
Below is code snippet for event.
<?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="Techone\Todisablemodule\Observer\PaymentAvailable" />
</event>
</config>
Our next step is to create the observer file named as PaymentAvailable.php
file.
Below is the path for observer file.
app/code/Techone/Todisablemodule/PathtoObserver/
In observer, you have to check payment method code.
If the payment method code exist then set to false.
Below is code snippet for the observer file.
<?php
namespace Techone\Todisablemodule\PathtoObserver;
use Magento\Framework\Event\ObserverInterface;
class PaymentAvailable implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
// replace "checkmo" with your payment available method code eg: sagepay.
if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo"){
$checkResult = $observer->getEvent()->getResult();
$checkResult->setData('is_available', false);
}
}
}
Magento 2 Call Helper Function in phtml
Conclusion:
From above example you come to understand in detail in simple way and step by step regarding Disable Payment Method Programmatically.
I hope you like this article and if you have any query, please comment below.