Reply to comment

This post deals with a thing that didn't get any real answer in this thread : using checkboxes for custom registration fields

This will display a checkbox on the frontend and a "Yes/No" select in the backend.
Let's say we want to know if the customer likes (or not) the chocolate flavour.

At step 3.
Add

<chocolate><create>1</create><update>1</update></chocolate>

At step 4.
Add

'chocolate' => array(
                                  'label'         => 'Do you like chocolate',
                                  'type'          => 'int',
                                  'input'         => 'select',
                                  'visible'       => true,
                                  'required'      => true, // or false
                                  'source'        => 'eav/entity_attribute_source_boolean',
                          ),

At step 5.
Add

$setup->addAttribute('customer', 'chocolate', array(
	'label'		=> 'Do you like chocolate',
	'type'		=> 'int',
	'input'		=> 'select',
	'visible'	=> true,
	'required'	=> true, // or false
	'position'	=> 1,
        'source'        => 'eav/entity_attribute_source_boolean',
	));

At step 6.

For register.phtml, add

<div class="input-box">
 <label for="chocolate"><?php echo $this->__('Do you like chocoalte') ?><span class="required">*</span></label><br />
 <input type="checkbox" name="chocolate" id="chocolate" value="1" title="<?php echo $this->__('Do you like chocolate') ?>" class="class="required-entry checkbox" />
</div>

For edit.phtml, add

<div class="input-box">
 <label for="chocolate"><?php echo $this->__('Do you like chocoalte') ?><span class="required">*</span></label><br />
 <input type="checkbox" name="chocolate" id="chocolate" value="1" title="<?php echo $this->__('Do you like chocolate') ?>" <?php if($this->getCustomer()->getChocolate()==1): ?> checked="checked"<?php endif; ?> class="class="required-entry checkbox" />
</div>

We also need to modify the AccountController.php file (best practice is to override it to prevent from loosing modification on core updates)

Around line 583, change

foreach ($fields as $code=>$node) {
    if ($node->is('update') && isset($data[$code])) {
     $customer->setData($code, $data[$code]);
    }
   }
 
   $errors = $customer->validate();

to

foreach ($fields as $code=>$node) {
    if ($node->is('update') && isset($data[$code])) {
     $customer->setData($code, $data[$code]);
    }
   }
 
   $customer->setChocolate((boolean)$this->getRequest()->getParam('chocolate', false));
 
   $errors = $customer->validate();

An we're done. Tested on Magento 1.4.0.1.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apache>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <perl>, <php>, <python>, <ruby>, <xml>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.