Magento 2 Payment Methods – How to Show/Hide Based on Amount

magento-communitymagento2payment-methodsPHPplugin

I'm using Magento 2 CE Version 2.1.0.

I have 2 payment method Paypal Express Checkout & Bank Transfer.

When the amount is greater than X then Paypal should not be displayed only Bank Transfer.

I have configured that variable on admin side on system.xml for X amount.

By referring to Magento 2 : How do we add zipcode validation on COD payment method? as per @Ashish suggestion.

I have below code now

app\code\Custom\Module\etc\frontend\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Paypal\Model\Express">
        <plugin sortOrder="1" name="restrictByCustomer" type="Custom\Module\Plugin\Payment\Method\Paypal\Available"/>
</type>

app\code\Custom\Module\Plugin\Payment\Method\Paypal\Available.php

namespace Custom\Module\Plugin\Payment\Method\Paypal;

use Magento\Customer\Model\Session as CustomerSession;
use Magento\Backend\Model\Auth\Session as BackendSession;
use Magento\Paypal\Model\Express;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Checkout\Model\Cart;

class Available {

    /**
     * @var CustomerSession
     */
    protected $customerSession;

    /**
     * @var BackendSession
     */
    protected $backendSession;

    /**
     * @param CustomerSession $customerSession
     * @param BackendSession $backendSession
     */
    public function __construct(
    CustomerSession $customerSession, BackendSession $backendSession, ScopeConfigInterface $scopeConfig, Cart $cart
    ) {
        $this->customerSession = $customerSession;
        $this->backendSession = $backendSession;
        $this->scopeConfig = $scopeConfig;
        $this->cart = $cart;
    }

    /**
     *
     * @param Paypal Express $subject
     * @param $result
     * @return bool
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function afterIsAvailable(Express $subject, $result) {

        // Do not remove payment method for admin
        if ($this->backendSession->isLoggedIn()) {
            return $result;
        }

        $cartQuote = $this->cart->getQuote()->getData();
        $cartGrandTotal = $cartQuote['base_grand_total'];

        list($grandTotal, $decimals) = explode(".", $cartQuote['base_grand_total']);
        $papalMaxShowLimit = $this->scopeConfig->getValue('mygeneral/settings/paypal_show_amount', ScopeInterface::SCOPE_STORE);

        if ($grandTotal > $papalMaxShowLimit) {
            return false;
        } else {
            return true;
        }
    }
}

I have put all condition. But on return false it's not hiding Paypal Express Checkout Method.

[Update] I'm not able select Paypal Payment Method's Radio Button On Checkout Page. It's not hiding it completely.

Best Answer

app/code/Vendor/Module/etc/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">
    <preference for="Magento\Paypal\Model\Express" type="Vendor\Module\Model\Express" />
</config>

app/code/Vendor/Module/Model/Express.php

namespace Vendor\Module\Model;

class Express extends \Magento\Paypal\Model\Express 
{

    public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
    {
    ...........Write your own logic here and reture true/false on basis of ................     
    return false;
    }
}
Related Topic