Magento Tutorials

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

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

8 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago