简体   繁体   中英

Magento: Group Field in Customer Registration Page

Is there a way to avoid editing Magento Core ( app/code/core/Mage/Customer/controllers/AccountController.php ) file in order to enable a Customer Group option field in Registration Page?

Searching about it, the only successful method to do it is editing the previously mentioned core file, finding this code:

$customer->getGroupId();

And replacing it with:

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

The best case scenario is including, somehow, this code in a local module file, something like app/code/local/Enterprise/Module/controllers/AccountController.php . I actually have a module to include the function, but still not found the correct code to add on the module.

It is always recommended to create a new module for every new functionality, but if you are changing just one line of code, you can alternatively, place your modified file in the following path (note the replacement of core by local): app/code/local/Mage/Customer/controllers/AccountController.php

Then, you can revert your core file, and the autoloader should still get your modified version. That way you will avoid to modify the core, but bear in mind that when you upgrade Magento to a new version you might need to update this file as well.

Check this link for more information: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_create_a_local_copy_of_app_code_core_mage

To do it the right way: Extending the controller within a module.

Place this in your config.xml:

<frontend>
    <routers>
        <customer>
            <args>
                <modules>
                    <company_module before="Mage_Customer">Company_Module</company_module>
                </modules>
            </args>
        </customer>
    </routers>
</frontend>

Then, create the controller file app/code/local/Company/Module/controllers/AccountController.php :

<?php
require_once 'Mage/Customer/controllers/AccountController.php';
class Company_Modulename_AccountController extends       Mage_Customer_AccountController
{
 /**
  * Get Customer Model
  *
  * @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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM