Get custom attribute value in Magento

Standard

Simplest way:

echo $_product->getAttributeText('custom_attribute_code');

Problem:

You will get a fetal error if the attribute is not already registered.

Fatal error: Call to a member function getSource() on a non-object in D:\..\app\code\core\Mage\Catalog\Model\Product.php on line 1389

Solution:

$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute)
{
	echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}

This code will first check if the attribute code exists and gets its value if its there.

Advertisements

7 thoughts on “Get custom attribute value in Magento

  1. Muhammad Umar

    showing error

    Fatal error: Call to a member function getResource() on a non-object in F:\wamp\www\hopscotch\app\design\frontend\hopscotch\default\template\page\1column.phtml on line 57

    Can you please tell me why? 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: