1

How to disable cash on delivery payment based on cod availability. etc/di.xml

<type name="Magento\OfflinePayments\Model\Cashondelivery">
        <plugin name="cashondeliveryplugin" type="Xxx\Yyy\Plugin\Model\Cashondelivery" sortOrder="10" disabled="false" /> </type> 

Cashondelivery.php

 public function aroundIsAvailable(\Magento\OfflinePayments\Model\Cashondelivery $subject, callable $proceed)
    {
        $result = $proceed();
        $zipCode = $this->_checkoutSession->getQuote()->getShippingAddress()->getPostcode();
        $codAvailability = $this->_pincodeModel->isCODAvailable($zipCode) ? "yes" : "no";
        if ($codAvailability == 'no') {
            return false;
        }
        return $result;
    }
}

Its not working in backend...Any modification in my code .....

2

please use below plugin for same

<type name="Magento\Payment\Model\MethodList">
        <plugin name="methodlist" type="Module\NameSpace\Plugin\Model\Method\Available" sortOrder="1" />
    </type>

In that class

 public function aroundGetAvailableMethods($object, callable $proceed,$quote)
    {
         $result = $proceed($quote);
        foreach($result as $key=>$_result){
            if($_result->getCode() == "cashondelivery"){ //make sure your payment method code
                $isAllowed = $this->_pincodeModel->isCODAvailable($quote->getShippingAddress()->getPostcode()) ? "1" : ""           
                if(!$isAllowed)
                {
                    unset($result[$key]);
                }
            }
        }
        return $result;
    }
  • its working for backend? – Mahi M Feb 20 '18 at 13:04
  • yes it will work – Sunil Patel Feb 20 '18 at 13:05
  • how to get zipcode in backend – Mahi M Feb 20 '18 at 13:15
  • please check code – Sunil Patel Feb 20 '18 at 13:25
  • is that works or not? – Sunil Patel Feb 20 '18 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.