0
votes

I am trying to make a group selection dropdown on the Magento customer registration page work. I have done all of the following:

Inserted the following code into template/persistent/customer/form/register.phtml:

<label for="group_id" style='margin-top:15px;'><?php echo $this->__('Which group do you belong to? (select "Pet Owner" if you are uncertain)') ?><span class="required">*</span></label>
            <div style='clear:both'><p style='color:red;'>Note: DVM/DACVO and Institution accounts require administrative approval.</p></div>
            <div class="input-box" style='margin-bottom:10px;'>
                    <select style='border:1px solid gray;' name="group_id" id="group_id" title="<?php echo $this->__('Group') ?>" class="validate-group required-entry input-text" />
                        <?php $groups = Mage::helper('customer')->getGroups()->toOptionArray(); ?>
                        <?php foreach($groups as $group){ ?>
                            <?php if ($group['label']=="Pet Owner" || $group['label']=="DVM / DACVO" || $group['label']=="Institution"){?>
                                <option value="<?php print $group['value'] ?>"><?php print $group['label'] ?></option>
                            <?php } ?>
                        <?php } ?>
                    </select>
                    </div>

Then the following in /app/code/local/Mage/Customer/controllers/AccountController.php in the createPostAction():

$customer->setGroupId($this->getRequest()->getPost(‘group_id’)); 

Finally the following in /app/code/local/Mage/Customer/etc/config.xml where group id was added:

  <fieldsets>
            <customer_account>
                <prefix>
                    <create>1</create>
                    <update>1</update>
                    <name>1</name>
                </prefix>
                <firstname>
                    <create>1</create>
                    <update>1</update>
                    <name>1</name>
                </firstname>
                <middlename>
                    <create>1</create>
                    <update>1</update>
                    <name>1</name>
                </middlename>
                <lastname>
                    <create>1</create>
                    <update>1</update>
                    <name>1</name>
                </lastname>
                <suffix>
                    <create>1</create>
                    <update>1</update>
                    <name>1</name>
                </suffix>
                <email>
                    <create>1</create>
                    <update>1</update>
                </email>
                <password>
                    <create>1</create>
                </password>
                <confirmation>
                    <create>1</create>
                </confirmation>
                <dob>
                    <create>1</create>
                    <update>1</update>
                </dob>
                <group_id><create>1</create><update>1</update></group_id>
                <taxvat>
                    <create>1</create>
                    <update>1</update>
                </taxvat>
                <gender>
                    <create>1</create>
                    <update>1</update>
                </gender>
            </customer_account>

I have tested it several times and every customer is still being added as the default customer group. Can you see what I am doing wrong?

2
‘group_id’ has Word quotesFrancis Kim
If your talking about $customer->setGroupId($this->getRequest()->getPost(‘group_id’)); I tried that without the quotes ....getPost(group_id)); and it still doesn't work :(CaitlinHavener
It needs quotes but they are open and close quotes which will cause problems.Francis Kim
Okay I tried that too and its not working ''CaitlinHavener
$customer->setGroupId($this->getRequest()->getPost('group_id'));CaitlinHavener

2 Answers

0
votes
<script type="text/javascript">
$(document).on('change','#country',function() {
var param = 'country='+$('#country').val();
$.ajax({
    showLoader: true,
    url: YOUR_URL_HERE,
    data: param,
    type: "GET",
    dataType: 'json'
}).done(function (data) {
    //data.value has the array of regions
});

});

Add this script in your js file.

public function getCountries()
{
    $country = $this->directoryBlock->getCountryHtmlSelect();
    return $country;
}

use \Magento\Customer\Model\ResourceModel\Group\Collection and the above code in your block.

<div class="field group_id required">
        <label for="group_id" class="label"><span><?php echo __('Group') ?></span></label>
        <div class="control">
            <select name="group_id">
                <?php foreach ($groups as $key => $data) { 
                    ?>
                <option value="<?php 
                echo $data['value']; ?>"><?php echo $data['label']; ?></option>
                <?php }?>
            </select>
        </div>
    </div>

And in the phtml file add the above field. I know it's an old post, but giving the solution just in case anyone needs it.

-1
votes

Create your own module instate of using /app/code/local/Mage/Customer/controllers/AccountController.php. Please add the following code in your congig.xml if your module name Custom and namespace Mymodule.

<global>
    <fieldsets>
        <customer_account>
            <group_id><create>1</create><update>1</update></group_id>
        </customer_account>
    </fieldsets>
</global>
<frontend>  
<routers>
 <customer>
    <args>
        <modules>
            <Mymodule_Custom before="Mage_Customer_AccountController">Mymodule_Custom</Mymodule_Custom>
        </modules>
    </args>
  </customer>
</routers>

This will override your AccountController. Now find the following code for the controller.

<?php 

require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AccountController.php');
class Mymodule_Custom_AccountController extends Mage_Customer_AccountController
{
public function createPostAction()
{
$session = $this->_getSession();
    if ($session->isLoggedIn()) {
        $this->_redirect('*/*/');
        return;
    }
    $session->setEscapeMessages(true); // prevent XSS injection in user input
    if (!$this->getRequest()->isPost()) {
        $errUrl = $this->_getUrl('*/*/create', array('_secure' => true));
        $this->_redirectError($errUrl);
        return;
    }

    $customer = $this->_getCustomer();

    try {
        $errors = $this->_getCustomerErrors($customer);

        if (empty($errors)) {
            if($this->getRequest()->getPost('group_id')){ 
            $customer->setGroupId($this->getRequest()->getPost('group_id'));
            } else {
                $customer->getGroupId(); 
            }
            $customer->cleanPasswordsValidationData();
            $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);
            $session->setEscapeMessages(false);
        } else {
            $message = $e->getMessage();
        }
        $session->addError($message);
    } catch (Exception $e) {
        $session->setCustomerFormData($this->getRequest()->getPost())
            ->addException($e, $this->__('Cannot save the customer.'));
    }
    $errUrl = $this->_getUrl('*/*/create', array('_secure' => true));
    $this->_redirectError($errUrl);

}

}

?>

Hope this will work for you.