I need to disable COD option from observer, For observer my code is:

public function execute(\Magento\Framework\Event\Observer $observer)
  $order= $observer->getData('order');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$quote = $observer->getEvent()->getQuote(); 
$QuoteData = $quote->getItemsCollection(); 
$checkResult = $observer->getEvent()->getResult();

    foreach($QuoteData as $data) {   
             $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
            $p = $objectManager->get('Magento\Catalog\Model\Product')->load($data['product_id']);
            $attribute = $p->getResource()->getAttribute('cod'); 
            if ($attribute) 
                  $attr_value = $attribute ->getFrontend()->getValue($p);

                    if($attr_value == 'Yes' || $attr_value == 'yes' || $attr_value == 'YES'){ return;
                        $code = $observer->getEvent()->getMethodInstance()->getCode();
                             if($code == "cashondelivery" || $code == "mpcashondelivery"){

                             $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page



But COD is not disabling if anyone have solution please share.


2 Answers 2


You can do the same with Plugin. I too found an issue while getting the quote through observer way. Try to implement it by Plugin. The below code is working fine for me.


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <type name="Vendor\Module\Model\Module">
        <plugin sortOrder="1" name="restrictByCustomer" type="Vendor\Module\Plugin\Payment\Method\Module\Available"/>


public function afterIsAvailable(\Magento\Payment\Model\Method\AbstractMethod $subject, $result)
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
    $paymentMethodCode = $subject->getCode();

    if ($paymentMethodCode == 'cod') {
        return false;
    } else {
        return $result;  //Return result for true scenario. return true wont work

However, if you still want to do it with Observer only, try to get quote by following way,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

Note: I do not recommend to use object manager.


$QuoteData does not give you the Quote items. But $QuoteData->getAllItems() given the quote items.


foreach($QuoteData as $data) { is wrong code

it Should be

foreach ($QuoteData->getAllItems() as $item) { 

    if ($item->getParentItemId()) {

Also, you should remove $order= $observer->getData('order'); at payment_method_is_active event you cannot get Order oBject

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.