7

I got all the drop down attribute value in this way

    <select class=" required-entry required-entry select" name="product[age]" id="age">
        <?php 
             $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','age');
             $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
             $attributeOptions = $attribute->getSource()->getAllOptions();
             foreach($attributeOptions as $each){?>
               <option value="<?php echo $each[value]?>"><?php echo $each["label"]?></option><?php
             }

}?>
    </select>

In Here attribute Id = age. Now i already store some value in database that should be in selected

Like this i need to display in front end

Please Help me for this Thanks in advance

Front end i need in this way

Also i used in this code

<select class=" required-entry required-entry select" name="product[age]" id="age">

<?php $productId = $this->getProduct()->getId();
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
$selectedvalue  = $product->getAttributeText('age');

$atributeCode = 'age';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$atributeCode);
        $options = $attribute->getSource()->getAllOptions();

        foreach($options as $key => $value){


            if(in_array($selectedvalue,$value)){
                ?>
                <option>
                <?php echo $value['label'] ; ?>
                </option><?php 
                continue;
            }
?>
                <option>
                <?php echo $value['label']; ?>
                </option><?php 

        }?>
</option>
</select>

it also display all attribute value not highlighted selected value

Magento 2
  • 3,834
  • 7
  • 52
  • 107

3 Answers3

14

Use getAttributeText function to get Select box attribute value in frontend from Product object

 echo  $product->getAttributeText('age_group');

it will directly display value which is selected from admin for particular product.

hope this will work for you.

liyakat
  • 3,995
  • 7
  • 27
  • 35
4

Try this,

<?php if ($_product->getData('attribute_name')): ?>
<?php echo nl2br($_product->getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product)) ?>
<?php endif;?>

(or)

$productId = 1;
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
$selectedvalue  = $product->getAttributeText('color');

$atributeCode = 'color';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$atributeCode);
        $options = $attribute->getSource()->getAllOptions();

        foreach($options as $key => $value){

            if(in_array($selectedvalue,$value)){
                echo "<pre>";
                print_r($value); //get Product's selected attribute options Values
                echo "</pre>";  
                continue;
            }

                echo "<pre>";
                print_r($value); //get remaining attribute options Values
                echo "</pre>";

        }

you can change as per your requirement

saravanavelu
  • 3,941
  • 23
  • 35
  • 51
1

​Go to magento website root directory​

app\design\frontend\MyPackage\MyTheme\template\catalog\product\view.phtml

Edit and add the code which you want to display on front-end product page.

<?php if ($_product->getColor()):?>
    <?php
         $getOptionText = $_helper->productAttribute($_product, $_product->getColor(), 'color');
         $attr = $_product->getResource()->getAttribute("color");
         if ($attr->usesSource()) {
         $color_label = $attr->getSource()->getOptionText($getOptionText);
         }
    ?>
    <div class="color">
         <div class="std"><?php echo $this->__('Color :- ') ?><?php echo $color_label; ?></div>
    </div>
<?php endif;?>
saravanavelu
  • 3,941
  • 23
  • 35
  • 51