Monday 13 August 2018

Magento 2 Hide Payment Method from Front-end.

I have created module to Disable Payment Method from Front-end. So that payment method will be active for admin users only.

For that I have also give enable/disable configuration field in admin.

Frist, let's create module.. This is very basic and very simple module. Just create the module and install it on your store.


1) Create 1. registrations.php file in the root of your module..
So, create app/code/Gopal/DisablePaymentInFront/registration.php

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

2) Create module.xml file to enable module and add dependency to the payment module.
So, create app/code/Gopal/DisablePaymentInFront/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="Gopal_DisablePaymentInFront" setup_version="2.0.1">  
     <sequence>  
       <module name="Magento_Payment"/>  
     </sequence>  
   </module>  
 </config>  

3) Now create di.xml to add dependency.
We are creating Plugin method of the magento2. Plugin is very good feature included in the Magento 2.
So, create, app/code/Gopal/DisablePaymentInFront/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">  
   <type name="Magento\Payment\Model\Method\AbstractMethod">  
     <plugin name="disable_payment_method_in_frontend" type="Gopal\DisablePaymentInFront\Plugin\DisablePaymentInFront" />  
   </type>  
 </config>  

4) We are adding system configuration field to enable or disable this feature for particular payment method.
There will be Disable in Frontend option to disable payment method in frontend.
So Create, app/code/Gopal/DisablePaymentInFront/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" translate="label" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1">  
       <group id="checkmo" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
       <group id="purchaseorder" translate="label" type="text" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
       <group id="banktransfer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
       <group id="cashondelivery" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
     </section>  
   </system>  
 </config>  

5) Now Create Plugin to add logic to disable any payment method for frontend as per configured in system configuration.

app/code/Gopal/DisablePaymentInFront/Plugin/DisablePaymentInFront.php
 <?php  
 namespace Gopal\DisablePaymentInFront\Plugin;  
 class DisablePaymentInFront  
 {  
      const CHECKMO = \Magento\OfflinePayments\Model\Checkmo::PAYMENT_METHOD_CHECKMO_CODE;  
      const CASH_ON_DELIVERY = \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;  
   /**  
    * @var \Magento\Framework\App\State  
    */  
   private $appState;  
   /**  
    * @var \Magento\Framework\App\Config\ScopeConfigInterface  
    */  
   private $scopeConfig;  
   /**  
    * @param \Magento\Framework\App\State $appState  
    */  
   public function __construct(  
     \Magento\Framework\App\State $appState,  
     \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig  
   )  
   {  
     $this->appState = $appState;  
     $this->scopeConfig = $scopeConfig;  
   }  
   /**  
    * @param \Magento\Payment\Model\Method\AbstractMethod $subject  
    * @param $result  
    * @return bool  
    * @throws \Magento\Framework\Exception\LocalizedException  
    */  
   public function afterIsAvailable(\Magento\Payment\Model\Method\AbstractMethod $subject, $result)  
   {  
     $area_code = $this->appState->getAreaCode();  
     if ($area_code != \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) {  
       if ($this->checkForCheckMo($subject, self::CHECKMO)  
         || $this->checkForCheckMo($subject, self::CASH_ON_DELIVERY)  
         || $this->checkForCheckMo($subject, self::PURCHASE_ORDER)  
         || $this->checkForCheckMo($subject, self::BANK_TRANSFER)) {  
         return false;  
       }  
     }  
     return $result;  
   }  
   /**  
    * @param $subject  
    * @param $method  
    * @return bool|mixed  
    */  
   private function checkForCheckMo($subject, $method)  
   {  
     if ($subject->getCode() == $method) {  
       $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;  
       return $this->scopeConfig->getValue('payment/' . $method . '/disable_in_front', $storeScope);  
     }  
     return false;  
   }  
 }  


Go to Admin -> Stores -> Configuration -> Sales -> Payment Method.
Then go to particular payment method and
Select "Disable in Frontend" =>  "Yes"
Then Clear cache and generation as par Magento2.
Check admin and fronend, You can see that payment method should be disabled in frontend and it is showing in admin.

This method will be useful for some payment method which is only needed to admin user.

1 comment: