Skip to main content
added 833 characters in body
Source Link
Sohel Rana
  • 36.1k
  • 3
  • 73
  • 91

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"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);;
    }
}

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" type="SR\Stackexchange\Plugin\Model\MethodList" sortOrder="10"/>
    </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;
    }
}

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);;
    }
}
Source Link
Sohel Rana
  • 36.1k
  • 3
  • 73
  • 91

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" type="SR\Stackexchange\Plugin\Model\MethodList" sortOrder="10"/>
    </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;
    }
}