{"id":271,"date":"2017-11-30T21:38:13","date_gmt":"2017-11-30T21:38:13","guid":{"rendered":"http:\/\/www.evermoretechnologies.com\/blog\/?p=271"},"modified":"2017-12-29T20:17:25","modified_gmt":"2017-12-29T20:17:25","slug":"magento-2-payment_method_is_active-observer","status":"publish","type":"post","link":"https:\/\/www.evermoretechnologies.com\/blog\/2017\/11\/magento-2-payment_method_is_active-observer\/","title":{"rendered":"Magento 2 Disable Payment Method in php Using payment_method_is_active Observer"},"content":{"rendered":"<p>I recently came across a situation where I needed to be able to turn off payment method on checkout based on customer information on a Magneto 2 site.<\/p>\n<p>The best solution seemed to be to tie into the payment_method_is_active event. I left the payment method turned on in the admin for ease of use and clarity to the store owner and had my observer turn off the method based on criteria.<\/p>\n<p>The code structure you need is pretty simple<br \/>\napp\/code\/Evermore\/Payment is the root of my extension.<\/p>\n<p>As with all extension, add the registration.php file<\/p>\n<p>registration.php<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n'Evermore_Payment',\r\n__DIR__\r\n);<\/pre>\n<p>Then add the module.xml file.<\/p>\n<p>etc\/module.xml<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&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:Module\/etc\/module.xsd\"&gt;\r\n    &lt;module name=\"Evermore_Payment\" setup_version=\"0.0.1\"&gt;\r\n    &lt;\/module&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Now an events.xml file to add the observer call.<\/p>\n<p>etc\/events.xml<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\"?&gt;\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=\"Evermore_Payment_Method_Is_Active\" instance=\"Evermore\\Payment\\Observer\\DisablePaymentMethods\" \/&gt;\r\n    &lt;\/event&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<p>Now the structure is in place to call your Observer.\u00a0 Finally add the Observer file.<\/p>\n<p>Observer\/DisablePaymentMethods.php<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n\r\n\/**\r\n * Disables Payment Method of PO if the customer doesn't have approved billing terms.\r\n *\r\n *\/\r\n\r\nnamespace Evermore\\Payment\\Observer;\r\n\r\nuse Magento\\Framework\\Event\\Observer;\r\nuse Magento\\Framework\\Event\\ObserverInterface;\r\nuse Psr\\Log\\LoggerInterface;\r\n\r\nclass DisablePaymentMethods implements ObserverInterface {\r\n  protected $_customerInterface;\r\n  protected $_logger;\r\n\r\n  public function __construct(\r\n    \\Magento\\Customer\\Api\\CustomerRepositoryInterface $customerInterface,\r\n    LoggerInterface $logger\r\n  ) {\r\n    $this-&gt;_customerInterface = $customerInterface;\r\n    $this-&gt;_logger = $logger;\r\n  }\r\n\r\n  \/**\r\n   * @param Observer $observer\r\n   *\r\n   * @return void\r\n   *\/\r\n  public function execute(Observer $observer) {\r\n    \/\/Replace this code with your own checks.  This code is checking a customer attribute.  If they are not approved for billing terms, the purchaseorder method is turned off.\r\n    $result = $observer-&gt;getEvent()-&gt;getResult();\r\n    $method_instance = $observer-&gt;getEvent()-&gt;getMethodInstance()-&gt;getCode();\r\n    $quote = $observer-&gt;getEvent()-&gt;getQuote();\r\n    if($method_instance == 'purchaseorder') {\r\n      $customer = $this-&gt;_customerInterface-&gt;getById($quote-&gt;getCustomer()-&gt;getId());\r\n      if($customer-&gt;getCustomAttribute('approved_billing_terms')-&gt;getValue() == 0) {\r\n        $result-&gt;setData('is_available', false);\r\n      }\r\n    }\r\n  }\r\n}<\/pre>\n<p>Put this code in your extension, run the standard code<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">php bin\/magento setup:upgrade\r\nphp bin\/magento setup:di:compile\r\n<\/pre>\n<p>And you&#8217;re done.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently came across a situation where I needed to be able to turn off payment method on checkout based on customer information on a Magneto 2 site. The best&#8230; <a href=\"https:\/\/www.evermoretechnologies.com\/blog\/2017\/11\/magento-2-payment_method_is_active-observer\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34,35],"tags":[],"_links":{"self":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/271"}],"collection":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":7,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/271\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}