blakcaps
blakcaps

Reputation: 2647

Magento Retrieving Attribute Values

There are different ways to retrieve attribute values in Magento,

$options=$_product->getAttributeText('some_attribute')

$options=$_product->getResource()->getAttribute('some_attribute')->getFrontend()->getValue($_product)

$options=$_product->getSomeAttribute()

How different are the above methods and which is the proper way to retrieve attribute value?

Upvotes: 2

Views: 4031

Answers (2)

Kalpesh
Kalpesh

Reputation: 5695

echo $_product->getSomeAttribute()

would get the value with attribute as a text value or textarea value, etc..

echo $_product->getAttributeText('some_attribute')

would get an array of all options in a Drop Down or Multiple Select attribute type.

$attributes = $_product->getAttributes();
$someAttr = $attributes['some_attribute']->getFrontend()->getValue($_product);

would get you the value of any type of attribute, even the value from Drop Down or Multiple Select attribute types.

Upvotes: 4

Guerra
Guerra

Reputation: 2790

With getAttributetext you'll get the attribute directly on array of attributes. When you are using get methods, you'll geting the product by the "right" way, becouse get and set can gage the date respecting the magento rules. And using the getFrontend you'll geting this directly from database.

There is no right way to do this, but there is low cost solution, i think using get method and getAttributeText is the best way.

Upvotes: 0

Related Questions