I want to hide COD payment method based on the Product attribute. I tried payment_method_is_active event method to disable using observer like below.
My observer file is here
public function execute(
MagentoFrameworkEventObserver $observer
) {
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$method = $observer->getEvent()->getMethodInstance();
$quote = $observer->getEvent()->getQuote();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$items = $cart->getItems();
$disable_cod = false;
foreach ($items as $item) {
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
if(array_key_exists("attributes_info",$options) && count($options('attributes_info')) >= 1)
{
foreach($options('attributes_info') as $myopt)
{
if($myopt('label')==='E-Gift'){ // Condition i am checking
$disable_cod = true;
}
}
}
if($disable_cod){
$this->logger->critical("Disable COD");
if($method->getCode() === 'cashondelivery'){
$this->logger->critical("Here 1");
$result = $observer->getEvent()->getResult();
$result->setData('is_available', false);
}
}else{
$this->logger->critical("Enable COD");
}
}
COD is disabling only if I have an E-Gift product only. If I have other products with E-Gift product COD is not disabling. Ultimately I need to hide the COD if at least one product including E-Gift.
Please help me with this.