Magento 1 uses product attributes to store additional information about products. While Magento provides convenient methods such as getData() and getAttributeText(), choosing the wrong method or trying to access an attribute that does not exist—can result in unexpected errors.
In this guide, we will explain how to get a custom attribute value in Magento 1, how to retrieve the label of a dropdown attribute, why getAttributeText() can cause a fatal error, and how to safely check whether a custom product attribute exists before retrieving its value.
What Is a Custom Product Attribute in Magento?
A custom product attribute is an additional field that you create to store product-specific information that is not covered by Magento's standard attributes.
For example, an online store might create attributes such as:
- Brand
- Material
- Product Size
- Warranty Period
- Manufacturer
- Compatibility
- Product Type
- Custom Specification
Each Magento product attribute has an attribute code. The attribute code is what you use in PHP when retrieving the attribute from a product object.
For example, if your custom attribute code is custom_attribute_code, you can retrieve its value from the product object using Magento's product methods.
How to Get a Custom Attribute Value in Magento 1
If you already have a loaded Magento product object, you can use getData() to retrieve the raw value of an attribute.
$attributeValue = $_product->getData('custom_attribute_code');echo $attributeValue;
This approach is useful when you need the stored value of the attribute. For example, a text attribute can be retrieved directly with:
echo $_product->getData('manufacturer');
However, the result depends on the attribute type. A dropdown or multiselect attribute may store an option ID rather than the human-readable label.
getData() vs getAttributeText() in Magento
One of the most common Magento attribute-related questions is whether to use getData() or getAttributeText().
Using getData()
Use getData() when you need the underlying value stored for the product attribute.
$value = $_product->getData('custom_attribute_code');echo $value;
Using getAttributeText()
For attributes such as dropdowns and other source-based attributes, getAttributeText() can return the readable option label instead of the stored option value.
echo $_product->getAttributeText('custom_attribute_code');
For example, if a product has a dropdown attribute called brand, the stored value may be an option ID while getAttributeText() returns a label such as Samsung.
Magento's getAttributeText() implementation obtains the attribute and then accesses its source to retrieve the option text. That is why calling it with an attribute code that Magento cannot resolve can produce a fatal error.
Why Can getAttributeText() Cause a Fatal Error?
Consider this code:
echo $_product->getAttributeText('custom_attribute_code');
If the attribute exists and is correctly configured, this can work as expected.
The problem occurs when the supplied attribute code does not exist for the product entity. Magento attempts to access the attribute's source model. If the attribute object is not available, the following type of error can occur:
Fatal error: Call to a member function getSource() on a non-object
This is particularly important on older Magento 1 installations where different stores, modules, or customizations may have different product attributes.
Instead of assuming that an attribute always exists, you can first retrieve the attribute object and check whether it was found.
Safe Way to Get a Custom Attribute Value in Magento 1
A safer approach is to retrieve the attribute through the product resource model before attempting to get its frontend value.
$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute) {
echo $attribute->getFrontend()->getValue($_product);
}
Here is what the code does:
- Gets the product's resource model.
- Looks for the specified attribute code.
- Checks whether the attribute exists.
- Retrieves the frontend value only when the attribute is available.
This prevents your code from blindly calling methods on an attribute object that does not exist.
Complete Example
Suppose your Magento product has a custom attribute with the code product_material. You can safely retrieve its frontend value like this:
<?php
$attributeCode = 'product_material';
$attribute = $_product->getResource()->getAttribute($attributeCode);
if ($attribute) {
$attributeValue = $attribute->getFrontend()->getValue($_product);
if ($attributeValue) {
echo $attributeValue;
}
}
?>
This is useful when working with older Magento stores where custom attributes may have been added by different extensions or custom modules.
How to Get a Dropdown Attribute Label in Magento 1
If your custom attribute is a dropdown, getData() may return the option ID rather than the option label.
For example:
$value = $_product->getData('brand');echo $value;
The returned value may be an internal option ID.
If you need the readable label, you can use:
echo $_product->getAttributeText('brand');
However, when the attribute may not exist, use the safer attribute check:
<?php
$attributeCode = 'brand';
$attribute = $_product->getResource()->getAttribute($attributeCode);
if ($attribute) {
echo $attribute->getFrontend()->getValue($_product);
}
?>
The frontend value method is also useful when you want Magento to provide the value in its frontend representation rather than simply reading the raw stored data.
How to Handle a Missing Attribute
A good Magento implementation should not assume that every product has every custom attribute available.
For example, if your code is used across multiple Magento stores, one store may have a custom attribute while another does not. Checking the attribute object first makes the code more defensive.
<?php
$attributeCode = 'custom_attribute_code';
$attribute = $_product->getResource()->getAttribute($attributeCode);
if (!$attribute) {
return;
}
$value = $attribute->getFrontend()->getValue($_product);
if ($value) {
echo $value;
}
?>
This pattern is especially helpful in custom Magento themes, templates, modules, and extensions where missing configuration should not bring down the frontend.
What If You Only Need the Raw Attribute Value?
If you already know that the attribute exists and you need its stored value, getData() is often the simpler option:
$value = $_product->getData('custom_attribute_code');if ($value !== null) { echo $value;}
This is different from retrieving a frontend label. For example, a select attribute can contain an option ID internally, while its frontend representation contains the corresponding label.
Therefore, choose the method based on what your application needs:
| Requirement | Magento 1 Method |
|---|---|
| Get raw product attribute data | getData() |
| Get a dropdown option label | getAttributeText() |
| Safely check whether an attribute exists | getResource()->getAttribute() |
| Get the frontend-rendered attribute value | getFrontend()->getValue($_product) |
Common Mistakes When Retrieving Magento Custom Attributes
1. Using the wrong attribute code
The attribute code must match the actual Magento attribute code. The frontend label and attribute code are not necessarily the same.
For example, an attribute may have the label Product Brand while its code is product_brand.
2. Assuming every attribute exists
Custom Magento installations often contain attributes added by themes, extensions, or custom modules. If an attribute is missing, directly calling getAttributeText() can cause an error.
3. Confusing raw values with labels
For select-based attributes, getData() may return the stored option value while getAttributeText() is intended to return the option text.
4. Loading products unnecessarily
If you already have a loaded product object, avoid repeatedly loading the same product simply to retrieve an attribute. Use the existing product object and ensure the required attributes are available in the product collection.
5. Forgetting product collection configuration
When retrieving products from a collection, custom attributes may need to be included in the collection depending on how the collection is built. Magento developers commonly encounter cases where an attribute is available on a fully loaded product but not on a collection item. One approach discussed in Magento community resources is using addAttributeToSelect() or ensuring the attribute is configured for product listings where appropriate.
Best Practice for Magento 1 Custom Attributes
When working with Magento 1 custom product attributes, use the simplest method that matches your requirement:
- Use
getData()for a raw attribute value. - Use
getAttributeText()when you need a readable option label. - Check the attribute object before using frontend or source methods when the attribute may not exist.
- Use the correct attribute code instead of the frontend display label.
- Avoid unnecessarily reloading product objects.
- Test custom attribute code across all relevant store views.
- Be especially careful with legacy Magento 1 extensions that modify product attributes.
Recommended Safe Code
If you need a reusable pattern for safely retrieving a Magento 1 custom attribute, this is a good starting point:
<?php
$attributeCode = 'custom_attribute_code';
$attribute = $_product->getResource()->getAttribute($attributeCode);
if ($attribute) {
$value = $attribute->getFrontend()->getValue($_product);
if ($value !== false && $value !== null && $value !== '') {
echo $value;
}
}
?>
The important part is the existence check before calling getFrontend(). This makes the code safer when the attribute configuration is not guaranteed.
Frequently Asked Questions
How do I get a custom attribute value in Magento 1?
If you need the raw value, use getData():
echo $_product->getData('custom_attribute_code');
If you need the frontend value, retrieve the attribute and use its frontend model.
How do I get a custom dropdown attribute value in Magento 1?
For a dropdown attribute, you can use:
echo $_product->getAttributeText('custom_attribute_code');
If the attribute might not exist, check the attribute object first to avoid errors.
Why does Magento 1 getAttributeText() return a fatal error?
getAttributeText() expects Magento to resolve the specified attribute and access its source. If the attribute cannot be resolved, older Magento implementations can produce a fatal error involving getSource().
What is the difference between getData() and getAttributeText()?
getData() retrieves the product's stored attribute data. getAttributeText() is useful for source-based attributes when you want the human-readable option text rather than the stored option value.
How can I check whether a Magento 1 attribute exists?
You can retrieve the attribute through the product resource model and check the result:
$attribute = $_product->getResource()->getAttribute('custom_attribute_code');if ($attribute) { // Attribute exists}
Can I get a Magento custom attribute without using getAttributeText()?
Yes. For a raw value, use getData(). For a frontend value, you can use the attribute's frontend model:
$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute) {
echo $attribute->getFrontend()->getValue($_product);
}
Does this code work with Magento 2?
This article is specifically written for Magento 1. Magento 2 uses a different architecture and product attribute API, so Magento 2 code should be implemented using Magento 2's service contracts and product APIs rather than copying Magento 1 code directly.
Need Help With a Legacy Magento Store?
Custom attributes are only one part of maintaining a Magento store. Legacy Magento websites can also have outdated extensions, custom PHP code, performance bottlenecks, frontend issues, integration problems, and technical SEO problems.
Scriptbaker provides software engineering and web development services for businesses that need help maintaining, improving, or modernizing custom web platforms.
Our team can help with custom PHP development, Magento development and maintenance, website modernization, API integrations, SaaS development, automation, hosting, and SEO improvements.
Conclusion
Getting a custom product attribute in Magento 1 is straightforward when you use the appropriate method for the attribute type. getData() is useful for retrieving raw values, while getAttributeText() can return readable option labels for source-based attributes.
The important consideration is that custom attributes should not always be assumed to exist. If an attribute may be missing, retrieve it through the product resource model first and check that the attribute object exists before accessing its frontend or source methods.
Using this defensive approach can make legacy Magento 1 customizations more reliable and help prevent avoidable frontend fatal errors.
Last updated: