0

I was trying to disable the Cash on Delivery payment method using payment_method_is_active event and observer. But It stopped placing orders due to JavaSript error in the console.

Error in the checkout page:

    Uncaught TypeError: Unable to process binding "html: function(){return getInstructions() }"
Message: Cannot read properties of undefined (reading 'cashondelivery')
    at UiClass.getInstructions (cashondelivery-method.min.js:1:256)
    at html (eval at createBindingsStringEvaluator (knockout.min.js:285:237), <anonymous>:3:64)
    at evaluateValueAccessor (knockout.min.js:306:54)
    at knockout.min.js:330:181
    at update (knockout.min.js:399:434)
    at ko.dependentObservable.disposeWhenNodeIsRemoved (knockout.min.js:336:74)
    at Function.evaluateImmediate_CallReadThenEndDependencyDetection (knockout.min.js:212:240)
    at Function.evaluateImmediate_CallReadWithDependencyDetection (knockout.min.js:207:626)
    at Function.evaluateImmediate (knockout.min.js:206:46)
    at ko.computed.ko.dependentObservable (knockout.min.js:195:71)

Observer

<?php
declare(strict_types=1);

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class TogglePaymentMethods implements ObserverInterface
{
    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $result = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        $quote = $observer->getEvent()->getQuote();

        if($method_instance->getCode() == 'cashondelivery'){
            $result->setData('is_available', false);
        }
    }

}

2 Answers 2

0

Try to use this code in observer

<?php
declare(strict_types=1);
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
    
class TogglePaymentMethods 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); 
       }
    }
}

Also, you have set is_available true, it needs to be false for disabling payment method

3
  • I am sorry for the typo. Updated it. I meant $result->setData('is_available', false); though. Mar 30 at 5:26
  • Does it work ? If you're facing same issue, then try to check the store configuration for Payment Method, if its disable from there, then also it will throws error !!! Mar 30 at 6:26
  • No, It was not working. The payment method is enabled in the admin panel. Mar 30 at 8:05
0

you can use a plugin on the Magento\OfflinePayments\Model\Checkmo class.

  1. Create the file app/code/[Vendor]/[Module]/etc/di.xml and add the following code:

    <config>
         <type name="Magento\OfflinePayments\Model\Checkmo">
             <plugin name="disable_checkmo_payment" type="[Vendor]\[Module]\Plugin\Model\Checkmo" sortOrder="1"/>
         </type>
    </config>
    
  2. Create the file app/code/[Vendor]/[Module]/Plugin/Model/Checkmo.php and add the following code:

    <?php
    
     namespace [Vendor]\[Module]\Plugin\Model;
    
     use Magento\OfflinePayments\Model\Checkmo as CheckmoModel;
    
     class Checkmo
     {
         /**
          * Disable the Cash on Delivery payment method
          *
          * @param CheckmoModel $subject
          * @param bool $result
          * @return bool
          */
         public function afterIsAvailable(CheckmoModel $subject, $result)
         {
             // Check if the payment method is Cash on Delivery
             if ($subject->getCode() == 'checkmo') {
                 // Disable the payment method
                 return false;
             }
    
             return $result;
         }
     }
    
3
  • I did try this method too. But no luck. Mar 30 at 5:27
  • I just edited my code, could you try please ?
    – Dotsquares
    Mar 30 at 5:36
  • I didn't check on checkmo but I did the same for cashondelivery. Not working. Mar 30 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.