Back to Top

Magento Product Attributes

Updated 21 March 2013

Get Custom Attributes Value in Frontend.

1. Suppose our custom attribute code is “test_attr” and of type Text Field or Text Area .

<?php
     $product = Mage::getModel("catalog/product")->load($product_id);
     echo $product->getTestAttr();
?>

I use getTestAttr() because my attribute code is “test_attr”, Use uppercase after underscore or begining of character, like

for “testattr” method should be getTestattr()

for “test_attr” method should be getTestAttr()

Searching for an experienced
Magento Company ?
Find out More

2. Suppose our custom attribute code is “test_attr” and of type Multiselect or Dropdown .

<?php
     $product = Mage::getModel("catalog/product")->load($product_id)
     $attributeCode = 'test_attr';
     $AllAttributes = $product->getAttributes();
        if(array_key_exists($attributeCode , $AllAttributes)){
            $Attribute = $AllAttributes["$attributeCode"];
            $Value = $Attribute->getFrontend()->getValue($product);
        }
     echo $Value;
?>

3. To get all the options of attributes .

<?php  
     $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','test_attr');
     $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
     $attributeOptions = $attribute->getSource()->getAllOptions();
     foreach($attributeOptions as $each){
       echo $each["label"]." ".$each["value"]."<br>";
     }
?>

4. To get value of text type, attribute.

$_product->getData("test_attr");

5. To get values of multiselect type, attribute.

$_product->getResource()->getAttribute('test_attr')->getFrontend()->getValue($_product);

6. To get value of dropdown type, attribute.

$_product->getAttributeText('test_attr');

Enjoy !!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home