3

I am using magento 2.1.0, I created custom attribute of customer, and add field on frontend in customer edit information form.

when I entered any value in edit form, it is saved in adimn form but not visible on frontend. means data is not retrieve on frontend.

so how to retrieve that data to show on frontend in this field:

     <div class="field bloodgroup">
            <label for="bloodgroup" class="label"><span><?php /* @escapeNotVerified */echo __('Blood Group') ?></span></label>
            <select name="bloodgroup" id="bloodgroup">
            <?php                   
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                           $model = $objectManager->create('\xxx\xxx\Model\BGroup')->avialable();
            foreach ($model as $b)
            { 
              ?>                    
            <option value="<?php echo $b['BloodGroup']; ?>"><?php echo $b['BloodGroup']; ?></option>

        <?php }   ?>     

            </select>
    </div>
  • 2
    Please add code which you have tried this far. – Jaimin Sutariya Feb 8 '17 at 7:17
2

You have to use below code to retrieve customer attribute value on edit form

<?php if($block->getCustomer()->getCustomAttribute('attribute_code')) {
   echo $block->getCustomer()->getCustomAttribute('attribute_code')->getValue();
}?>

EDIT:

    <?php $selected = '';
   if($block->getCustomer()->getCustomAttribute('attribute_code')) {
           $selected = $block->getCustomer()->getCustomAttribute('attribute_code')->getValue();
        }?>
    <div class="field bloodgroup">
                <label for="bloodgroup" class="label"><span><?php /* @escapeNotVerified */echo __('Blood Group') ?></span></label>
                <select name="bloodgroup" id="bloodgroup">
                <?php                   
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                               $model = $objectManager->create('\xxx\xxx\Model\BGroup')->avialable();
                foreach ($model as $b)
                { 
                  ?>                    
                <option value="<?php echo $b['BloodGroup']; ?>" <?php 
                 if($selected == $b['BloodGroup']){ ?> selected='selected'      <?php} ?> ><?php echo $b['BloodGroup']; ?></option>

            <?php }   ?>     

                </select>
        </div>
  • 1
    Thank you for reply, how i used this code in dropdown field? – sam Feb 8 '17 at 11:03
  • Using this code you will get value which is stored – Prashant Valanda Feb 8 '17 at 11:23
  • yes, for text field , i used and get value successfully, but i don't understand how to use this for dropdown, i edit the the question, please check. – sam Feb 8 '17 at 11:41
  • Updated answer check it – Prashant Valanda Feb 8 '17 at 11:58
  • it is not working, like after save and refresh full form is disable – sam Feb 8 '17 at 12:20

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.