0

I would like to hide the checkmo payment method on checkout, if the billing and shipping address are not the same.

Thank you!

1 Answer 1

0

Try below code

Create file : 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="disable_payment" instance="Vendor\Extension\Observer\Paymentmethoddisable" />
    </event>
</config>

Create file : app/code/Vendor/Extension/Observer/Paymentmethoddisable.php

<?php
namespace Vendor\Extension\Observer;
use Magento\Framework\Event\ObserverInterface;
class Paymentmethoddisable implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo"){ 

           // write your condition here
           if( 'billingaddress' != 'shippingaddresss'){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); 
            }
        }
    }
}
2
  • Thank you, but 'billingaddress' != 'shippingaddresss' does not work.
    – Daniel
    Commented Jun 5, 2023 at 9:42
  • You can get the billing and shipping address from the current quote. And write the condition according to your logic.
    – MP Raj
    Commented Jun 5, 2023 at 9:49

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.