Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Change gender dropbox to radio button and remove empty value
 
hbstreng
Jr. Member
 
Total Posts:  3
Joined:  2012-01-08
 

Hi,

Today I started working at the registration form. I work in Magento v1.7.0. I would like to know the gender of my customers so I switch the value of the field ‘Show Gender’ to required in System > Configuration > Customers > Customer Registration. That works fine. The gender option is however a dropbox and I want to change it into checkbox (radio button). On the internet I find the following code:

<div class="input-box">
    <
label><?php echo $this->__('Gender'); ?></label>
    
<?php $options Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();?>
    <?php $value 
$this->getGender();?>
    <?php 
foreach ($options as $option):?>
    
<input type="radio" name="<?php echo $this->getFieldName('gender')?>" value="<?php echo $option['value'] ?>"<?php if ($option['value'== $value) echo ' selected="selected"' ?> /><?php echo $option['label'] ?>
    
<br />
    
<?php endforeach;?>
</div>

The problem is that the gender option is not required anymore and instead of only the value ‘male’ or ‘female’ there is also an empty value available (so 3 checkboxes instead of 2).  Can anyone help?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
sridharsubash
Jr. Member
 
Total Posts:  3
Joined:  2011-06-30
 

use if condition on $option[’value’] to avoid the empty value check box. add the condition as given below

<div class="input-box">
<label><?php echo $this->__('Gender'); ?></label>
<?php $options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();?>
<?php $value = $this->getGender();?>
<?php foreach ($options as $option):?>

<?php if ($option['value']): ?>

<input type="radio" name="<?php echo $this->getFieldName('gender')?>” value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' selected="selected"' ?> /><?php echo $option['label'] ?>

<?php endif; ?>

<?php endforeach;?>
</div>

 
Magento Community Magento Community
Magento Community
Magento Community
 
Emizen Tech
Jr. Member
 
Avatar
Total Posts:  1
Joined:  2013-07-15
India
 

you can use this to remove empty values

<div class="input-box">
    <
label><?php echo $this->__('Gender'); ?></label>
    
<?php $options Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();?>
    <?php $value 
$this->getGender();?>
    <?php 
foreach ($options as $option):?>
    <?php 
if(!empty($option['value'])):?>
    
<input type="radio" name="<?php echo $this->getFieldName('gender')?>" value="<?php echo $option['value'] ?>"<?php if ($option['value'== $value) echo ' selected="selected"' ?> /><?php echo $option['label'] ?>
    
<br />
    
<?php endif;?>
    <?php 
endforeach;?>
</div>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top