I want to display select input field which contain customer gender options in my custom layout block in magento 2 ? how can i do this?


public function getCustomerGenderOptions(){


<select name="gender">
 <?php foreach ($block->getCustomerGenderOptions() as $genderOption): ?>
    <option value="<?= $genderOption['value'] ?>"><?= $genderOption['label'] ?></option>
 <?php endforeach; ?>

Use Below code


namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;;
use Magento\Eav\Model\Config;

class Customer extends Template
   protected $eavConfig;

   public function __construct(
        Context $context,       
        Config $eavConfig
        $this->eavConfig = $eavConfig;


    public function getCustomerGenderOptions(){
        $attribute = $this->eavConfig->getAttribute('customer','gender');
        $options = $attribute->getSource()->getAllOptions();
        return $options;
| improve this answer | |

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.