Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

Disable a Payment Method Programmatically in Magento 2

Vinh Jacker | 12-18-2024

Disable a Payment Method Programmatically

Payment system management can be considered as one of the cores of the Magento 2 store system as a good handling system can help you optimize your sales, profits as well as your efforts.

Sometimes, you will want to restrict specific payment methods based on the customer groups, product/order attributes, shipping parameters, etc. In other words, it’s time when you can programmatically disable a payment method from your Magento 2 backend configuration. For example, with customers who are coming from India, you can deactivate other payment methods and only display the CCAvenue method.

In today’s article, I will show you 2 simple stages to disable payment method programmatically in Magento 2.

2 Steps to Disable a Payment Method Programmically in Magento 2:

Step 1: Edit config.xml file

To disable the Payment Method Programmatically, firstly, you need to open the config.xml file and then add the following code to that file:

<global>
    ...
    <events>
        <payment_method_is_active>
            <observers>
                <disable_paymentmethod>
                    <class>Vendor_Extension_Model_Observer</class>
                    <method>paymentMethodIsActive</method>
                </disable_paymentmethod>
            </observers>
        </payment_method_is_active>
    </events>
    ...
</global>

Step 2: Edit Observer.php file

In this step, the below code should be added in the Observer.php file

<?php
class Vendor_Extension_Model_Observer {
    public function paymentMethodIsActive(Varien_Event_Observer $observer) {
        $method = $observer->getMethodInstance();
        
        if ($method->getCode() == 'payment_method_code') {
            
            if (condition) {
                $result = $observer->getResult();
                $result->isAvailable = false;
            }
        }
        
    }
}

Related Post:

Conclusion

That’s all about how to disable a Payment Method Programmically. Implement these stages correctly, you can control the payment methods choices at your checkout page according to different conditions. For the full solution to restrict payment choices, you can view Mageplaza Payment Restrictions module.

I hope this tutorial is helpful for you. If you have any question about this topic or face any issue while following the steps, feel free to let me know.

Thanks for reading!

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services