I’ve created custom module for event and observer to restrict some city from COD.
However I’m facing an error at checkout page and inside the log I found the following error:
Error(code: 0): Call to a member function getShippingAddress() on null
Observer :
<?php
namespace VendorModuleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestDataPersistorInterface;
use MagentoFrameworkAppObjectManager;
class PaymentMethodAvailable implements ObserverInterface
{
protected $_appState;
public function __construct(
MagentoFrameworkAppState $appState
) {
$this->_appState = $appState;
}
public function execute(MagentoFrameworkEventObserver $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);
}
}
}
}