skip to Main Content

Hi i have 2 payment method available in my magento2 store . One is cash on delivery and other is custom payment gateway . I install custom payment gateway extension and it is working fine .

Now i have some conditions if that condition is ok then only i need to make active that custom payment gateway extension .

My products have a product attribute called ‘otherthancod’ . If
‘otherthancod’ is active then only show the custom
payment gateway in checkout page. For that i write the following code .

        $items = $cart->getItems();
        $flag = 0;
        $count=0;
        foreach($items as $item){
            
            $attribute1 = $item->getProduct()->getData('otherthancod');
             if($attribute1){
                $flag++;
                $count++;
            }else{
                $flag--;
            }
        }
        
        if($flag == $count){
                    $checkResult = $observer->getEvent()->getResult();
                    $checkResult->setData('is_available', true); 
        }else{
                    $checkResult = $observer->getEvent()->getResult();
                    $checkResult->setData('is_available', false); 
        }
        
  

Now i want to know where i need to put this code ? I don’t want to create another extension for that .

Please help.

In my custom payment extension i have seen the following page
app/code/custompaymentgaetway/custom/Gateway/Config/config.php

class Config extends MagentoPaymentGatewayConfigConfig{
 

}

can i add if condition before this class ? I think this class is activating payment gateway .

I can see that in frontend template of my payment gateway is
viewfrontendwebtemplatecustompaymentgaetway.html. Actually i want to hide this frontend once the condition is false .

2

Answers


  1. Don’t hesitate to create your own module in app/code/YourNamespace.

    Basically you just need a registration.php file and an etc/module.xml:

    https://devdocs.magento.com/videos/fundamentals/create-a-new-module/


    Also see the following example showing how to declare the Observer for this event:

    https://magento.stackexchange.com/a/188367/27460

    Login or Signup to reply.
  2. You should not afraid of setup:upgrade command as it is OOTB Magento command it should work fine, and If something break then please fix it might be some permission issue on server side

    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 that hits on the 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="CompanyModuleObserverPaymentMethodAvailable" />
        </event>
    </config>
    

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

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

    Now the payment method Check Money Order is disabled from checkout page .

    For reference please check this link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search