22
votes

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?

5
you can configure the attribute to be visible where you need it, in what kind of pages do you want to access it?OSdave
app/code/core/Mage/Catalog/Block/Product/View/Options/Type/Select.phpRaduS
about line 131 where the <li> startsRaduS

5 Answers

40
votes

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).

32
votes

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;
13
votes

Other simple way is add this "custom_attribute" to the list of attributes to get by default when you check product data from a quote item.

If you already created a custom module in config.xml add this.

<global>
    ...
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <custom_attribute />
                </product_attributes>
            </item>
        </quote>
    </sales>
    ...
</global>
6
votes

This may not provide much, if any performance benefit; however, it will fetch only the attribute value and no other columns:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('entity_id', $_item);
$collection->getSelect()
    ->reset('columns')
    ->columns(array('[custom attribute code]'));
$value = $collection->getFirstItem()
    ->getData('[custom attribute code]');

You could also use direct SQL, though I wouldn't recommend it unless performance were a real issue:

$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = <<<SQL
    SELECT `value`
    FROM catalog_product_entity_[backend type]
    WHERE entity_id = {$_item}
      AND attribute_id = [attribute ID]
SQL;
$row = $connection->fetchRow($sql);
$value = $row['value'];
1
votes

If the desired product value is based on a dropdown/source-attribute, you can do the following:

    $resource = $magentoProductInstance->getResource();
    $value = $resource->getAttributeRawValue($productId, $attributeCode, Mage::app()->getStore());
    //if the value is an option id continue with this
    $optionId = $value;
    $value = $resource->getAttribute($attributeCode);
        return $productAttribute
            ->setStoreId(Mage::app()->getStore()->getId())
            ->getSource()
            ->getOptionText($optionId);