Hide Payment Methods in Magento 2 on backend and frontend

Hide Payment Methods in Magento 2 on backend and frontend

Magento 2 offers us some built-in Payment Gateways to include in front-end to make payment. There are 2 types of Payment Methods:

  1. Offline
  2. Online

A custom Offline Payment Method can easily be added and third-party Online Payment Method can also be added by creating a custom extensions.

Sometimes, there might be a requirement to hide/disable payment method conditionally from admin panel and front-end as well.

So, here, I am going to create a custom extension to disable / hide / exclude specific Payment Method(s) from One Checkout Page on front-end and from admin panel. To achieve this task, an Observer will be created. Here, you can get more details to create an Observer

Following are the steps:

  1. Creating and registering a new Custom Module
  2. Including Event and Defining Observer
  3. Creating Observer
  4. Running Magento Commands


To create a custom module, very first step is to create a file, module.xml in folder, <Magento Root Folder>/app/code/<Vendor Name>/<Module Name>/etc/ folder. Let’s take the path -


and paste the following code into this file:

<?xml version="1.0"?> <!-- /** * @copyright Copyright (c) 2015 X.commerce, Inc. (http://www.magentocommerce.com) */--> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">  <module name="Bizspice_PaymentMethod" setup_version="1.0">  </module> </config>

Now, create a file registration.php in /app/code/Bizspice/PaymentMethod/ folder with the following code:

<?php \Magento\Framework\Component\ComponentRegistrar::register(  \Magento\Framework\Component\ComponentRegistrar::MODULE,  'Bizspice_PaymentMethod',  __DIR__ );


To include an event to add a custom observer, create a file events.xml under /app/code/Bizspice/PaymentMethod/etc/ folder with the following code:

<?xml version="1.0" encoding="UTF-8"?> <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="restrict_payment_methods" instance="Bizspice\PaymentMethod\Observer\RestrictPaymentMethods" />  </event> </config>

Here, you can see that event payment_method_is_active has been written. This is the event which is triggered on Checkout Page for Payment Method availability. Here, our custom observer will be called which will exclude the specific Payment Method(s) from One Checkout Page.

Now, create observer RestrictPaymentMethods.php under /app/code/Bizspice/PaymentMethod/Observer folder with the following code:

<?php namespace Bizspice\PaymentMethod\Observer; use Magento\Framework\Event\Observer as EventObserver; use Magento\Framework\Event\ObserverInterface; use Magento\Checkout\Model\Session; use Magento\Backend\Model\Session\Quote as adminQuoteSession; class RestrictPaymentMethods implements ObserverInterface {  protected $_state;  protected $_session, $_quote;  //Fetch Codes of Payment Methods---  const CHECKMO = \Magento\OfflinePayments\Model\Checkmo::PAYMENT_METHOD_CHECKMO_CODE;   const COD = \Magento\OfflinePayments\Model\Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE;   const PURCHASE_ORDER = \Magento\OfflinePayments\Model\Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;   const BANK_TRANSFER = \Magento\OfflinePayments\Model\Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE;   public function __construct(  \Magento\Framework\App\State $state,   Session $checkoutSession,  adminQuoteSession $adminQuoteSession) {  $this->_state = $state;  if ($state->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML) {  $this->_session = $adminQuoteSession;  } else {  $this->_session = $checkoutSession;  }  $this->_quote = $this->_session->getQuote();  }  /**  * payment_method_is_active event handler.  *  * @param \Magento\Framework\Event\Observer $observer  */  public function execute(EventObserver $observer) {  //Code of Current Payment Method--  $code = $observer->getEvent()->getMethodInstance()->getCode();  /*  * You can use $this->_quote object to apply conditions based on current quote--  * For instance -   $totalItems = $this->_quote->getItemsCount(); //Total Items in cart  $items = $this->_quote->getAllVisibleItems(); //Items in cart  */    /*  * Now, you can check if current method code is as same as the code of payment method which  * you want to disable then apply the following condition  * Suppose you want to do for CHECKMO  * Following code will exclude the CHECKMO payment method from both admin panel and front-end  */  if($code == self::CHECKMO) {  $checkResult = $observer->getEvent()->getResult();  //this is disabling the payment method at both checkout page in front-end and admin panel  $checkResult->setData('is_available', false);  }  /*  * If payment method has to be excluded from Fron-end Only then do the following  * Suppose you want to do for COD  */  if ($this->_state->getAreaCode() != \Magento\Framework\App\Area::AREA_ADMINHTML && $code == self::COD) {  $checkResult = $observer->getEvent()->getResult();  //this is disabling the payment method at both checkout page in front-end only  $checkResult->setData('is_available', false);  }  } }


Custom Offline Payment Method has been created and now the last step is to install this module and clear the cache (if enabled in Magento) for using this module. Run the following commands:

#rm -rf pub/static/* var/* generated/*; #php bin/magento setup:upgrade #php bin/magento setup:di:compile #php bin/magento setup:static-content:deploy -f #php bin/magento indexer:reindex #php bin/magento cache:clean #php bin/magento cache:flush

We also have made a plugin package for this article. If you want a working plugin to hide your payment methods, please go to this link and buy this FREE product from our Magento extension store.