0

How to do Magento 2 payment method based on customer group in frontend checkout page .

Iam checking condition for Remaining Amount

But i want customer group also , if

Customer group=4 then only its should display

if($creditdata->getRemainingAmount()<$paymentamount)

      return false;

        else

            return true;


Thanks in advance.

This is my code ....


<?php


namespace Ced\CreditLimit\Model;

class CreditPaymentHide extends \Ced\CreditLimit\Model\CreditPayment

{


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

    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $checkoutdata = $objectManager->create('Magento\Checkout\Model\Cart');
        $customer = $objectManager->create('Magento\Customer\Model\Session');

        if(!$objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('b2bextension/credit_limit/enable')){
        return false;
      }
      if($customer->isLoggedIn())
        {
            $creditdata = $objectManager->create('Ced\CreditLimit\Model\CreditLimit')->load($customer->getCustomerId(),'customer_id');


        $discountTotal =0;
            foreach ($checkoutdata->getQuote()->getAllItems() as $item){
                $discountTotal += $item->getDiscountAmount();
            }
            $total = $checkoutdata->getQuote()->getBaseGrandTotal();
            $paymentamount = $total-$discountTotal;
            if($creditdata->getRemainingAmount()<$paymentamount)
                return false;
            else
                return true;
        }
        else{
            return false;
        }
    }

}
3
  • Please post some code you've done or some research for the same Aug 24, 2018 at 23:01
  • describe your requirement in detail. Do you want this in custom payment method? Or you are asking something else? Aug 25, 2018 at 13:39
  • i have update code please have a ckeck MR.Vivek and MR.Dhiren..... Aug 27, 2018 at 6:09

1 Answer 1

0

Create custom module and use event "payment_method_is_active" to fire an observer.

    <?php
    namespace YourNamespace\DisabledPgByCustomergroup\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\Request\DataPersistorInterface;
    use Magento\Framework\App\ObjectManager;
    use Magento\Framework\Event\Observer;

    class ObserverforDisabledPaymentgateway implements ObserverInterface
    {
      public function __construct() {}

      /**
       *
       * @param \Magento\Framework\Event\Observer $observer
       * @return void
       */
      public function execute(Observer $observer)
      {
        $result          = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        $quote           = $observer->getEvent()->getQuote();
        if(
          $quote !== null && 
          $quote->getCustomerGroupId() =='FOO_CUSTOMER_GROUP_ID'
        ){
            /* Disable FOO payment gateway */
            if($method_instance->getCode() == 'FOO_PAYMENT_METHOD_CODE'){
                 $result->isAvailable = false;
            }
        }
      }
    }

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.