PHP Magento Tips

Hide Cash on Delivery Payment Method for Certain Customer Groups In Magento

Rate this posting:
{[['']]}

It is very easy to hide cash on delivery payment method for certain customer groups In Magento. in fact you can hide any payment method for certain customer groups by this tutorial.

Step 1: Create module initialization file. Create file in app/etc/modules/Mycompany_Payment.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_Payment>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
             </depends>
        </Mycompany_Payment>
    </modules>
</config>

Step 2: Create Module config.xml file. Create file in app/code/local/Mycompany/Payment/etc/config.xml and add below code.

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_Payment>
            <version>0.0.1</version>
        </Mycompany_Payment>
    </modules>
     <global>
        <models>
            <mycompany_payment>
            <class>Mycompany_Payment_Model</class>
            </mycompany_payment>
        </models>
    </global>
    <frontend>
        <events>
            <payment_method_is_active>
                <observers>
                    <Mycompany_Payment>
                        <type>singleton</type>
                        <class>mycompany_payment/observer</class>
                        <method>filterpaymentmethod</method>
                    </Mycompany_Payment>
                </observers>
            </payment_method_is_active>
        </events>
    </frontend>
</config>

Step 3: Create system.xml file to give customer group option for cash on delivery payment method. Create file in app/code/local/Mycompany/Payment/etc/system.xml  and add below code in it:

<?xml version="1.0"?>
<config>
   <sections>
        <payment>
            <groups>
                <cashondelivery>
                    <fields>
                        <customer_payment_group translate="label">
                            <label>Hide Cashondelivery for Below Customer Group</label>
                            <!--<frontend_type>select</frontend_type>-->
                            <frontend_type>multiselect</frontend_type>
                            <source_model>adminhtml/system_config_source_customer_group</source_model>
                            <sort_order>100</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </customer_payment_group>
                    </fields>
                </cashondelivery>
            </groups>
        </payment>
    </sections>
</config>

Step 4: Create Observer file to add condition for payment method. Create file in app/code/local/Mycompany/Payment/Model/Observer.php and add below code in it.

<?php
class Mycompany_Payment_Model_Observer {

    public function filterpaymentmethod(Varien_Event_Observer $observer) {


        /* call get payment method */
        $method = $observer->getEvent()->getMethodInstance();
  $customergroup = Mage::getStoreConfig('payment/cashondelivery/customer_payment_group',Mage::app()->getStore());
  $customergrouparray = explode(",",$customergroup);
  
  if(Mage::getSingleton('customer/session')->isLoggedIn()) {
  
   $roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
   $role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
   
   /*if(is_array($customergrouparray)){
    Mage::log($customergrouparray, null, 'payment.log', true);
   }*/
   
   if($method->getCode()=='cashondelivery'){
    $quote = $observer->getEvent()->getQuote();
  
    if(in_array($roleId, $customergrouparray)){
     $result = $observer->getEvent()->getResult();
     $result->isAvailable = false;
     return;
    }else{
     $result = $observer->getEvent()->getResult();
     $result->isAvailable = true;
    }
   }
  }  
    }
}
?>
Tag : Magento
0 Comments On "Hide Cash on Delivery Payment Method for Certain Customer Groups In Magento"

Back To Top