Magento- Get a custom attribute of a product by sku

Magento- Get a custom attribute of a product by sku

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento- Get a custom attribute of a product by sku

Displaying Custom Product Attributes by SKU in Magento

When working with custom attributes in Magento, you might face challenges when trying to retrieve their values using the product’s SKU. While loading a product by its name works perfectly, loading it by its SKU and accessing custom attributes can be tricky. Let me guide you through this issue.

Understanding the Problem

You’re attempting to display the value of a custom attribute (technologies) using the product’s SKU. However, when you use getTechnologies(), you receive an integer instead of the actual string value stored in your custom attribute. This is because Magento’s core model doesn’t handle this situation gracefully.

The Core Issue

The problem lies in how Magento handles custom attributes for loaded products. When you load a product by its SKU, Magento uses the loadByAttribute method which returns an object of type Mage_Catalog_Model_Resource_Eav_Mysql4_Product. This object doesn’t have direct access to the attribute values stored in EAV (Entity-Attribute-Value) format.

A Practical Solution

To resolve this issue, you need to use Magento’s EAV model to fetch the custom data directly from the database. Here’s how you can do it:

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $bulb_merge);
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');

$attributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'technologies')->getId();
$sql = "SELECT `value` FROM catalog_product_entity_text WHERE entity_id = ? AND attribute_id = ?";
$bind = array($product->getId(), $attributeId);
$value = $readConnection->fetchOne($sql, $bind);

echo $value;

What’s Happening Here?

  1. We use the loadByAttribute method to load the product by SKU.

  2. We get a database connection object from Magento’s core resource model.

  3. We find the ID of your custom attribute using Magento’s EAV configuration model.

  4. We construct an SQL query to fetch the value of your custom attribute for the loaded product.

  5. We execute this query with the appropriate bindings and store the result in the $value variable.

  6. Finally, we echo the fetched value which is the string representation of your custom attribute.

Conclusion

When dealing with custom attributes in Magento, especially when trying to fetch them using SKUs, you often need to work around the core models’ limitations. By directly querying the database using EAV, you can retrieve the desired values accurately.

Remember that this method assumes you have the necessary read access permissions on your Magento installation and the attribute is of type ‘text’. If it’s a different type (like int or datetime), you’ll need to adjust the SQL query accordingly.

For more insights into working with custom attributes in Magento, I recommend exploring our Magento Customization Guide at Laravel Company. It provides detailed step-by-step instructions for creating and managing custom attributes effectively.

Happy coding! Let me know if you have any other questions about Magento development.

Catalog Database Configuration Errors

Our network