3

I want to assign specific payment method only for selected one customer group,

successfully get customer group & Payment method code,

How to add condition, following are my workout,

Following Tutorial : https://meetanshi.com/blog/disable-payment-method-programmatically-in-magento/

workout :

<?php
    class Gta_FindPaymentGateway_Model_Observer
    {
        public function paymenter($Observer)
        {
            if(Mage::getSingleton('customer/session')->isLoggedIn())
            {
                // Get group Id
                $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();

                //Get customer Group name
                $group = Mage::getModel('customer/group')->load($groupId);

                $allPaymentMethods = Mage::getModel('payment/config')->getAllMethods();

                foreach($allPaymentMethods as $paymentMethod)
                {
                    if(($paymentMethod->getCode() == 'payubiz') && $group->getCode() !== 'Tester'))
                        {
                            $result = $Observer->getResult();   
                            $result->isAvailable = false;
                        }
                }
            }
        }
    }
?>

Above script no error, but condition not working.

1
  • did you check my answer? – Sagar Parikh Jan 29 '20 at 4:14
5

I tried this on a vanilla installation of Magento 1.9.3.6

Below are the files.

app/code/local/Gta/FindPaymentGateway/etc/config.xml

<?xml version="1.0"?>

<config>
    <modules>
        <Gta_FindPaymentGateway>
            <version>0.1.0</version>
        </Gta_FindPaymentGateway>
    </modules>
    <global>
        <events>
            <payment_method_is_active>
                <observers>
                    <findpaymentgateway_payment_method_is_active>
                        <class>Gta_FindPaymentGateway_Model_Observer</class>
                        <method>paymentMethodIsActive</method>
                    </findpaymentgateway_payment_method_is_active>
                </observers>
            </payment_method_is_active>
        </events>
    </global>
</config>

app/code/local/Gta/FindPaymentGateway/Model/Observer.php

<?php

class Gta_FindPaymentGateway_Model_Observer
{
    public function paymentMethodIsActive(Varien_Event_Observer $observer)
    {
        // Assume you want to show Check/Money Order payment method only for General group customers.
        // The General customer group ID is 1
        // The code for Check/Money Order is 'checkmo'
        $currentCustomerGroup = (int)Mage::getSingleton('customer/session')->getCustomerGroupId();

        // If the current payment method is Check/Money Order and current customer group is NOT 1,
        // then you need to disable the payment method Check/Money Order
        if (($observer->getMethodInstance()->getCode() == 'checkmo') && ($currentCustomerGroup !== 1)) {
            $result = $observer->getResult();
            $result->isAvailable = false;
        }

    }
}

NOTE: In this example according to the observer function, only the customers belong to customer group id 1 will be able to see the Check/Money Order payment method in the checkout page.

9
  • Here is my observer => justpaste.it/6qhci, i want payubiz payment method only for tester customer group, my tester customer group id => 4 – zus Jan 31 '20 at 4:51
  • @zus : Is this working for you?. – Sukeshini Jan 31 '20 at 5:45
  • no, still not working, return error Fatal error: Uncaught Error: Call to a member function getCode() on null in C:\wamp64\www\magento\app\code\local\Gta\FindPaymentGateway\Model\Observer.php on line 16 – zus Jan 31 '20 at 5:47
  • 16th line => if (($Observer->getMethodInstance()->getCode() == 'payubiz') && ($currentCustomerGroup !== 4)) – zus Jan 31 '20 at 5:47
  • put this as the parameter of the paymenter function Varien_Event_Observer $observer – Sukeshini Jan 31 '20 at 5:49
1

I worked on similar functionality in magento 2, maybe it can help, let me know if you need anymore help in it.

I created functionality for a specific customer group which will eligible for reward point

    $getCustomerGroup = $this->_scopeConfig->getValue('Reward_Section/Reward_group/Reward_Third_field', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

    $selectedCustomerGroups = (explode(",",$getCustomerGroup));

    if($this->customerSession->isLoggedIn()) {
        $getCurrentCustomerGroup = $this->customerSession->getCustomer()->getGroupId();
        if(in_array($getCurrentCustomerGroup, $selectedCustomerGroups)) 
        {
        //disable code for payment method
        }
    }elseif(in_array('0', $selectedCustomerGroups)){
        //for not loggedin customer group
        //disable code for payment method
    }

I focused on condition as you asked on the question, not the logic behind disable payment method.

PS: above code is in magento 2

-1

Please follow this blog

https://meetanshi.com/blog/disable-payment-method-programmatically-in-magento/

you need to add your condition in this code

8
  • Yes, i followed that tutorial, codeshare.io/jGzQ9 can i get help? – zus Jan 28 '20 at 7:00
  • yes, please use this code and set your condition – Jigs Parmar Jan 28 '20 at 7:02
  • I have doubt in how to apply payment method condition in it. codeshare.io/jGzQ9 – zus Jan 28 '20 at 7:03
  • codeshare.io/jGzQ9 i change in code please use this – Jigs Parmar Jan 28 '20 at 7:04
  • 1
    FYI - I need payubiz only for Tester group – zus Jan 28 '20 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.