Site icon Brian Prom Blog

Magento 2 Set Payment Method Availability for Frontend (website) vs Backend (admin)

Magento admin configuration doesn’t allow you to control if a payment method is available in just the frontend or backend (admin).

Thankfully they did offer some pre-built methods for configuring this via the codebase.

Control payment methods in Magento to – enabling and disabling for frontend or backend as needed.

If you take a look at vendor/magento/module-payment/Model/Method/AbstractMethod.php you’ll see there are several protected methods that can be set in a payment method model.

grep 'protected $_can' vendor/magento/module-payment/Model/Method/AbstractMethod.php
    protected $_canOrder = false;
    protected $_canAuthorize = false;
    protected $_canCapture = false;
    protected $_canCapturePartial = false;
    protected $_canCaptureOnce = false;
    protected $_canRefund = false;
    protected $_canRefundInvoicePartial = false;
    protected $_canVoid = false;
    protected $_canUseInternal = true;
    protected $_canUseCheckout = true;
    protected $_canFetchTransactionInfo = false;
    protected $_canReviewPayment = false;
    protected $_canCancelInvoice = false;

The two of these that are relevant to the topic at hand are $_canUseInternal and $_canUseCheckout

$_canUseInternal – Enable/Disable for Backend (admin)

This method specifies if the payment method is available in the Magento Admin or not.

A good example of how this is used is PayPal. The PayPal payment method is ONLY available for the customer to use on the frontend – not when placing an order in the Magento admin.

    /**
     * Using internal pages for input payment data.
     *
     * Can be used in admin.
     *
     * @return bool
     * @deprecated 100.2.0
     */
    public function canUseInternal()
    {
        return $this->_canUseInternal;
    }

$_canUseCheckout – Enable/Disable for Frontend (website)

This method specifies if the payment method is available in the public facing frontend or not.

One example here is to accept the Check / Money Order payment method is accessible only in the Magento admin but not for public customers to use.

    /**
     * Can be used in regular checkout
     *
     * @return bool
     * @deprecated 100.2.0
     */
    public function canUseCheckout()
    {
        return $this->_canUseCheckout;
    }

Example Module to Disable Check / Money Order (checkmo) on Frontend (available to Admin only)

Here is a quick example module to set the Magento default Check / Money Order (checkmo) payment method to be used in the admin only. This allows for accepting payments via check, money order, or a virtual terminal with a payment gateway but not giving this option to your customers on the frontend.

First define your module.

## app/code/Namespace/MagentoOfflinePayments/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Namespace_MagentoOfflinePayments',
    __DIR__
);
## app/code/Namespace/MagentoOfflinePayments/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="Namespace_MagentoOfflinePayments">
        <sequence>
            <module name="Magento_OfflinePayments"/>
        </sequence>
    </module>
</config>

Then extend the payment model, setting the $_canUseCheckout method to false.

## app/code/Namespace/MagentoOfflinePayments/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\OfflinePayments\Model\Checkmo" type="Namespace\MagentoOfflinePayments\Model\Checkmo" />
</config>
## app/code/Namespace/MagentoOfflinePayments/Model/Checkmo.php

<?php

namespace Namespace\MagentoOfflinePayments\Model;

class Checkmo extends \Magento\OfflinePayments\Model\Checkmo
{

    /**
     * Availability on frontend checkout
     *
     * @var bool
     */
    protected $_canUseCheckout = false;

}

As a bonus touch, add a note in the admin on the Check / Money Order settings section that this change has been made.

## app/code/Namespace/MagentoOfflinePayments/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="payment">
            <group id="checkmo">
                <field id="note_disabled" translate="label" type="label" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>NOTE:</label>
                    <comment><![CDATA[<span style="background-color: #fbdccc;color: #e22626;font-weight: bold;padding: 5px;display: inline-block;">This payment method is only available to the Magento admin via a code modification.</span>]]></comment>
                </field>
            </group>
        </section>
    </system>
</config>

Lastly, you just need to tell Magneto to install this module.

php bin/magento setup:upgrade && php bin/magento setup:di:compile && php bin/magento cache:flush

And that’s it. Now the Check / Money Order (checkmo) payment method will no longer be available on the frontend but will work without issue in the admin.

Exit mobile version