Site icon MageComp Blog

Solved: Disable COD payment method programmatically in Magento 2

Hello Magento Folks,

In this article, I will explain How to Disable COD payment method programmatically in Magento 2. Don’t forget to check out our previously published article to go through with the quick summary in the month of February at MageComp Magento 2 Extensions Digest February 2021 (New Release & Updates). Also checkout Best Payment Gateway in India 2021. Let’s get started with today’s topic.

Payment word when you listen than the first thing that hits your mind is security. Basically, Payment can be taken into consideration as one of the most crucial parts of any online store. And when there is the finest payment handling system the store benefits in optimizing the sales and overall revenue. 

Significant online store owners are providing and handling different payment methods at the checkout page. When all these payment methods are offered by any online store then there are chances of getting confused in managing all these facilities. 

Besides which when it comes to the COD payment method then there are many negative cases where the online store owners are afraid to provide COD as a payment method. Therefore to solve this you can follow the below-given steps.

Steps to Disable COD payment method programmatically in Magento 2:

Step 1: Firstly, we will require to create an “events.xml” file inside our extension at the following  path.

app\code\Vendor\Extension\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="custom_payment" instance="Vendor\Extension\Observer\PaymentMethodAvailable" />

    </event>

</config>

Step 2: After that, we need to create a  “PaymentMethodAvailable.php” file inside the below folder path of extension.

app\code\Vendor\Extension\Observer\PaymentMethodAvailable.php

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;

class PaymentMethodAvailable implements ObserverInterface

{

    public function execute(\Magento\Framework\Event\Observer $observer)

    {

        if($observer->getEvent()->getMethodInstance()->getCode()=="cashondelivery"){

            $checkResult = $observer->getEvent()->getResult();

            $checkResult->setData('is_available', false); 

        }

    }

}

That’s It

Wrap Up: 

Hopefully, all have implemented Disable COD payment method programmatically in your Magento 2 stores. Automate your Cash on Delivery (COD) method by installing our Magento 2 Cash On Delivery Suite that helps the customers to verify the cash on delivery option availability and charges of that if COD is not available. Featured section for COD order verification with the help of OTP and backend grid to handle COD orders to secure your stores with fake orders.

In case of any errors and difficulties you come across in implementing the above solution then let me know in the comment section below I will solve them there. 

Share the article with your Magento Friends.

Happy Reading

Exit mobile version