Don Korleone
Don Korleone

Reputation: 331

Adding additional field to registration form in Magento 1.9

i was using this tutorial to add additional field in Magento 1.9 in Registration form: http://www.fontis.com.au/blog/magento/know-more-about-your-customers-adding-custom-signup-attributes

But unfortunately it not works. I am new in Magento and need some help. I would appreciate step by step instruction on how to create new module in order to be able to add this additional field in current registration form Magento 1.9.

Upvotes: 3

Views: 18570

Answers (2)

Larzan
Larzan

Reputation: 9667

If you want to do it manually, directly via SQL because you only need a quick fix, here is how the values are stored in the DB for Magento 1.9 (might work for M2).

This is not good practice as it is a direct DB manipulation that won't work if you move your theme to another server without using the same DB. There are some use cases in which this might be warranted though ;)

  1. Add a new entry into the 'eav_attribute' table, in this case, to have a checkbox, use

    INSERT INTO `eav_attribute`
      (`entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`)
    VALUES
      ( 1,  'gdpr_accept',  NULL,   NULL,   'int',  NULL,   NULL,   'select',   'GDPR', NULL,   'eav/entity_attribute_source_boolean',  1,  1,  '0',    0,  NULL);
    

    This will add a checkbox entry with the name 'gdpr_accept' (to be used in the form html later) and the title 'GDPR' (will be used when referring to it).

  2. Add a new entry to 'customer_eav_attribute', using the attribute_id of the entry you created in 1.

    INSERT INTO `customer_eav_attribute`
      (`attribute_id`, `is_visible`, `input_filter`, `multiline_count`, `validate_rules`, `is_system`, `sort_order`, `data_model`)
    VALUES
      (ATTRIBUTE_ID_FROM_1, 1,  NULL,   0,  NULL,   0,  100,    NULL);
    

    This will add the necessary setting to the new value.

  3. Add a new entry to the 'customer_form_attribute' table, again with the attribute_id from 1.

    INSERT INTO `customer_form_attribute`
      (`form_code`, `attribute_id`) 
    VALUES
      ('customer_account_create',   ATTRIBUTE_ID_FROM_1);
    

    This will tell Magento to check for the new checkbox value when validating the registration form.

  4. This step is the same as with the accepted answer, you can now add the checkbox to the form and it will be validated automagically by Magento:

    <li class="field gdpraccept">
      <div class="input-box">
        <input type="checkbox" id="gdpr_accept" name="gdpr_accept" value="1"
               title="<?php echo $this->__('Accept the privacy policy') ?>" class="checkbox required-entry">
      </div>
      <label for="is_subscribed">
          <?php echo $this->__('Registering you confirm that you accept our  ') ?>
          <a href="<?php echo Mage::helper('cms/page')->getPageUrl( 25 ) ?>">
              <?php echo $this->__('privacy policy'); ?>
          </a>.
      </label>
    </li>
    

    Note that in this case the id of the page with the privacy policy is 25, it will probably be different in your case.

This was a simple checkbox case, if you want a different field, with custom validation, have a look at the 'eav_attribute' table, there you can find examples of other fields that have been added.

Or, even better, walk the recommended way and use a module like the http://www.silksoftware.com/magento-module-creator/ is creating (you can do the same thing they do in your own custom module)

Upvotes: 0

James H.
James H.

Reputation: 383

Ok, I just did it, here is how. Go to http://www.silksoftware.com/magento-module-creator/ and using its Module Creator to create a new module called "YourCustomerAttribute".

  1. Set "Add Customer Attribute" to YES
  2. Make proper inputs and selections as you needed.
  3. Make sure to select the forms you needed the new attributes to be used.
  4. Generate the module.
  5. Upload the module to your Magento folder.
  6. Modify located at app/design/frontend/base/default/template/persistent/customer/form/register.phtml and add:

     <div class="input-box">
     <label for="YourAttributeName"><?php echo $this->__('YourAttributeName') ?><span class="required">*</span></label><br />
     <input type="text" name="YourAttributeName" id="YourAttributeID" value="<?php echo $this->htmlEscape($this->getFormData()->getYourAttributeName()) ?>" title="<?php echo $this->__('YourAttributeName') ?>" class="required-entry input-text" />
    </div>
    
  7. If you want customer to be able to modify the attribute in customer panel, then modify app/design/frontend/base/default/template/customer/form/edit.phtm and add:

    <li>
        <label for="YourAttributeName" class="required"><em>*</em><?php echo $this->__('YourAttributeName') ?></label>
        <div class="input-box">
            <input type="text" name="YourAttributeName" id="YourAttributeID" value="<?php echo $this->escapeHtml($this->getCustomer()->getYourAttributeName()) ?>" title="<?php echo $this->__('YourAttributeName') ?>" class="input-text required-entry" />
        </div>
    </li>
    
  8. Refresh all caches.

Upvotes: 17

Related Questions