General

How to Disable Payment Method Programmatically in Magento 2

Hello Magento Folks?,

What’s Up! Welcome to the Magento Tutorial blog. Today will help you How to Disable Payment Method Programmatically in Magento 2. Also, go through the previously placed article How to Install Magento SUPEE 11346 [With or Without SSH]. 

Introduction:

The online stores are providing many methods for payments at checkout. In providing many payment method options many times the store is screwed up with managing all payment methods and the costing is not affordable by the store owners. Always there should be stability in between offering the payment methods and maintaining customer experience in your store.

To overcome these problems the store owner can disable payment methods programmatically in Magento 2. One can also restrict the payment method on the base of customer groups, shipping parameters, product/order attributes, etc. Let’s Do It?

Steps to Disable Payment Method Programmatically in Magento 2:

Step 1: add events.xml in the following path

app\code\Vendor\Extension\etc\events.xml

Then add following code

<?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="Vendor\Extension\Observer\Paymentmethoddisable" />
    </event>
</config>

Step 2: Now add Paymentmethoddisable.php in following path

app\code\Vendor\Extension\Observer\Paymentmethoddisable.php

Then add following code

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;


class Paymentmethoddisable implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo"){  // Here you can replace required payment method code
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); 
        }
    }
}

Conclusion:

Hence, after the implementation of the given above steps, you will be easily able to How to Disable Payment Method Programmatically in Magento 2. If any difficulties then comment down in the comment section below. Don’t forget to share the article with your friends.

Happy Coding! 

Click to rate this post!
[Total: 6 Average: 4.8]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago