1

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);
}
5
  • Try adding $customer->save() when setting the customer group id
    – MagentJo
    Commented Jan 31, 2017 at 18:59
  • So I added if($this->getRequest()->getPost('group_id')) { $customer->setGroupId($this->getRequest()->getPost('group_id')); $customer->save(); } else { $customer->getGroupId(); $customer->save(); } but still not working
    – Jayreis
    Commented Jan 31, 2017 at 19:22
  • Actually, after reviewing this a bit more, in your if(empty($errors)){...} code, after $customer->save() do your setting of the group id then save again. I didn't realize that this was during the creation of the customer, so first, the customer has to be saved, then you assign/set the group id, then save the customer right after.
    – MagentJo
    Commented Jan 31, 2017 at 21:24
  • I updated the section of code for the if(empty($errors)){ as per your suggestions Magentjo . I then cleared Magento cache and still not saving the new group id?
    – Jayreis
    Commented Feb 1, 2017 at 15:07
  • Are you logging to make sure that the group id is being passed? Log this $this->getRequest()->isPost() and make sure all of the values needed are being posted.
    – MagentJo
    Commented Feb 1, 2017 at 16:38

2 Answers 2

2

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();  
1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.