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

 

<?php




\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    "Vinay_DisablePayment",

    __DIR__

);




?>

 

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

 

<?xml version="1.0" ?>

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

    <module name="Vinay_DisablePayment" setup_version="1.0.0"/>

</config>

Now create the di.xml file for global scope,

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

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\OfflinePayments\Model\Checkmo">

        <plugin sortOrder="1" name="restrictByCustomer1" type="Vinay\DisablePayment\Plugin\Payment\Method\Checkmo\Available"/>

    </type>

</config>

Now create Available.php file at a location.

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

<?php




namespace Vinay\DisablePayment\Plugin\Payment\Method\Checkmo;

use Magento\Customer\Model\Session as CustomerSession;

use Magento\Backend\Model\Auth\Session as BackendSession;

use Magento\OfflinePayments\Model\Checkmo;




class Available

{




    /**

     * @var CustomerSession

     */

    protected $customerSession;




    /**

     * @var BackendSession

     */

    protected $backendSession;




    /**

     * @param CustomerSession $customerSession

     * @param BackendSession $backendSession

     */

    public function __construct(

        CustomerSession $customerSession,

        BackendSession $backendSession

    ) {

        $this->customerSession = $customerSession;

        $this->backendSession = $backendSession;

    }




    /**

     *

     * @param Checkmo $subject

     * @param $result

     * @return bool

     * @throws \Magento\Framework\Exception\LocalizedException

     */

    public function afterIsAvailable(Checkmo $subject, $result)

    {

        // Do not remove payment method for admin

        if ($this->backendSession->isLoggedIn()) {

            return $result;

        } else {

            return false;

        }

    }

}

 

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