I am using magento 2. And I need to disable one payment method 'cash on delivery' to specific cities(not for countries).I found so many solutions but those are not meant for magento 2, I think those are for older versions.Because I couldn't find those files(or methods in the specified location) in my code. Can anyone help me ? Any help will be appreciated.

1 Answer 1


You need to create a new module and use a 'plugin' to add some logic to the isAvailable method of Magento\OfflinePayments\Model\Cashondelivery.

Example plugin class:


namespace Your\Module\Plugin;

use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Quote\Model\Quote;

class CashondeliveryPlug
    public function aroundIsAvailable(AbstractMethod $subject, callable $proceed, Quote $quote)
        $result = $proceed($quote);

        if ($result) {
            $city = $quote->getBillingAddress()->getCity();
            if (in_array(strtolower($city), self::$cities)) return false;

        return $result;

    static $cities = [ // the cities to be excluded
        'new york',

And you'd have this in {module_dir}/etc/di.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <type name="Magento\OfflinePayments\Model\Cashondelivery">
        <plugin name="cashondeliveryplug" type="Your\Module\Plugin\CashondeliveryPlug" disabled="false" sortOrder="10"/>
  • where should i place this code?
    – Anu
    Dec 17, 2016 at 12:13
  • In my project there is no isAvailable function in cashondelivery.php file.but instead this function is situated in AbstractMethod.php file
    – Anu
    Dec 17, 2016 at 12:18
  • 1
    Yes, Cashondelivery inherits the method from AbstractMethod. You should follow a tutorial on how to create a module and use this code in your module. The only files I haven't given you are registration.php and etc/module.xml which are the boilerplate module declaration files. Dec 17, 2016 at 12:24
  • thanks Aaron. And i'm really new to magento. Can you plz guide me to place this code, if you don't mind?
    – Anu
    Dec 17, 2016 at 12:29
  • 1
    There's plenty of good tutorials explaining how to create a module (inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2) - do that first. Assuming you have a module, you'd then put the code in {your_module}/Plugin/CashondeliveryPlug.php and {your_module}/etc/di.xml}. Dec 17, 2016 at 12:36

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.