cancel
Showing results for 
Search instead for 
Did you mean: 

Enable/Disable COD payment method programmatically

SOLVED

Enable/Disable COD payment method programmatically

Hello everyone,

 

I want to enable COD for a particular cart using custom module. Can anyone suggest me, what should i do to achieve this? As i fetched all the available payment methods but unable to set a particular payment method like COD.

 

Your reply will be hearty  appreciated.

Thanks

 

Certified Magento 2 Developer
Vivek Singh
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Enable/Disable COD payment method programmatically

Hello @Vivek Singh,

 

You can enable table rate shipping method and you can easily restrict zip code within in it. Please refer this link https://docs.magento.com/m1/ce/user_guide/shipping/table-rates.html

 

--
If my answer is useful, please Accept as Solution & give Kudos

View solution in original post

4 REPLIES 4

Re: Enable/Disable COD payment method programmatically

Hi @Vivek Singh

 

Here i would say you required to write down your custom module as an Plugin.

 

Create a custom plugin and check for the conditions and based on that you can enable/disable your payment method.

 

Below i am sharing the url links for the same 

 

https://magento.stackexchange.com/questions/130466/magento-2-how-do-i-programmatically-remove-paymen...

 

https://magento.stackexchange.com/questions/180422/magento-2-payment-methods-hide-by-code

 

Hope it helps!

if issue solved,Click Kudos & Accept as Solution

Re: Enable/Disable COD payment method programmatically

Hello @Vivek Singh,

 

First you will need to create events.xml file under app/code/Company/Module/etc/. Then write “payment_method_is_active” event in it. This is the event which hits on checkout page for payment method availability.

<?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="Company\Module\Observer\PaymentMethodAvailable" />
    </event>
</config>

Now create PaymentMethodAvailable.php under Company/Module/Observer/ and write following code in the file. I am disabling the cashondelivery payment method, you can change payment method code according to your need.

 

<?php
 
namespace Company\Module\Observer;
 
use Magento\Framework\Event\ObserverInterface;
 
 
class PaymentMethodAvailable implements ObserverInterface
{
    /**
     * payment_method_is_active event handler.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // you can replace "checkmo" with your required payment method code
        if($observer->getEvent()->getMethodInstance()->getCode()=="cashondelivery"){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
        }
    }
}

Now the payment method cashondelivery is disabled from checkout page.

 

--
If my answer is useful, please Accept as Solution & give Kudos

 

Re: Enable/Disable COD payment method programmatically

Hello @gelanivishal

I am able to set payment method by this solution, actually i want cash on delivery for particular product according to zipcode when zipcode available in your area cash on delivery should be enable.

Certified Magento 2 Developer
Vivek Singh

Re: Enable/Disable COD payment method programmatically

Hello @Vivek Singh,

 

You can enable table rate shipping method and you can easily restrict zip code within in it. Please refer this link https://docs.magento.com/m1/ce/user_guide/shipping/table-rates.html

 

--
If my answer is useful, please Accept as Solution & give Kudos