Thêm fields cho form đăng ký khách hàng

Thảo luận trong 'Programming Questions' bắt đầu bởi Kasihati, 4 Tháng tư 2009.

  1. Kasihati

    Kasihati New Member

    Chào các thành viên trong forum.
    Mình đang gặp khó khăn khi thêm một trường vào form đăng ký khách hàng trong Magento. Có thành viên nào đã modified phần này xin chỉ giúp mình nhé
    Many thanks

    Bài viết khác cùng chuyên mục:

  2. minhdiep

    minhdiep Member

  3. Kasihati

    Kasihati New Member

    Cảm ơn bạn. Mình đã thử nhưng ko có kết quả. Thứ nhất là không thêm được attribute code "occupation" vào bảng eav_attribute. Thứ hai là mình không biết trong code gọi hàm getOccupation() được viết thế nào?! Bạn có thể test và cho mình biết kết quả nhé!
  4. senior_mager

    senior_mager New Member

    Bác có thể thêm bằng tay vào DB (cái này chỉ để hiểu hơn về Mage) :)
    Đầu tiên bác vào bảng eav_entity_type để lấy entity_type_id
    Ví dụ: "customer/customer_address" có entity_type_id=2
    Sau đấy, bác vào bảng eav_attribute để add thêm field.
    Ví dụ: thêm home_phone vào customer address, thì entity_type_id = 2, attribute_code = home_phone
    Để set attribute: setHomePhone()
    Để get attribute: getHomePhone()
    Framework nó hỗ trợ như thế, kể cả bác add new attribute cho products bên backend cũng thế.
    Attribute có code là "att_for_testing", thì có 2 hàm get, set tương ứng là: getAttForTesting, setAttForTesting
    ;)
  5. admin

    admin Mai Văn Hải Staff Member

    Upload luôn lên đây cho anh em tham khảo:

    app/design/frontend/default/yourtheme/template/customer/form/register.phtml - Has the HTML form for initial registration Note: this file will also be used for a one-shot database extension

    app/design/frontend/default/yourtheme/template/customer/form/edit.phtml - Has the HTML form for edit by customers

    app/code/core/Mage/Customer/Model/Entity/Setup.php - Has an array full of customer attributes to use for registration

    app/code/core/Mage/Customer/controllers/AccountController.php - Has a specific block of code for registration - this is no more needed

    app/code/core/Mage/Customer/etc/config.xml - Has a description of fields to be used in the forms

    Alright, for this little tutorial, we will do a couple of fields. The first one will be a text box asking the customer for their occupation/title. Navigate to somewhere around line 54 of register.html - You should see this block of code:
    Mã:
          <li>
              <div class="input-box">
                  <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
                  <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
              </div>
          </li>
    Now, we need to add our code for the occupation text box, so change that block of code to the following:
    Mã:
             <li>
              <div class="input-box">
                  <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
                  <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
              </div>
              <div class="input-box">
                  <label for="occupation"><?php echo $this->__('Occupation/Title') ?></label><br/>
                  <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getFormData()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
              </div>
          </li>
    Ok, if you refresh your create account (register) page, you will see the new field show up.

    Do a similar thing in edit.phtml, replacing getFormData by getCustomer. In my 1.1.3 version, this is at line 32. Old code:
    Mã:
                <li>
                      <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
                  </li>
                  <li>
                      <div class="input-box">
                      <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
                      <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
                      </div>
                  </li>
    New code:
    Mã:
                  <li>
                      <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
                  </li>
                  <li>
                      <div class="input-box">
                      <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
                      <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
                      </div>
                  </li>
                      <li>
                          <div class="input-box">
                              <label for="occupation"><?php echo $this->__('Occupation') ?> </label><br/>
                              <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getCustomer()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
                          </div>
                      </li>
  6. admin

    admin Mai Văn Hải Staff Member

    Now, the fun part is getting that value into the database. Now, navigate to around line 93 in Setup.php - you should see this block of code:
    Mã:
     
          'email' => array(
                                  'type'          => 'static',
                                  'label'         => 'Email',
                                  'class'         => 'validate-email',
                                  'sort_order'    => 6,
                              ),
    
    Now, we need to add our attribute to this file. So, change this block of code to:
    Mã:
     
          'email' => array(
                                  'type'          => 'static',
                                  'label'         => 'Email',
                                  'class'         => 'validate-email',
                                  'sort_order'    => 6,
                              ),
          'occupation' => array(
                                  'label'         => 'Occupation',
                                  'required'      => false,
                                  'sort_order'    => 7,
                              ),
    Right under your occupation array, you will see the group_id one, etc. You need to increment the sort_order as they were doing before. So, since the sort_order for occupation is 7, make the one after it 8, and so on.
    This part is needed no more, left here for possible reference - should be deleted at the end of October 2008

    Now, head to line 164 of AccountController.php. You should see this block of code:
    Mã:
          $customer = Mage::getModel('customer/customer')
              ->setFirstname($this->getRequest()->getPost('firstname'))
              ->setLastname($this->getRequest()->getPost('lastname'))
              ->setEmail($this->getRequest()->getPost('email'))
              ->setPassword($this->getRequest()->getPost('password'))
              ->setConfirmation($this->getRequest()->getPost('confirmation'))
              ->setId(null);
    We need to add our new occupation attribute to this, so change the code to this:
    Mã:
     
          $customer = Mage::getModel('customer/customer')
                          ->setFirstname($this->getRequest()->getPost('firstname'))
                          ->setLastname($this->getRequest()->getPost('lastname'))
                          ->setEmail($this->getRequest()->getPost('email'))
                          ->setOccupation($this->getRequest()->getPost('occupation')) //Add this
                          ->setPassword($this->getRequest()->getPost('password'))
                          ->setConfirmation($this->getRequest()->getPost('confirmation'))
                          ->setId(null);
    End of the unneeded code

Chia sẻ trang này