How to disable the Check/money order payment method in frontend-only and enable it in backend in Magento2?

Magento 2 can offer various types of payment methods, gateways, and payment services.

Payment methods: Payment methods are the number of ways by which merchants can collect payments from their customers.

By default, magento2 has 5 default payment methods.

Which are as follows:

  1. Check/money order
  2. Bank Transfer
  3. Cash on delivery
  4. Purchase Order
  5. Zero Subtotal Checkout

Magento also allows us to accept payment using third-party services such as PayPal and Authorize.net.

To enable and configure one of the basic payment methods, jump to
Admin -> Stores -> Configuration -> Sales -> Payment Methods

Now let’s discuss what if we have a situation in which we need to disable a Check/money order payment method at the front end only.

You can show the Check/money order payment method in the admin area only and disable it in the frontend area.

Here, we will create a simple module to disable check/money order payment method.

First of all, we need to register our module with registration.php file

Full path : app/code/Vinay/DisablePayment/registration.php

 

 

Create a module.xml file, Path: app/code/Vinay/DisablePayment/etc/module.xml

 

Now create the di.xml file for global scope,

Path: app/code/Vinay/DisablePayment/etc/di.xml

Now create Available.php file at a location.

Path: app/code/Vinay/DisablePayment/Plugin/Payment/Method/Checkmo/Available.php

 

Now run the command to enable module,

php bin/magento setup:upgrade

When you check the frontend you cant see Check/money order method.

 

At the backend, Check/money order is enabled:

At the frontend, the Check/money order is disabled:

To disable any other payment method, we can use Magento\Payment\Model\MethodList class and this class used getAvailableMethods() function to check available methods on frontend. We can disable any specific method by creating plugin for this class getAvailableMethods() method.

 

 

 

Leave a Reply