When working with Magento 2, sometimes we want to disable Payment methods on the Front page and use it only for Admin. Or, if we want to set more rules for Payment methods to check whether payment method can be used. How we can do that?
Magento 2 Payment Methods – Admin Only Payment Method
magento2overridespayment-gatewaypayment-methods
Related Solutions
Jaydeep Sinh Parmar,can do this by Magento Event/Observer
First,using payment_method_is_active disable on depends current store.
Code for this:
Module config.xml code:
<global>
<events>
<payment_method_is_active>
<observers>
<paymentfilter_payment_method_is_active>
<type>singleton</type>
<class>yourmodel/observer</class>
<method>filterpaymentmethod</method>
</paymentfilter_payment_method_is_active>
</observers>
</payment_method_is_active>
</events>
</global>
Observer code is:
<?php
class YOURNANESPACE_YOURMODULE_Model_Observer {
public function filterpaymentmethod(Varien_Event_Observer $observer) {
/* call get payment method */
$method = $observer->getEvent()->getMethodInstance();
/* get Quote */
$quote = $observer->getEvent()->getQuote();
/* Disable Your payment method for adminStore */
if (Mage::app()->getStore()->isAdmin()):
if($method->getCode()=='YOUR_PAYMENT_METHOD_CODE' ){
$result = $observer->getEvent()->getResult();
$result->isAvailable = false;
}
endif;
}
}
Best idea to use event/observer concept.Fire an observer on event: payment_method_is_active
Create events.xml
at app\code\[Vendorname]\[Modulename]\etc\
<?xml version="1.0"?>
<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="disable_pg" instance="[Vendorname]\[Modulename]\Observer\ObserverforDisabledFrontendPg" />
</event>
</config>
then at observer class ObserverforDisabledFrontendPg.php rewrite below code:
<?php
namespace [Vendorname]\[Modulename]\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class ObserverforDisabledFrontendPg implements ObserverInterface
{
protected $_appState;
public function __construct(
\Magento\Framework\App\State $appState
) {
$this->_appState = $appState;
}
/**
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$result = $observer->getEvent()->getResult();
$method_instance = $observer->getEvent()->getMethodInstance();
$quote = $observer->getEvent()->getQuote();
if(null !== $quote){
/*
* During Checkout magento call payment methods from both
* area frontend and Web_api are
*/
if($method_instance->getCode() =='checkmo' &&
in_array($this->_appState->getAreaCode(), $this->getDisableAreas()))
{
$result->setData('is_available',false);
//$items= $quote->getAllVisibleItems();
}
}
}
protected function getDisableAreas(){
return array( \Magento\Framework\App\Area::AREA_FRONTEND, \Magento\Framework\App\Area::AREA_WEBAPI_REST);
}
}
Best Answer
We should take a look at the payment abstract class
vendor/magento/module-payment/Model/Method/AbstractMethod.php
. We can see the methodpublic function isAvailable()
- This class will check whether payment method can be used.As can we see, there is an Observer event, like Magento 1, we can use Observer to disable our Payment Methods. It's Magento 1 way. However, Magento 2 introduces to developers a new way - Plugin.
Create DI on front page: app/code/{Vendor}/{Module Name}/etc/frontend/di.xml
app/code/{Vendor}/{Module Name}/Plugin/Model/Method/Available.php
$subject
is the current Payment Method Model.