2013-04-18 31 views
0

我正在嘗試在Magento客戶註冊頁面上進行組選擇下拉菜單。我已經做了所有的以下內容:Magento下拉列表選擇客戶註冊

插入下面的代碼模板/永久/客戶/表格/ 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> 

那麼下面的在/ app /代碼/本地/法師/客戶/controllers/AccountController.php在createPostAction():

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

最後在/app/code/local/Mage/Customer/etc/config.xml以下其中加入組編號:

<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> 

我已經多次測試過,每個客戶仍然被添加爲默認客戶羣。你能看到我做錯了什麼嗎?

+0

'group_id'有Word引號 – 2013-04-18 05:02:39

+0

如果您在談論$ customer-> setGroupId($ this-> getRequest() - > getPost('group_id'));我試過,沒有引號.... getPost(group_id));它仍然不起作用:( – CaitlinHavener 2013-04-18 05:10:25

+0

它需要報價,但他們是開放和關閉報價,這將導致問題。 – 2013-04-18 05:12:29

回答

-1

使用/app/code/local/Mage/Customer/controllers/AccountController.php創建您自己的模塊狀態。如果您的模塊名稱爲Custom和名稱空間Mymodule,請在congig.xml中添加以下代碼。

<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> 

這將覆蓋您的AccountController。現在找到控制器的以下代碼。

<?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); 

} 

} 

?> 

希望這會對你有用。