0

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.

2 Answers 2

2

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); 
                }
            }
        
    }      
}
6
  • That will work ;) Although he might probably want something administrable where you can add your cities directly in backend at the same location where you can select countries. But the logic remains fine.
    – Claims
    Commented Jul 22 at 12:45
  • Yeah you can add them in system config and use that, im just giving the direction.
    – Kareem k
    Commented Jul 22 at 12:49
  • Hi @kareem-k, could you please advise where I should create these files ? Because I'm not experience in Magento
    – Dun7ill
    Commented Jul 22 at 14:49
  • @Dun7ill you need to create a custom module, if you want some help to cuustomize it and understand for example how to create the observer inside the module you can do that with "no code" with mage2gen website; it might help you understand where everything goes : mage2gen.com When Kareemk refeers to event, he is refeering to events.xml where you can declare the observer inside the module
    – Claims
    Commented Jul 23 at 12:04
  • @Dun7ill code updated with file paths
    – Kareem k
    Commented Jul 25 at 8:11
0

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.

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.