I want to enable COD only for specific city, is it possible ?
I saw many articles but it's for Magento 1 not 2
Looking forward for your kind assist.
Use this working code, Path for event will be Vendor/Module/etc/events.xml
event
<event name="payment_method_is_active">
<observer name="custom_payment" instance="Vendor\Module\Observer\PaymentMethodAvailable" />
</event>
path for observer will be Vendor/Module/Observer/PaymentMethodAvailable.php
in Observer
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class PaymentMethodAvailable implements ObserverInterface
{
protected $_appState;
public function __construct(
\Magento\Framework\App\State $appState
) {
$this->_appState = $appState;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$result = $observer->getEvent()->getResult();
$method_instance = $observer->getEvent()->getMethodInstance();
$Shipping =$observer->getEvent()->getQuote()->getShippingAddress();
$quote = $observer->getEvent()->getQuote();
/*
* During Checkout magento call payment methods from both
* area frontend and Web_api are
*/
if($method_instance->getCode() == 'cashondelivery')
{
if( strtolower($Shipping['city']) == 'liverpool'){
$result->setData('is_available',true);
}
else{
$result->setData('is_available',false);
}
}
}
}
From admin backend
Store > Configuration > Sales > Payment Method > Cash On Delivery Payment
It's only allowing to specify countries.
So I guess for city you will have to go with custom code extension.