3

I have created custom shipping method "Pick up at the store" and payment method "Pay in store" and I would like to hide all other payment methods, when I choose "Pick up at the store" shipping method.

I know, that payment method class has function "isAvailable", but it isn't a good idea to create special conditions for all available methods.

Please advice how I can hide all other payment method except "Pay in store", when "Pick up at the store" shipping method is choose. Thanks.

  • Did you get this? What's going on? – 3CE Sep 24 '18 at 18:11
1

You can disable remaining payment methods using "payment_method_is_active" event

app/code/Company/Module/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="custom_payment" instance="Company\Module\Observer\PaymentMethodAvailable" />
    </event>
</config>

Company/Module/Observer/PaymentMethodAvailable.php

<?php

namespace Company\Module\Observer;

use Magento\Framework\Event\ObserverInterface;


class PaymentMethodAvailable implements ObserverInterface
{

    protected $shippingMethod;

    public function __construct(
           \Magento\Quote\Api\Data\ShippingMethodInterface $shippingMethod){

           $this->shippingMethod = $shippingMethod;
    } 
    /**
     * payment_method_is_active event handler.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
         $methodTitle = $this->shippingMethod->getMethodTitle();
          if($methodTitle=="Pick up at the store"){
                if($observer->getEvent()->getMethodInstance()->getCode()!=="payinstore"){
                $checkResult = $observer->getEvent()->getResult();
                $checkResult->setData('is_available', false);
           }
         }

    }
}

Ref link - https://webkul.com/blog/disable-payment-method-programmatically-magento2/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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