0

I am using magento 2. And I need to disable one payment method 'cash on delivery' to specific states. Any help will be appreciated.

1 Answer 1

1

For that you have to create observer:

VENDOR\MODULE\etc\events.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="payment_method_is_active">
        <observer name="codpaymentdisableregion" instance="VENDOR\MODULE\Observer\Paymentactive" />
    </event>
</config>

VENDOR\MODULE\Observer\Paymentactive.php

<?php
namespace VENDOR\MODULE\Observer;

use Magento\Framework\Event\ObserverInterface;

class Paymentactive implements ObserverInterface 
{
    protected $_checkoutSession;

    public function __construct(\Magento\Checkout\Model\Session $checkoutSession) {
        $this->_checkoutSession = $checkoutSession;
    }
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $_session=$objectManager->get('Magento\Checkout\Model\Session');
        $method = $observer->getEvent()->getMethodInstance();
        $result = $observer->getEvent()->getResult();

        if($_session->getQuote()->getShippingAddress()->getRegion()=='XYZ' && $method->getCode()=="cashondelivery")
        {
            return $result->setData('is_available', false);
        }
        return $result->setData('is_available', true);

    }
}
?>

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.