Home > Magento > Customer Group Registration

Customer Group Registration

Posted on: 29th Jan 2010 By: Adam Moss 19 Comments

A feature that is absent from the Magento frontend is the ability for a customer to choose which customer group they belong to during registration, no different to what is available to the admin when they choose to register new customers. This can be quite important in instances where you wish to apply different tax rules to different types of customer. For example you might have General Customers and VAT Exempt customers.

To enable this ability is actually extremely simple. Firstly, XML.

Make a LOCAL copy of config.xml from app > code > core > Mage > Customer > etc > config.xml and add the following line of code into the customer account fieldset:

<fieldsets>
<customer_account>
<prefix><create>1</create><update>1</update><name>1</name></prefix>
<firstname><create>1</create><update>1</update><name>1</name></firstname>
<middlename><create>1</create><update>1</update><name>1</name></middlename>
<lastname><create>1</create><update>1</update><name>1</name></lastname>
<suffix><create>1</create><update>1</update><name>1</name></suffix>
<email><create>1</create><update>1</update></email>
<group_id><create>1</create><update>1</update></group_id>
<password><create>1</create></password>
<confirmation><create>1</create></confirmation>
<dob><create>1</create><update>1</update></dob>
<taxvat><create>1</create><update>1</update></taxvat>
</customer_account>
</fieldsets>

Now you need to add code to the frontend of the store, in the registration form. Open template/customer/form/register.phtml and add the following code somewhere in the form:

<div class="input-box">
<label for="group_id"><?php echo $this->__('Group') ?><span class="required">*</span></label><br/>
<select name="group_id" id="group_id" title="<?php echo $this->__('Group') ?>" class="validate-group required-entry input-text" />
<?php $groups = Mage::helper('customer')->getGroups()->toOptionArray(); ?>
<?php foreach($groups as $group){ ?>
<option value="<?php print $group['value'] ?>"><?php print $group['label'] ?></option>
<?php } ?>
</select>
</div>

That’s all there is to it – customers can now choose which group they belong to during sign up.

19 Responses to “ Customer Group Registration ”

  1. CodeSoda
    #1 | 11th May 2010

    Ello,

    Im using ver. 1.3.2.4 and I cannot locate a config.xml in the layout folder, is there another xml that I can place this onto? The 2nd part of your post I can get to show, but it doesn’t submit with the customer sign up form, and I’m assuming it may have to be something with the xml (although im not sure why you have all “1″ on the values, i possible could you explain a little more?)

  2. CodeSoda
    #2 | 11th May 2010

    actually i was able to get this to work correctly, instead of looking for a config.xml in teh layouts, i added it to app\code\code\Mage\Customer\etc\config.xml

    under line 96, just an fyi for anyone else, btw thanks for the info

  3. Adam Moss
    #3 | 8th June 2010

    CodeSoda, thanks for your correction. I have updated the post with the correct file location.

  4. Thomas
    #4 | 31st August 2010

    Hi
    Thank you for this, it’s really easy to do.

    Have you an idea how to add the group when you are on “one page checkout”?
    It’s easy to display the select with differents groups, but the data isn’t registred :(

    i tried to add 11 in app/code/core/Mage/Checkout/etc/config.xml but didn’t work :(

  5. James
    #5 | 25th October 2010

    This was really helpful but I also need to add it to the one page checkout and I can’t get it to work. Like Thomas I can add the dropdown without a problem but I can’t get it to save the data. Did anyone have any luck with this?

  6. zeplin
    #6 | 30th December 2010

    I followed every step and can’t get it to work. Everything works except when I go in the backend and manage customers it looks like the customers are in placed in the general customer group? Can someone help?

  7. Peter Jaap
    #7 | 6th January 2011

    Hi guys,

    I fixed the problem. It seems that in Magento 1.4.x the code changed so this doesn’t work as stated above anymore. You also need to add something in AccountController.php.

    This is what you do;

    1. Copy app/code/core/Mage/Customer/controllers/AccountController.php to app/code/local/Mage/Customer/controllers/AccountController.php to preserve changes done throughout upgrades

    2. Open app/code/local/Mage/Customer/controllers/AccountController.php and go to line 267. This line reads; “$customer->setData($code, $data[$code]);”

    3. Change this line into:

    if($code==’group_id’) {
    $customer->setGroupId($data['group_id']);
    } else {
    $customer->setData($code, $data[$code]);
    }

    Save the file and enjoy! I will look into the onepage problem as well in a day or so.

  8. Peter Jaap
    #8 | 6th January 2011

    Ok, after a few hours of trying several things (I think I created over a hundred accounts in the process), I finally figured out how to implement the customer group select option in the Onepage Checkout.

    I take it you already know how to add the field itself to the form; the problem was that it just wasn’t registered. This is how I did it (M1.4.1.1 installation).

    1. Copy app/code/core/Mage/Checkout/Model/Type/Onepage.php to app/code/local/Mage/Checkout/Model/Type/Onepage.php to preserve the modifications throughout future upgrades.

    2. In the function saveBilling, right at the start, add;
    $this->getQuote()->setCustomerGroupId($data[’group_id’]);
    This is because we need to transfer the variable $data[’group_id’] to another function and this is one of the easiest ways to do that.

    3. In the function _prepareNewCustomerQuote, at the end, it says;
    $quote->setCustomer($customer)
    ->setCustomerId(true);
    Right before this, add;
    $customer->setGroupId($quote->getData(’customer_group_id’));

    Et voila! The customer group gets stored in the database!

  9. Pol
    #9 | 21st January 2011

    Hello, your line:

    $customer->setGroupId($quote->getData(’customer_group_id’));

    doesn’t work. You must retrieve the group_id selected in billing in an appropiate way, and not with $quote->getData(’customer_group_id’), but How?? I dont know…please try something

  10. Prattski
    #10 | 10th May 2011

    I would suggest NOT doing what the poster of this article suggests. You should never overwrite core files, which he has you doing with the Customer core module, editing it’s config.xml.

    If you want to do it properly, write your own simple module and add the xml changes to your own config.xml file. You can then use your own layout xml file and your own template file and keep everything a little separated, should you ever want to remove this functionality.

  11. Adam Moss
    #11 | 10th May 2011

    Not everyone will know how to write modules, my solution works fine as long as you create config.xml in the local folder.

  12. Prattski
    #12 | 10th May 2011

    Writing modules is amazingly easy, especially when all you need is a config.xml. Why not rewrite your post so that you teach people how to do it properly instead? All you will need is 1 extra file in the etc/modules/ directory :)

    I’m just a proponent of doing things properly. I am not keen at all with even utilizing the app/code/local/Mage/ overrides. It works, but it is dirty, and there’s no way to disable the changes.

  13. Adam Moss
    #13 | 10th May 2011

    I agree that modules are a much better practice. For something as simple as this though, many developers prefer a quick fix. I prefer to think I’m just giving people the code, it’s up to them how they utilize it.

  14. William
    #14 | 4th July 2011

    Hello
    Congratulations on the tutorial, worked perfectly with me.
    As we only work with two groups (individual and company) I want to know how overwritten the “select” with “2 radiobuttons.”
    graciously.

  15. Laurie
    #15 | 10th July 2011

    I did this and it worked kind of. when registering it gave me a drop down box with the groups. I clicked wholesale but when you go to customer info it shows it as signing up as general and no wholesale prices show up. Also, for wholesale accounts i would like them to give business info and sales tax id, and have an approval process. do you know how i can do that?
    thanks!

  16. Hafsa
    #16 | 28th July 2011

    Great it works for me thank you so much.

  17. Anna Green
    #17 | 16th September 2011

    Easy Peasy – Now Iv got a question for you, I have two store views one with tiered pricing that is only viewable to one customer group, is there a way to redirect customer groups to there appropriate store views on log in?

  18. ronald
    #18 | 28th November 2011

    Please help how to add

    ” if($code==’group_id’) {
    $customer->setGroupId($data['group_id']);
    } else {
    $customer->setData($code, $data[$code]);
    }”

    This code in magento 1.4.2..please help me where to add this code

  19. sonal
    #19 | 22nd December 2011

    Try this code……It works…..

    if($this->getRequest()->getPost(‘group_id’))
    {
    $customer->setGroupId($this->getRequest()->getPost(‘group_id’));
    }
    else
    {
    $customer->getGroupId();
    }

    /*Add this code in AccountController.php of app/code/core/Mage/Customer/controllers/*/

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.