Skip to content

Instantly share code, notes, and snippets.

@Vinai
Created June 20, 2014 08:34
Show Gist options
  • Save Vinai/71a24b8a70a7fc95907b to your computer and use it in GitHub Desktop.
Save Vinai/71a24b8a70a7fc95907b to your computer and use it in GitHub Desktop.
Loading individual attribute values on catalog/product instances.
<?php
require('htdocs/app/Mage.php');
umask(0);
Mage::setIsDeveloperMode(true);
Mage::app();
$attributeCode = 'description'; // any attribute code
// load model without only static attribute values
/** @var Mage_Catalog_Model_Product $product */
$product = Mage::getModel('catalog/product')->getCollection()->getFirstItem();
// proof the attribute is not set
var_dump($product->getData($attributeCode));
// load the individual attribute value without reloading the whole model
/** @var Mage_Catalog_Model_Resource_Product $resource */
$resource = $product->getResource();
$attribute = $resource->getAttribute($attributeCode);
$result = $resource->getAttributeRawValue($product->getId(), $attribute, $product->getStoreId());
$product->setData($attributeCode, $result);
$attribute->getBackend()->afterLoad($product);
// proof the attribute is now set on the model
var_dump($product->getData($attributeCode));
echo "special case media_gallery only needs the backend model afterLoad call\n";
$product = Mage::getModel('catalog/product')->getCollection()->getFirstItem();
$attributeCode = 'media_gallery';
$attribute = $product->getResource()->getAttribute($attributeCode);
$attribute->getBackend()->afterLoad($product);
var_dump($product->getData($attributeCode));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment