Active an extension based on condition in magento 2

前端 未结 2 1759
醉话见心
醉话见心 2020-12-12 06:54

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 i

相关标签:
2条回答
  • 2020-12-12 07:01

    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

    0 讨论(0)
  • 2020-12-12 07:07

    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="Company\Module\Observer\PaymentMethodAvailable" />
        </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 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()=="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

    0 讨论(0)
提交回复
热议问题