{"id":243,"date":"2018-11-23T08:14:46","date_gmt":"2018-11-23T08:14:46","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/11\/23\/disable-payment-method-programmatically-in-magento-2\/"},"modified":"2025-05-22T17:12:34","modified_gmt":"2025-05-22T11:42:34","slug":"disable-payment-method-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/disable-payment-method-programmatically-in-magento-2\/","title":{"rendered":"How to Disable Payment Method Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Restrict Payment Methods Based on Conditions<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take Full Control of Allowed Payment Methods in Your Store Using Meetanshi&#8217;s Magento 2 Payment Restrictions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Successful E-commerce store owners have learned the criticality of offering and managing various payment methods at checkout. In the process of providing numerous payment methods, the business ends up messing with the management or incur a loss due to offering such facilities. There should be a balance between maintaining user experience and offering payment methods that do not load the management or be too costly for the store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The best way to gain this perfect balance is to <em><strong>disable payment method programmatically in Magento 2<\/strong><\/em> on the checkout page, <a href=\"https:\/\/meetanshi.com\/blog\/make-a-payment-method-visible-only-to-admin-in-magento-2\/\">making a payment method visible only to admin<\/a>. The payment methods can be disabled based on customer groups or for specific products. For example, keep the priority of providing excellent user experience to VIP customers whereas don&#8217;t waste your bucks in offering payment facilities to guest customers!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, you may want to restrict payment methods based on product or order attributes. For all these above conditions and more, I&#8217;ve come up with a method to disable payment method programmatically in Magento 2. The code can be customized to disable payment methods as per the conditions like customer groups, products, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With this method, the payment method won&#8217;t be displayed in the frontend.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to disable payment method programmatically in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create <strong>events.xml<\/strong> at <strong>app\/code\/Vendor\/Extension\/etc\/<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?> \n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">    \n&lt;event name=\"payment_method_is_active\">         \n&lt;observer name=\"disable_payment\" instance=\"Vendor\\Extension\\Observer\\PaymentMethodAvailable\" \/>     \n&lt;\/event> \n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create <strong>PaymentMethodAvailable.php<\/strong> at &nbsp;<strong>app\/code\/Vendor\/Extension\/Observer\/<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Observer;\nuse Magento\\Framework\\Event\\ObserverInterface;\nclass PaymentMethodAvailable implements ObserverInterface\n{\n    public function execute(\\Magento\\Framework\\Event\\Observer $observer)\n    {\n        \/\/ you can replace \"checkmo\" with your required payment method code\n        if($observer->getEvent()->getMethodInstance()->getCode()==\"checkmo\"){\n            $checkResult = $observer->getEvent()->getResult();\n            $checkResult->setData('is_available', false); \n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the above code, impose all the restrictions you want in the payment methods in&nbsp;Magento 2 store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to skip the tedious implementation process, try <em><strong><a href=\"https:\/\/meetanshi.com\/magento-2-payment-restrictions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Payment Restrictions<\/a><\/strong><\/em> extension offering similar functionality!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Restrict Payment Methods Based on Conditions Take Full Control of Allowed Payment Methods in Your Store Using Meetanshi&#8217;s Magento 2 Payment Restrictions. Successful E-commerce store&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-243","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":15550,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions\/15550"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}