Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Get a custom attribute value without loading the entire product

Tags:

At the moment I use this to get a custom attribute value:

$_item = $this->getProduct()->getId(); $_product = $_product = Mage::getModel('catalog/product')->load($_item);   $optionvalue = $_product->getCustomAttributeValue(); echo $optionvalue; 

I wonder is there an easier way to get this custom value without loading the entire product?

like image 846
RaduS Avatar asked Oct 15 '13 11:10

RaduS


2 Answers

This depends on which version of Magento you're running. Different versions have different offerings. If you're running Community edition 1.6+, the Catalog module has a very nice method just for you!

Try the following:

$_item = $this->getProduct()->getId(); $_resource = $this->getProduct()->getResource(); $optionValue = $_resource->getAttributeRawValue($_item, 'custom_attribute_value', Mage::app()->getStore()); echo $optionvalue; 

If you're interested, you could dive down into Mage_Catalog_Model_Resource_Abstract to see what this little guy is doing. It's essentially just a query (admittedly a rather complex one, as EAV tends to be) to retrieve the one attribute you asked for (or the multiple attributes you asked for, since you can pass an array as well).

like image 105
JMTyler Avatar answered Oct 02 '22 01:10

JMTyler


I just want to improve @JMTyler answer, because I found out you don't need a real product model to get the getResource()

So you can just do it having a product id and using a singleton ( this would be better in case you are doing it in a loop so you don't actually create the model many time )

$product_id = 10075; $_resource = Mage::getSingleton('catalog/product')->getResource(); $optionValue = $_resource->getAttributeRawValue($product_id,  [ATTRIBUTE_ID/ATTRIBUTE_CODE], Mage::app()->getStore()); echo $optionValue; 
like image 38
WonderLand Avatar answered Oct 02 '22 01:10

WonderLand