Jun 15, 2013
kalpesh

Magento get attribute value

To get product attribute value in Magento is very common. You will need it everytime when dealing with catalog products in your development. Attributes have different types, which can be any of Text Field, Text Area, Date, Yes/No, Multiple Select, Dropdown, Price, Gallery, Media Image, Fixed Product Tax (as you can see in backend Manage Attributes > New Attribute > Catalog Input Type for Store Owner). To get the value for these different types of attributes there is no one straight line of code, you will need to use different code for getting values from plain attributes, while different code to get values from select and multiselect attributes.

Get attribute value for PLAIN TEXT, TEXTAREA or DATE type attribute:

1
$attribute_value = $product->getShirtSize(); //for shirt_size attribute

Get value from SELECT, MULTISELECT, DROPDOWN or YES/NO attributes:

1
$attribute_value = $product->getAttributeText($attribute_code);

OR

1
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);

Get value from PRICE attributes:

1
$attribute_value = $product->getNew_price(); //for attribute code "new_price"

and in product list page,

1
$attribute_value = $product->getNewPrice();

Get attribute value by attribute code and productID WITHOUT loading whole product

1
$attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($product_id, $attribute_code, $store_id);

3 Comments

  • I am struggeling for about 10 hours now.

    What I am trying to accomplish:

    The simple php code to display the selected LABELS and VALUES of a multiselect product attribute in list.phtml

    I want to use the LABEL as icon image title and the VALUE as the actual image source.

    I have search Google and tried many many ways. But nothin is exactly what I need.

    Can you please please help me out?

  • I have to get the brand value, but is inside a admin table.
    with this -> $product_data[‘merk’]=$product->getAttributeText(‘manufacturer’);
    I only can get the Dealer or the Number.

    Can you help me?

  • thanks it helped me a lot…………

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications