4

I am trying to disable all payment methods when specific payment method is enabled for some set of pin codes.

Please find below code to get an idea of what I am doing.

di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Vendor\Module\Model\Module">
        <plugin sortOrder="1" name="restrictByCustomer" type="Vendor\Module\Plugin\Payment\Method\Module\Available"/>
    </type>
</config>

app/code/Vendor/Module/Plugin/Payment/Method/Module/Available.php

<?php

namespace Vendor\Module\Plugin\Payment\Method\Module;

use Magento\Customer\Model\Session as CustomerSession;
use Magento\Backend\Model\Auth\Session as BackendSession;
use Vendor\Module\Model\Module;
use \Magento\Checkout\Model\Session as CheckoutSession;

class Available
{
    protected $checkoutSession;

    protected $customerSession;

    protected $backendSession;

    public function __construct(
        CustomerSession $customerSession,
        BackendSession $backendSession,
        CheckoutSession $checkoutSession
    ) {
        $this->customerSession = $customerSession;
        $this->backendSession = $backendSession;
        $this->checkoutSession = $checkoutSession;
    }

    public function afterIsAvailable(\Magento\Payment\Model\Method\AbstractMethod $subject, $result)
    {
        $paymentMethods =  $subject->getCode(); //I am getting only one payment method. I want to get all payment methods here.

        if($subject->getCode() == 'clickcanarias')
        {
            return false;
        }

        return $result;
    }
}

Basically, I am trying to disable all payment methods when my payment method is active. I want to get all the payment methods list in $subject. For that purpose which class needs to override?

2
  • hi @vishwas let me know whether u r able to disable payment method 'clickcanarias' as per u r code? Commented Jul 24, 2017 at 11:09
  • 1
    Yes, but I want to disable all other methods when this method is enabled. I am getting only this method here. That's the issue. Commented Jul 24, 2017 at 11:10

5 Answers 5

1

By Dependency Injection:

protected $cartModel;

public function __construct(
   ...
   Magento\Checkout\Model\Cart $cartModel
   ...
) {
    $this->cartModel = $cartModel;
}


public function afterIsAvailable(\Magento\Payment\Model\Method\AbstractMethod $subject, $result)
{
    $postcode = $this->cartModel->getQuote()->getShippingAddress()->getPostcode();
    $country = $this->cartModel->getQuote()->getShippingAddress()->getCountry();
    $province = $this->cartModel->getQuote()->getShippingAddress()->getRegion();
    $paymentMethodCode = $subject->getCode();

    if ($paymentMethodCode == 'method-code') {
        return $result; //Return result for true scenario. return true wont work
    } else {
        return false;
    }
}

By ObjectManager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$postcode = $cart->getQuote()->getShippingAddress()->getPostcode();
$country = $cart->getQuote()->getShippingAddress()->getCountry();
$province = $cart->getQuote()->getShippingAddress()->getRegion();
$paymentMethodCode = $subject->getCode();

if ($paymentMethodCode == 'clickcanarias') {
    return $result; //Return result for true scenario. return true wont work
} else {
    return false;
}
1

create events.xml file under app/code/Company/Module/etc/ Then we need to use below event in it.

"payment_method_is_active" events.xml

<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 Company/Module/Observer/PaymentMethodAvailable.php Below is sample code.

<?php

namespace Company\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class PaymentMethodAvailable implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // you can replace "custom_ship" with your required payment method code
        if($observer->getEvent()->getMethodInstance()->getCode()=="custom_ship"){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); 
        }
    }
}

Hope it helps!! Thanks.

3
  • 1
    By this way, I am getting only first payment method, not all payment method that's why I used plugin way to do this. Commented Jul 24, 2017 at 11:08
  • Yes this will be called each time when magento will check if payment method is enabled or not. so you just need to write invers code if you want to disable all other payment method then return false in all case only in your case return true. Commented Jul 24, 2017 at 11:39
  • Just try to log the name of shipping method name you will get all shipping method name in log. Commented Jul 24, 2017 at 11:40
1

Magento provides API for retrieving the list of active payment methods: \Magento\Payment\Api\PaymentMethodListInterface::getActiveList.

The API interface https://github.com/magento/magento2/blob/2.1-develop/app/code/Magento/Payment/Api/PaymentMethodListInterface.php#L27 and its implementation - https://github.com/magento/magento2/blob/2.1-develop/app/code/Magento/Payment/Model/PaymentMethodList.php#L81.

You can add the after plugin to filter only needed payment methods.

0

Please try like this

namespace Company\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class PaymentMethodAvailable implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // you can replace "custom_ship" with your required payment method code
        if($observer->getEvent()->getMethodInstance()->getCode() !="paypal_express"){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); 
        }
    }
}
6
  • By this way, I am getting only first payment method, not all payment methods that's why I used plugin way to do this. I am getting only one method in $observer->getEvent()->getMethodInstance()->getCode() Commented Jul 24, 2017 at 11:13
  • yes i do understand vishwas just use != Commented Jul 24, 2017 at 11:14
  • so that all payment methods will be disable except your payment method Commented Jul 24, 2017 at 11:14
  • instead of 'paypal_express' keep your payment method code and try it once. thanks Commented Jul 24, 2017 at 11:15
  • I have tried this but it seems to not working. I am not getting any other payment methods except first method. So how does this code works? Commented Jul 24, 2017 at 11:17
0

If you wanted to use a plugin maybe try the Owebia Shipping plugin (https://marketplace.magento.com/owebia-owebia-shipping-2.html)

You can call on custom fields and show only certain shipping methods/costs at certain times.

Maybe have a look at the documentation (https://owebia.com/os2/en/doc) before you install it to make sure you can configure it as necessary

Its extremely versatile so i'm sure you can use it to get to where you want to

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.