Magento2 Payment Methods – Retrieve All Payment Methods in Plugin

checkoutmagento-2.1magento2payment-methods

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?

Best Answer

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;
}
Related Topic