0

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,$quote = null)
    {
        $result = $proceed($quote);
        $codAvailability = $this->_pincodeModel->isCODAvailable($quote->getShippingAddress()->getPostcode()) ? "yes" : "no";            
        if ($codAvailability == 'no') {
            return false;
        }
        return $result;
    }
}

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

3
  • <plugin name="cashondeliveryplugin" type="Xxx\Yyy\Plugin\Model\Cashondelivery" sortOrder="10" disabled="true" /> not working for you ? Run the setup upgrade again and check
    – MGento
    Commented Feb 21, 2018 at 6:21
  • make sure the disabled="true" in your etc/di.xml file
    – MGento
    Commented Feb 21, 2018 at 6:21
  • still not working
    – Mahi M
    Commented Feb 21, 2018 at 6:30

1 Answer 1

0

You tried wrong way, try following way:

SR/Stackexchange/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\MethodList">
        <plugin name="cashondeliveryplugin_frontend" type="SR\Stackexchange\Plugin\Model\MethodList" sortOrder="10"/>
    </type>
    <type name="Magento\OfflinePayments\Model\Cashondelivery">
        <plugin name="cashondeliveryplugin" type="SR\Stackexchange\Plugin\Model\Cashondelivery" sortOrder="10" disabled="false" />
    </type>
</config>

SR/Stackexchange/Plugin/Model/MethodList.php


namespace SR\Stackexchange\Plugin\Model;

class MethodList
{
    public function afterGetAvailableMethods(
        \Magento\Payment\Model\MethodList $subject,
        $availableMethods,
        \Magento\Quote\Api\Data\CartInterface $quote = null
    ) {
        foreach ($availableMethods as $key => $method) {
            if($method->getCode() == 'cashondelivery') {
                $codAvailability = $this->_pincodeModel->isCODAvailable($quote->getShippingAddress()->getPostcode()) ? 'yes' : 'no';
                if ($codAvailability == 'no') {
                    unset($availableMethods[$key]);
                }
            }
        }

        return $availableMethods;
    }
}

SR/Stackexchange/Plugin/Model/Cashondelivery.php


namespace SR\Stackexchange\Plugin\Model;

class Cashondelivery
{
    public function aroundIsAvailable(
        \Magento\OfflinePayments\Model\Cashondelivery $subject,
        \Closure $proceed,
        \Magento\Quote\Api\Data\CartInterface $quote = null
    ) {
        $codAvailability = $this->_pincodeModel->isCODAvailable($quote->getShippingAddress()->getPostcode()) ? 'yes' : 'no';
        if ($codAvailability == 'no') {
            return false;
        }
        return $proceed($quote);;
    }
}
15
  • Its working on front end only ...but i need both on frontend and backend......
    – Mahi M
    Commented Feb 21, 2018 at 7:13
  • its global, so you can use it for both
    – Sohel Rana
    Commented Feb 21, 2018 at 7:14
  • Sorry this is only for frontend. I will come up with backend too.
    – Sohel Rana
    Commented Feb 21, 2018 at 7:17
  • Check updated answer
    – Sohel Rana
    Commented Feb 21, 2018 at 7:23
  • Yes, both use different way.
    – Sohel Rana
    Commented Feb 21, 2018 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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