0

I'm using a third party one page checkout module on this system. And added an observer "payment_method_is_active" event for the cash on delivery and bank transfer methods to enable them only for local orders.

This is how payment method hiding works at the observer.

if($observer->getEvent()->getMethodInstance()->getCode()=="cashondelivery"){
        $checkResult = $observer->getEvent()->getResult();
        $checkResult->setData('is_available', true);
        if($getCountry == 'LK'){
            $checkResult = $observer->getEvent()->getResult();
            return $checkResult->setData('is_available', true); //this is enabling the payment method at checkout page
        }
        else {
            $checkResult = $observer->getEvent()->getResult();
            return $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
        }
    }

When the cart page loads: If the country is selected to a foreign country, cart page loads hiding both payment methods without any errors, then if I change it to the default country, it loads the two methods with this error.

enter image description here

The same error appears for the bank transfer method also.

When the cart page loads: But, If the country is selected to the default at page reload, both payment methods shows on the page without any errors. And the hiding / showing methods working nicely according to the country selection without any error.

Please help to fix this error. Any help would be appreciated.

3
  • Did you manage to have any luck with solving this. I'm facing this exact problem right now and have no idea where to begin... Commented Dec 31, 2020 at 3:42
  • Can you share js code for cashondelivery-method.js? Commented Dec 31, 2020 at 10:23
  • can you put full code of observer file ??? Commented Dec 31, 2020 at 10:52

2 Answers 2

0

Replace This code with this :-

From :-

if($observer->getEvent()->getMethodInstance()->getCode()=="cashondelivery"){
        $checkResult = $observer->getEvent()->getResult();
        $checkResult->setData('is_available', true);
        if($getCountry == 'LK'){
            $checkResult = $observer->getEvent()->getResult();
            return $checkResult->setData('is_available', true); //this is enabling the payment method at checkout page
        }
        else {
            $checkResult = $observer->getEvent()->getResult();
            return $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
        }
    }

To :-

if($observer->getEvent()->getMethodInstance()->getCode()=="cashondelivery"){
        $checkResult = $observer->getEvent()->getResult();
        $checkResult->setData('is_available', true);
        if($getCountry == 'LK'){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', true); //this is enabling the payment method at checkout page
        }
        else {
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
        }
    }
0

There is no need for a plugin/module.

It can be easily managed to form the Admin configuration:

go to Stores > Configuration > Sales > Payment Methods

in most of the payment method you will see the option for:

Payment from Applicable Countries: Change it to Specific Countries

Payment from Specific Countries: Exclude the country you don't wish to enable payment method.

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.