Magento 1.9 – Select Customer Group at Registration

customer-groupregister

This is driving me insane. I have followed every tutorial I have found and doesn't work.
I am just trying to include a radiogroup on the register screen so a customer can select 'Corporate' or 'Individual'.

I have created my module on

/app/code/local/company/CoreModifications/Controllers/AccountController.php

This overrides the default core function with:

     class Company_CoreModifications_Customer_AccountController extends  Mage_Customer_AccountController {


    /**
     * Get Customer Model. Overrides Magento function to setup group depending on customer selection.
     *
     * @return Mage_Customer_Model_Customer
     */
    protected function _getCustomer()
    {
        $customer = $this->_getFromRegistry('current_customer');
        if (!$customer) {
            $customer = $this->_getModel('customer/customer')->setId(null);
        }
        if ($this->getRequest()->getParam('is_subscribed', false)) {
            $customer->setIsSubscribed(1);
        }
        /**
         * Initialize customer group id
         */

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

        return $customer;
    }
}

I have set up my module config file with:

 <frontend>
  <routers>
  <customer>
    <args>
      <modules>
        <Company_CoreModifications before="Mage_Customer">Company_CoreModifications_Customer</Company_CoreModifications>
      </modules>
    </args>
  </customer>
 </routers>
</frontend>

I have modified the customer config.xml on
/app/code/core/Mage/Customer/etc/config.xml with:

 <global>
   <fieldsets>
     <customer_account>
        ...  //rest of config fieldsets
       <group_id>
         <create>1</create>
         <update>1</update>
       </group_id>
     <customer_account>
   <fieldsets>
 <global>

and of course I have modified my template to include the radiogroup:

        <div class="radiogroup">
            <input name="group_id" class="user_type" value="4" type="radio" tabindex="1">
            <label for="group_id_1">Corporate</label>
            <input name="group_id" class="user_type" value="1" checked="checked" type="radio" tabindex="2">
            <label for="group_id_2">Individual</label>
        </div>

But the value is not saved on the database
The examples I have found are done on versions before Magento 1.9 which is the one I'm using.

I have tried to follow the saving process on Magento and it seems that it saves a customer entity with the correct data but this is not saved on the database yet, then it saves two address entities and then again another customer entity where the customer details are correct but the group_id is changed to the default value, in my case 1. After that it is when the entity is saved so the group_id is incorrect (in my tests I want to get 4).

Best Answer

I believe Magento uses default controller (ie Mage_Customer_AccountController) itself to process the customer creation in this case.

There are some problems/suggestions which I need to put forward.

  1. Your controller location is wrong. It should be /app/code/local/Company/CoreModifications/controllers/Customer/AccountController.php instead of /app/code/local/company/CoreModifications/Controllers/AccountController.php

  2. Your controller definition should look like this.

    <?php
    require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AccountController.php');
    
    class Company_CoreModifications_Customer_AccountController
        extends  Mage_Customer_AccountController
    {
        // some code
    }
    

    For more information on how to overwrite a controller, you can refer this link

  3. Do not edit a core file. ie do not edit app/code/core/Mage/Customer/etc/config.xml. It is wrong. You can do the same-thing in your module's config.xml file

    <config>
        ...
        <default>
            <customer>
                <create_account>
                    <group_id>
                        <create>1</create>
                        <update>1</update>
                    </group_id>
                </create_account>
            </customer>
        </default>
    </config>
    

    In fact, AFAIK this declaration is not at all necessary to achieve what you are trying to do. So you can skip this step.

  4. Your code inside _getCustomer() method look okay for me. However you can improve your code like this.

    if($this->getRequest()->getPost('group_id', false)){
        $groupId = (int)$this->getRequest()->getPost('group_id');
        $this->setData('group_id', $groupId);
    } else {
        $customer->getGroupId();
    }  
    
  5. Ultimately double check your folder names and locations. Your module folder location is app/code/local/Company/CoreModifications/

  6. Last but not least, make sure you flushed cache.

Related Topic