Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Please follow this blog

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

you need to add your condition in this code

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top