2

I have added dropdown to choose Gender on Register-page. There are only two options ie Male and Female in Backend. On Frontend, In Gender dropdown, first value display as Blank then Male and then Female.

I want to Remove that blank option and female Option as default. If user not select any option from dropdown then it should be considered as Female.

Gender Dropdown On Register page

<label for="<?php echo $this->getFieldId('gender')?>" class="gender-
  label <?php if ($this->isRequired()) echo ' class="required"' ?>"><?
  php if ($this->isRequired()) echo '<em>*</em>' ?><?php echo $this-
  >__('Gender') ?>
</label>


<div class="input-box">
      <select id="<?php echo $this->getFieldId('gender')?>" name="<?php 
        echo $this->getFieldName('gender')?>" title="<?php echo 
        Mage::helper('core')->quoteEscape($this->__('Gender')) ?>"<?php if 
        ($this->isRequired()):?> class="validate-select"<?php endif; ?> <?
        php echo $this->getFieldParams() ?>>
         <?php $options = Mage::getResourceSingleton('customer/customer')-
           >getAttribute('gender')->getSource()->getAllOptions();?>
              <?php $value = $this->getGender();?>
                 <?php foreach ($options as $option):?>
                     <option value="<?php echo $option['value'] ?>"<?php 
                       if ($option['value'] == $value) echo ' 
                       selected="selected"' ?>><?php echo $option['label'] 
                    ?></option>
                 <?php endforeach;?>
    </select>
</div>
  • Update your phtml – Magento 2 Sep 13 '17 at 4:29
1

Try to remove first (empty) option and set value as Female (2) when there is no selected value:

<label for="<?php echo $this->getFieldId('gender') ?>" class="gender-label <?php if ($this->isRequired()) echo ' class="required"' ?>">
    <?php if ($this->isRequired()) echo '<em>*</em>' ?><?php echo $this->__('Gender') ?>
</label>


<div class="input-box">
    <select id="<?php echo $this->getFieldId('gender') ?>" name="<?php
    echo $this->getFieldName('gender') ?>" title="<?php echo
    Mage::helper('core')->quoteEscape($this->__('Gender')) ?>"
        <?php if ($this->isRequired()): ?> class="validate-select"<?php endif; ?> <?php echo $this->getFieldParams() ?>>
        <?php
        $options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();
        unset($options[0]);
        $value = $this->getGender() ? $this->getGender() : '2'; // 2 for female
        foreach ($options as $option): ?>
            <option value="<?php echo $option['value'] ?>"<?php
            if ($option['value'] == $value) echo ' 
                       selected="selected"' ?>><?php echo $option['label']
                ?></option>
        <?php endforeach; ?>
    </select>
</div>
0

Just update your code

<?php $options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();?>

with

<?php $options = array_reverse(Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions(false));?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.