So you have created some custom attributes for your Magento products and now you want to fetch their values. Magento provides a function getAttributeText that takes attribute code and returns you the attribute value. Problem: [sourcecode]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 To avoid fetal errors and crashes, here is safe way to get custom attribute value. If you get custom attribute values by following below procedure, your site will not crash even when the attribute was not yet registered. This code will first checks if the attribute code exists and gets its value if its there.
echo $_product->getAttributeText('custom_attribute_code');
Above method returns you attribute value only when the attribute was registered, but it will return a fetal error if the attribute was not already registered.
Safe way to get custom attribute value:
$attribute = $_product->getResource()->getAttribute('custom_attribute_code'); if ($attribute) { echo $attribute_value = $attribute ->getFrontend()->getValue($_product); }
Magento
Get custom attribute value in Magento
So you have created some custom attributes for your Magento products and now you want to fetch their values. Magento provides a function getAttributeText that t
· 5 min read · By Tahir Yasin
Related posts
Magento
Get Magento root category id
Learn how to retrieve the root category ID in Magento 1, understand the category hierarchy, avoid inefficient database queries, and use Magento’s built-in methods for cleaner, faster store development.
Magento
Get Magento Website Meta Data and Config Values
Following functions will help you to get Magento meta data like “Default Title”, “Default Description” To get Default Title: [code langu
Magento
Add Home link in Magento top navigation menu
Learn how to easily add a Home link to the Magento top navigation menu using simple template changes and PHP code. By default there is no Home link in Magento top navigation menu but you can easily add it yourself.