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

13

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.

  • no it display only selected value i need all the value but selected value should be highlighted – Magento 2 Aug 17 '15 at 6:33
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

  • check my edited question please – Magento 2 Aug 17 '15 at 6:16
  • no it display only selected value i need all the value but selected value should be highlighted – Magento 2 Aug 17 '15 at 6:33
  • check the updated answer – saravanavelu Aug 17 '15 at 6:59
  • No it display all the value only not highlight selected value – Magento 2 Aug 17 '15 at 7:21
  • you have to change the things as per your need – saravanavelu Aug 17 '15 at 7:27
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;?>
  • Empty drop down only displayed – Magento 2 Aug 24 '15 at 5:10

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.