Custom signup form assign customer to customer group
-
06-10-2020 - |
Question
I made a copy of the default Magento ce 1.9.1 customer signup form. I then added the below fields to the form
<input type="hidden" name="group_id" value="3">
<input type="hidden" name="success_url" value="./vipsuccess">
<input type="hidden" name="error_url" value="" />
Then I copied the AccountController.php
from the core/mage/customer/controllers/
to local/mage/customer/controllers
I then added the below code to the public function createPostAction()
section.
However my issue is that when I use the form to create an account the customer is still assigned to the general group?
public function createPostAction()
{
$errUrl = $this->_getUrl('*/*/create', array('_secure' => true));
if (!$this->_validateFormKey()) {
$this->_redirectError($errUrl);
return;
}
/** @var $session Mage_Customer_Model_Session */
$session = $this->_getSession();
if ($session->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
if (!$this->getRequest()->isPost()) {
$this->_redirectError($errUrl);
return;
}
$customer = $this->_getCustomer();
/** * Get Customer ID from customer registration form or if its not set in form post then use default. */
if($this->getRequest()->getPost('group_id'))
{
$customer->setGroupId($this->getRequest()->getPost('group_id'));
}
else
{
$customer->getGroupId();
}
try {
$errors = $this->_getCustomerErrors($customer);
if (empty($errors)) {
$customer->cleanPasswordsValidationData();
$customer->save();
$customer->setGroupId($this->getRequest()->getPost('group_id'));
$customer->save();
$this->_dispatchRegisterSuccess($customer);
$this->_successProcessRegistration($customer);
return;
} else {
$this->_addSessionError($errors);
}
} catch (Mage_Core_Exception $e) {
$session->setCustomerFormData($this->getRequest()->getPost());
if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
$url = $this->_getUrl('customer/account/forgotpassword');
$message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
} else {
$message = $this->_escapeHtml($e->getMessage());
}
$session->addError($message);
} catch (Exception $e) {
$session->setCustomerFormData($this->getRequest()->getPost());
$session->addException($e, $this->__('Cannot save the customer.'));
}
$this->_redirectError($errUrl);
}
Solution
After save updating the customer group
you need to add website id
too
$customer->save();
$custid=$customer->getId();
$customeragain = Mage::getModel("customer/customer")->load($custid);
$website_id = Mage::app()->getWebsite()->getId();
$customeragain->setData('group_id', $this->getRequest()->getPost('group_id'));
$customeragain->setWebsiteId($website_id);
$customeragain->save();
OTHER TIPS
You need to also check the "Disable automatic group assignment" cause he will be reassigned after saving on VAT Validation.
If $customer
is an instance of Mage_Customer_Model_Customer
you can then set the field before saving with:
$customer->setDisableAutoGroupChange(1)