5

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.

3

3 Answers 3

6
+50

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;
    }
}
7
  • Things which u have mentioned all are in Question
    – Jackson
    Commented Oct 6, 2016 at 8:41
  • Now check my updated answer. Commented Oct 6, 2016 at 9:00
  • Nope still now working only putting return false; then also it's Displaying Paypal Checkout
    – Jackson
    Commented Oct 6, 2016 at 9:16
  • Which paypal method you are using ? This is for paypal express checkout and its working for me. Commented Oct 6, 2016 at 9:24
  • Yes it's Paypal Express Checkout. It's not working for Me don't know why.
    – Jackson
    Commented Oct 6, 2016 at 9:28
2

You can use this to resolve this :

Magento 2 : How do we add zipcode validation on COD payment method?

in this example just replace the class Cashondelivery with your payment method class on which you want to add filter.

In this example

public function afterIsAvailable(Cashondelivery $subject, $result)
{
// Do not remove payment method for admin
if ($this->backendSession->isLoggedIn()) {
    return $result;
}
   ......return true/false according to your condition.......

return true/false;

}
13
  • Thanks @Ashish. Where should i put condition to check?
    – Jackson
    Commented Sep 23, 2016 at 5:49
  • @AnkitShah, Pl check my updated answer. Commented Sep 23, 2016 at 5:54
  • Ok. Means I need to put the condition on Paypal Express Checkout. Let me check @Ashish & revert.
    – Jackson
    Commented Sep 23, 2016 at 6:00
  • Hi @Ashish it's not hiding my Paypal Method. So updated code in question. Thanks
    – Jackson
    Commented Sep 23, 2016 at 6:37
  • @AnkitShah, Have you used Paypal Express class path instead of Cashondelivery class path of default Magento class properly ? Commented Sep 23, 2016 at 7:14
1

Finally, I have solved by overiding Paypal Express Model

In you di.xml file

<preference for="Magento\Paypal\Model\Express" type="Custom\Module\Model\Express" />

In Your app\code\Custom\Module\Model\Express.php

namespace Custom\Module\Model;

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

    public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) {

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $cart = $objectManager->get('Magento\Checkout\Model\Cart');
        $scopeConfig = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface');

        if (isset($cart)) {
            $grandTotal = $cart->getQuote()->getGrandTotal();
            if (!empty($grandTotal)) {
                list($grandTotal, $decimals) = explode(".", $grandTotal);

                if ($grandTotal > XXXX) { #Replace XXXX with Amount Say 1000/10
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        }
        return true;
    }
}

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.