Zend certified PHP/Magento developer

Magento 2 – 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 MyCompanyMyModulePlugin;
    use MagentoPaymentModelMethodAbstractMethod;
    use MagentoQuoteModelQuote;
    class CashOndeliveryPlugin
    {

      /**
       * @var MagentoCheckoutModelSession
       */
       protected $_checkoutSession;

      /**
       * Constructor
       *
       * @param MagentoCheckoutModelSession $checkoutSession
       */
        public function __construct
        (
            PsrLogLoggerInterface $logger,
            MagentoCheckoutModelSession $checkoutSession
         ) {
            $this->logger = $logger;
            $this->_checkoutSession = $checkoutSession;
            return;
        }

        public function aroundIsAvailable(MagentoPaymentModelMethodAbstractMethod $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"?>


    
        
    

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?