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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

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

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago