I need to show COD payment method for some zip-code only. How do I add filter in COD payment method for zip-code in Magento 2 ?

How to add my custom condition in cashondelivery.html

1 Answer 1


In your custom module :


    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          <type name="Magento\OfflinePayments\Model\Cashondelivery">
               <plugin sortOrder="1" name="restrictByCustomer"


 * Copyright © 2016 MageWorx. All rights reserved.
 * See LICENSE.txt for license details.

namespace [vendor]\[module]\Plugin\Payment\Method\CashOnDelivery;

use Magento\Customer\Model\Session as CustomerSession;
use Magento\Backend\Model\Auth\Session as BackendSession;
use Magento\OfflinePayments\Model\Cashondelivery;

class Available

     * @var CustomerSession
    protected $customerSession;

     * @var BackendSession
    protected $backendSession;

     * @param CustomerSession $customerSession
     * @param BackendSession $backendSession
    public function __construct(
    CustomerSession $customerSession,
    BackendSession $backendSession
    ) {
    $this->customerSession = $customerSession;
    $this->backendSession = $backendSession;

     * @param Cashondelivery $subject
     * @param $result
     * @return bool
     * @throws \Magento\Framework\Exception\LocalizedException
    public function afterIsAvailable(Cashondelivery $subject, $result)
    // Do not remove payment method for admin
    if ($this->backendSession->isLoggedIn()) {
        return $result;
    return true/false;


In this some time you will get the JS error of :


In this you need to change the .js file of that payment method and remove this line.

  • when it return false its shows a COD payment method in frontend Commented Nov 5, 2019 at 11:51

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.