0

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

4

In your custom module :

di.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="Magento\OfflinePayments\Model\Cashondelivery">
               <plugin sortOrder="1" name="restrictByCustomer"
            type="[vendor]\[module]\Plugin\Payment\Method\CashOnDelivery\Available"/>
          </type>
    </config>

[vendor][module]\Plugin\Payment\Method\CashOnDelivery\Available.php

<?php
/**
 * 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 :

window.checkoutConfig.payment.instructions

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

1
  • 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.