• USD
  • Portuguese
Magento 2: Disable Payment Method Programmatically

After reading the title, you may be wondering why any store owner would want to disable any payment method. After all, the common practice is to offer the greatest number of payment methods as it enhances the customer experience. While that is certainly true, disabling a payment method can be beneficial and, in some cases, mandatory. Before we discuss about Magento 2 disable product programmatically, let’s talk about why you may need to disable a payment method.

Reasons to Disable a Payment Method in Magento 2

Security Issues

There are cases where a payment method may be compromised. Naturally, you’ll want to protect your users and business against payment-related hassle. Thus, the only option is to disable the compromised method temporarily or even permanently.

Regulatory Requirement

Visa and Mastercard were forced to suspend their operations in Russia following the sanctions against the country after its invasion of Ukraine. There may be situations where a specific payment method is banned in a country. Thus, there is no option but to disable that method to ensure compliance.

Cost Management

Apart from the above two reasons, cost can be another factor in disabling a method. Of course, payment processors charge a fee. In some cases, the fee might make the product financially unfeasible to the end customer. This will lead to high cart abandonment rate. Also, higher fees may reduce your profitability. Thus, you may need to disable that method.

Fraud Prevention

The below image shows payment methods with the highest instances of fraud.

fraud-prevention

Source: Statista

As a store owner, you may decide to disable a method because of the fraud associated with it.

User Requirements

Suppose you offer 10 different payment options, including Amazon Pay. However, a detailed analysis of past two years reveal that customers mostly opt for 5 methods. It means that the remaining 5 are rarely used or not used at all. Naturally, you’ll be paying an annual or even monthly fee to the payment provider. Since they are not used regularly, there is no point in keeping them, meaning disabling them is a better option.

magento-2-payment-restriction

Payment Restrictions Pro

The Magento 2 Payment Restrictions extension limits payment methods based on shipping, customer groups, product attributes, and more, helping to cut costs, reduce risks, and offer relevant payment options.

Magento 2 Payment Options

Offering a perfect balance of cost effective and easy payment methods on the checkout page will tremendously increase your conversion rate. Unoptimized payment methods are one of the common reasons of cart abandonment. You may also want to disable a payment method based on customer groups, product or product attributes.

So, let’s see how to do this programmatically in Magento 2. If you are not a programmer and want an extension that does this for you along with many other features, you can go for Magento 2 Payment Restrictions extension.

How To Disable Payment Method Programmatically in Magento 2?

Follow this step-by-step method to programmatically disable payment method in Magento 2 store.

Step 1: Create events.xml file under app/code/Company/Module/etc/

<?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="disable_payment" instance="Company\Module\Observer\PaymentMethodAvailable" />
 </event>
</config>

Step 2: Now create PaymentMethodAvailable.php under Company/Module/Observer/ and write the following code.

<?php
namespace Company\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class PaymentMethodAvailable implements ObserverInterface
{
 public function execute(\Magento\Framework\Event\Observer $observer)
 {

 //Note: Replace "payment_method_code" with desired payment method code
 if($observer->getEvent()->getMethodInstance()->getCode()=="payment_method_code"){
 $checkResult = $observer->getEvent()->getResult();
 $checkResult->setData('is_available', false); 
 }
 }
}

The above code is a prototype of how to disable payment method programmatically in Magento 2. You can customize it to impose restrictions based on conditions like customer groups, products, attributes, etc.

Magento 2 Disable Amazon Pay

Using the above method, you can disable any method, including Amazon Pay. All you need to do is enter the code for Amazon Pay. Similarly, you can disable any other method which you find unfeasible for your store and users.

Final Thoughts on Magento 2 Disable Payment Method Programmatically

That’s it for this tutorial. If you find any issue regarding this article, then ask our expert Magento support team. Before disabling any method, we recommend carrying out a detailed analysis to ensure that you are making the right decision. Otherwise, you may make a decision that can undermine your competitiveness and user experience.

Read More:

This blog was created with FME's SEO-friendly blog