I have created a custom field in magento onepage checkout page. When a user is filling this field his customer group should set to group 1 and if the field is left blank his customer group should be group 2.

My main problems

1) How to set the customer group value programatically when user is registering thru checkout page

2) How to relate my custom field value and customer group value in checkout process.

Thanks in advance.


To achieve solution for above question, You need to use event observer pattern of magento, their is event called customer_save_before, That will help you to get required answer.

Now, we have to add in our custom module. I am calling my module Customer and it is part of the Npm group. So, the file should be named app/etc/modules/Npm_Customer.xml. Add the following code into this file:


Next, we need to create the code for the module. The idea behind our code is that we are going to create an observer on the customer_save_before event. Magento has a number of events that we can observe but most are beyond the scope of this article. What is useful is that the customer_save_before event is called both when a customer is created and when a customer makes changes to their account. This means that the one observer will be able to do the work for both events.

All of the code we are writing for our module will be located inside the app/code/local/Npm/Customer/ directory. The first file is etc/config.xml:

<?xml version="1.0"?>

The next step is to create the class that we have just told Magento we are going to use, and should be defined in the file Model/Customer/Observer.php:

class Npm_Customer_Model_Customer_Observer extends Mage_Core_Model_Abstract
     * observer for the customer saved event
    public function customerSaveBefore($observer)
        try {
            $customer = $observer->getCustomer();
            if (null != $customer->getPermissionCode()) {
                $customer->setData('group_id', 4); // Set the new customer group
            } else {
                $customer->setData('group_id', 1); // Set to the default customer group
        } catch ( Exception $e ) {
            Mage::log("customer_save_before observer failed: " . $e->getMessage());

Customers are now automatically assigned to your new group if they supply Permission code from the registration form.


| improve this answer | |

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.