2

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

0

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.

  • Thank you Rajeev, The first two points were correct on my end but I didn't copy them properly, sorry for that. the mistake was something really stupid: the closing </config> was missing from my module's config.xml. I realised when I was doing the point 3. Well, at least doing that made me realise of my mistake. Thanks. – Mario Lopez Oct 8 '15 at 4:28
  • @MarioLopez Every one learns from Mistakes. So I would say Mistakes are the great teachers. Happy to help you out :) – Rajeev K Tomy Oct 8 '15 at 4:31
1

You are trying to fetch data using POST method. My suggestion is use request-method for fetch group_id in your controller. Also checking with $this->getRequest()->getParam('group_id', false) will return proper value in your controller

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

TO

if ($this->getRequest()->getParam('group_id', false)) {      
    $customer->setGroupId($this->getRequest()->getParam('group_id', false)); 
}
  • Thanks Amit but that didn't work, my user is still created in the General group :( – Mario Lopez Oct 7 '15 at 4:58
  • have you check ... controller is overrider properly?May be your controller did not overrider properly – Amit Bera Oct 7 '15 at 4:59
  • I dont see any mistakes in $this->getRequest()->getPost() usage in his code. I think that should be the correct code, be honest. Could you please explain the advantage of $this->getRequest()->getParam() over the prior one, Amit ? – Rajeev K Tomy Oct 7 '15 at 5:02
  • ` $this->getRequest()->getParam()` does not depends form method type POST/GET it is always give the result – Amit Bera Oct 7 '15 at 5:16
  • It is true, but that does not make getPost() method wrong here. If we check core Magento, we can see that Magento use getPost() for every POST reqeust action. – Rajeev K Tomy Oct 7 '15 at 6:10

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.