Payments Methods Based On Customer Groups
Hi,
[Magento version 1.4.1.1]
Our main aim in this post is to offer a specific payment method(whether it’s PayPal, Check/Money Order or CreditCard) based on the group of the logged in customer. Furthermore, we’re going to create a new Order Status where each new order generated from our custom customer group automically get this order status assigned. We’re going to maintain Magento modularity by encapsulate this functionality in a module.
Let’s start by the module installation file, we’ll user ABC as our namespace and XYZ as our module name:
Module Installation File:
/app/etc/modules/ABC_XYZ.xml
<?xml version="1.0"?> <config> <modules> <ABC_XYZ> <active>true</active> <codePool>local</codePool> </ABC_XYZ> </modules> </config>
Nothing interesting going on here, so let’s move to the module configuration file:
Module Configuration File:
/app/code/local/ABC/XYZ/etc/config.xml
<?xml version="1.0"?> <config> <modules> <ABC_XYZ> <version>1.0</version> </ABC_XYZ> </modules> <global> <blocks> <checkout> <rewrite> <onepage_payment_methods>ABC_XYZ_Block_Onepage_Payment_Methods</onepage_payment_methods> </rewrite> </checkout> </blocks> <sales> <order> <statuses> <expedite translate="label"><label>Expedite</label></shop> </statuses> </order> </sales> </global> <frontend> <events> <checkout_type_onepage_save_order_after> <observers> <abc_xyz_model_type_observer> <type>model</type> <class>ABC_XYZ_Model_Type_Observer</class> <method>ProcessShopOrder</method> </abc_xyz_model_type_observer> </observers> </checkout_type_onepage_save_order_after> </events> </frontend> </config>
What we’re basically doing here are three things, first we are overriding the block responsible for showing each of the payments methods on the checkout. We also add our new order status Expedite. The last part is the most interesting one, we’re registering a new class we created as an observer for the checkout_type_onepage_save_order_after event. This means that whenever Magento receives an order from the user and saves it, it automatically calls our function. This is a common pattern that you can find everywhere (native win32 application using the available windows hooks for example).
We will also need to add another field in Magento Admin panel where we can set which customer group can use this payment methods, we can do this for the Check / Money Order methods with the following code, other payments methods can be added by changing the checkmo tag:
Module System Configuration File:
/app/code/local/ABC/XYZ/etc/system.xml
<config> <sections> <payment translate="label" module="payment"> <groups> <checkmo translate="label"> <fields> <specificgroups translate="label"> <label>Payment for Specific Customer Groups</label> <frontend_type>multiselect</frontend_type> <sort_order>52</sort_order> <source_model>adminhtml/system_config_source_customer_group</source_model> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </specificgroups> </fields> </checkmo> </groups> </payment> </sections> </config>
Now some PHP coding, we start with the observer class:
Observer Class:
/app/code/local/ABC/XYZ/Model/Type/Observer.php
<?php
class ABC_XYZ_Model_Type_Observer {
public function __construct(){
/*
* Event-Observer Magento
*/
}
public function ProcessShopOrder($observer) {
$method = Mage::getSingleton('checkout/session')->getQuote()->getPayment()->getMethodInstance();
$method_code = $method->getCode();
if ($method_code == 'checkmo') {
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$customer = Mage::getSingleton('customer/session');
$data = $customer->getCustomer();
$group_id = $data->getGroupId();
$specificgroups = explode(",",$method->getConfigData('specificgroups'));
if( in_array($group_id,$specificgroups) ){
$order= $observer->getEvent()->getOrder();
$order->setStatus('expedite')->save();
}
}
}
}
}
?>
We are setting here the status of any order belonging to the customer group we selected from Magento Admin Panel to Expedite. The only thing remaining to override the block responsible for showing the payments methods in the checkout:
The Block
/app/code/local/ABC/XYZ/Block/Onepage/Payment/Methods.php
<?php
class ABC_XYZ_Block_Onepage_Payment_Methods extends Mage_Checkout_Block_Onepage_Payment_Methods {
protected function _canUseMethod($method) {
/**
* Check for User Groups
*/
if ($method->getCode() == 'checkmo') {
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$customer = Mage::getSingleton('customer/session');
$data = $customer->getCustomer();
$group_id = $data->getGroupId();
$specificgroups = explode(",",$method->getConfigData('specificgroups'));
$testgroup = trim($method->getConfigData('specificgroups'));
if(!in_array($group_id,$specificgroups) && $testgroup!==""){
return false;
}
} else return false;
}
return parent::_canUseMethod($method);
}
}
?>
That’s about it, a bit long but easy to follow.