3

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.

1

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:

<config>
    <modules>
        <Npm_Customer>
            <active>true</active>
            <codePool>local</codePool>
        </Npm_Customer>
    </modules>
</config>

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"?>
<config>
    <modules>
        <Npm_Customer>
            <version>1.0</version>
        </Npm_Customer>
    </modules>
    <global>
        <events>
            <customer_save_before>
                <observers>
                    <npm_customer_save_observer>
                        <type>singleton</type>
                        <class>Npm_Customer_Model_Customer_Observer</class>
                        <method>customerSaveBefore</method>
                    </npm_customer_save_observer>
                </observers>
            </customer_save_before>
        </events>
    </global>
</config>

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:

<?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.

Thanks

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.