Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top