cancel
Showing results for 
Search instead for 
Did you mean: 

Enable cash on delivery only for specific shipment method in New Order in Backend

Enable cash on delivery only for specific shipment method in New Order in Backend

I used a Plugin in a custom module to set the isAvailable function for CashOnDelivery to false when shipping method "flatrate_flatrate" is selected.

file: /app/code/MyCompany/MyModule/Plugin/CashOnDeliveryPlugin.php

<?php
    namespace MyCompany\MyModule\Plugin;
    use Magento\Payment\Model\Method\AbstractMethod;
    use Magento\Quote\Model\Quote;
    class CashOndeliveryPlugin
    {

      /**
       * @var \Magento\Checkout\Model\Session
       */
       protected $_checkoutSession;

      /**
       * Constructor
       *
       * @param \Magento\Checkout\Model\Session $checkoutSession
       */
        public function __construct
        (
            \Psr\Log\LoggerInterface $logger,
            \Magento\Checkout\Model\Session $checkoutSession
         ) {
            $this->logger = $logger;
            $this->_checkoutSession = $checkoutSession;
            return;
        }

        public function aroundIsAvailable(\Magento\Payment\Model\Method\AbstractMethod $subject, callable $proceed)
        {
            $shippingMethod = $this->_checkoutSession->getQuote()->getShippingAddress()->getShippingMethod();
            if ($shippingMethod == 'flatrate_flatrate') {
                return false;
            }
            $result = $proceed();
            return $result;
          }
    }

and file: /app/code/MyCompany/MyModule/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\OfflinePayments\Model\Cashondelivery">
        <plugin name="cashondeliveryplug" type="MyCompany\MyModule\Plugin\CashondeliveryPlug" disabled="false" sortOrder="10"/>
    </type>
</config>

It works fine in frontend checkout, but doesn't work in New Order in Backend. What should I change in code to make it functional in Backend Order Creation? Thank you.

1 REPLY 1

Re: Enable cash on delivery only for specific shipment method in New Order in Backend

Actually, the checkout process for backend and frontend both are the different process to generate order, also according to our need we have to bind payment methods based on sipping method, So solution to this problem, we may create a plugin which will work for backend orders only. please to follow the below steps.

 

Step 1 : Fist of all create Dependency Injection di.xml file to define plugin.

Path - Vendor/Module/etc/di.xml

 

<type name="Magento\Payment\Block\Form\Container">

        <plugin name="cashondeliveryplug" type="Vendor\Module\Plugin\CashOndeliveryPlugin" disabled="false" sortOrder="1" />

</type>

 

Step 2 : Create a plugin file 

Path - Vendor/Module/Plugin/CashOnDeliveryPlugin.php

 

use Magento\Sales\Model\AdminOrder\Create as adminOrderCreate;

    class CashOndeliveryPlugin

    {

 

      /**

       * @var \Magento\Sales\Model\AdminOrder\Create

       */

       protected $adminOrderCreate;

 

      /**

       * Constructor

       *

       * @param \Magento\Sales\Model\AdminOrder\Create $adminOrderCreate

 

Hope it helps!

Thanks in Advance.