Magento 2 - Disable cash on delivery based on cod
-
31-12-2020 - |
Question
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 .....
Solution
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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange