Magento: Selecting Customer Group During Registration
In Magento Professional Edition v1.11, allowing customers to select their group during registration is a common requirement but not straightforward by default. Let’s walk through the steps to implement this feature without modifying core files.
Problem Overview
You’ve set up the radio buttons in your form/register.phtml file to display customer groups at registration:
<div class="input-box">
<label for="group_id"><?php echo $this->__('Select your customer group') ?></label><br />
<?php
$groups = Mage::helper('customer')->getGroups()->toOptionArray();
foreach ($groups as $group){
echo '<input type="radio" name="group_id" value="'.$group['value'].'" class="validate-radio">'.$group['label'].'</input><br/>';
}
?>
</div>
However, the selected group ID is not being saved to the database when a new customer registers. In your custom AccountController.php, you’ve tried setting the group ID:
public function createPostAction()
{
// ... existing code ...
if($this->getRequest()->getPost('group_id')){
$customer->setGroupId($this->getRequest()->getPost('group_id'));
}
// rest of the method...
}
But when you check in the admin interface, the group still shows as ‘General’. The key issue lies in properly overriding the core Magento controller without directly modifying it.
Solution: Properly Overriding the Controller
To correctly override the AccountController, follow these steps:
- Create your module structure:
Your directory hierarchy should look like this:
app/code/local
- WACI
- Customer
- controllers
- AccountController.php
- etc
- config.xml
- Configure your
config.xmlfile:
Ensure the config.xml is correctly configured to overwrite the AccountController:
<config>
<modules>
<WACI_Customer>
<version>0.1.0</version>
</WACI_Customer>
</modules>
<global>
<controllers>
<customer>
<rewrite>
<account_controller>
WACI_Customer_AccountController
</account_controller>
</rewrite>
</customer>
</controllers>
</global>
</config>
- Update your
AccountController.php:
Replace the contents of AccountController.php with the following:
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
class WACI_Customer_AccountController extends Mage_Customer_AccountController
{
public function createPostAction()
{
// ... existing code ...
if ($this->getRequest()->isPost()) {
$customer = Mage::registry('current_customer');
if($this->getRequest()->getPost('group_id')){
$customer->setGroupId($this->getRequest()->getPost('group_id'));
} else {
$customer->setGroupId(Mage_Customer_Model_Group::GROUP_GENERAL_ID);
}
}
// rest of the method...
}
}
- Flush Magento Cache:
After making these changes, ensure you flush your Magento cache for the changes to take effect:
php shell/abstract.php --flush-memcache
php shell/indexer.php reindexall
- Test Registration:
Visit your frontend and attempt a new customer registration. You should now see the group selection radio buttons working properly. The selected group ID will be saved in the database.
Conclusion
Properly overriding Magento controllers is crucial for maintaining a clean, upgrade-safe codebase. By following this approach, you can extend the core functionality without directly modifying it.
Remember to always test your changes thoroughly after making modifications to ensure they work as expected across different scenarios.
For more advanced customizations and best practices in Magento development, I recommend checking out Laravel Company’s guide on Magento module development.