I need to disable the CoD payment method for carts with downloadable products. I started working on a plugin, but got hung up on this - have no idea how to get product types from cart.
Any ideas?
I need to disable the CoD payment method for carts with downloadable products. I started working on a plugin, but got hung up on this - have no idea how to get product types from cart.
Any ideas?
Best idea to use event/observer concept.
Fire an observer on event: payment_method_is_active
.
Observer:
<?php
namespace [Vendorname]\[Modulename]]\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class ObserverforDisabledCod implements ObserverInterface
{
/**
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$result = $observer->getEvent()->getResult();
$method_instance = $observer->getEvent()->getMethodInstance();
$quote = $observer->getEvent()->getQuote();
if ($quote === null || $method_instance->getCode() != 'cashondelivery')
return;
/* Disable All payment gateway exclude Your payment Gateway */
$items = $quote->getAllVisibleItems();
foreach ($items as $eachitem) {
if ($eachitem->getProductType() == 'downloadable') {
$result->setData('is_available', false);
break;
}
}
}
}
Didn't work for me, needed to make the following changes:
$result->isAvailable = false;
with $result->setData('is_available', false);
$items= as $eachitem
with $items as $eachitem
(magento 2.1.7)