{"id":11738,"date":"2020-07-11T11:47:48","date_gmt":"2020-07-11T06:17:48","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11738"},"modified":"2020-10-09T18:58:37","modified_gmt":"2020-10-09T13:28:37","slug":"magento-2-disable-payment-method-programmatically","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-disable-payment-method-programmatically\/","title":{"rendered":"Magento 2 &#8211; How to Disable Payment Method Programmatically"},"content":{"rendered":"<p>In this post I am going to explain how to disable payment method programmatically in magento 2.<\/p>\n<p>First, We need to write the &#8220;payment_method_is_active&#8221; event, which checks on checkout for payment method availability.<\/p>\n<p>So, we are going to create event.xml file on below path.<\/p>\n<p>Path: <strong>app\/code\/Ht\/Mymodule\/etc\/<\/strong><\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\"&gt;\r\n    &lt;event name=\"payment_method_is_active\"&gt;\r\n        &lt;observer name=\"custom_payment\" instance=\"Ht\\Mymodule\\Observer\\PaymentMethodAvailable\" \/&gt;\r\n    &lt;\/event&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Next to that, we are going to create observer PaymentMethodAvailable.php on below path.<\/p>\n<p>Path: <strong>app\/code\/Ht\/Mymodule\/Observer\/<\/strong><\/p>\n<p>Here I have given you the example to disable the check and money order payment method, you can change payment method code as per your requirement.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Observer;\r\n\r\nuse Magento\\Framework\\Event\\ObserverInterface;\r\n\r\n\r\nclass PaymentMethodAvailable implements ObserverInterface\r\n{\r\n    public function execute(\\Magento\\Framework\\Event\\Observer $observer)\r\n    {\r\n        \/\/ replace \"checkmo\" with your payment method code\r\n\t\t\r\n        if($observer-&gt;getEvent()-&gt;getMethodInstance()-&gt;getCode()==\"checkmo\"){\r\n            $checkResult = $observer-&gt;getEvent()-&gt;getResult();\r\n            $checkResult-&gt;setData('is_available', false);\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1775809619\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to disable payment method programmatically in magento 2. First, We need to write the &#8220;payment_method_is_active&#8221; event, which checks on checkout for payment method availability. So, we are going to create event.xml file on below path. Path: app\/code\/Ht\/Mymodule\/etc\/ &lt;?xml version=&#8221;1.0&#8243;?&gt; &lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:framework:Event\/etc\/events.xsd&#8221;&gt; &lt;event name=&#8221;payment_method_is_active&#8221;&gt; &lt;observer name=&#8221;custom_payment&#8221;&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-disable-payment-method-programmatically\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":11824,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[2128,10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11738"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11738"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11738\/revisions"}],"predecessor-version":[{"id":11739,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11738\/revisions\/11739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/11824"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}