下面是如何使用Magento的类来做到这一点:
// Get the model of the attribute in question
/* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'color');
// Load the option collection for that attribute adding the storeFilter()
/* @var $collection Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection */
$collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($attribute->getId())
->setStoreFilter();
// Load the product so we can get the correct option from the collection
$product = Mage::getModel('catalog/product')->load(39);
$productOption = $collection->getItemById($product->getColor());
printf("Default: %s, Store: %s\n", $productOption->getDefaultValue(), $productOption->getValue());
根据需要调整。
感谢Vinai,优雅和有据可查。我想避免对产品进行全面加载,所以我想我会将该属性添加到我的config.xml中的默认集合属性。此外,是否缓存了属性选项的集合? –
根据您需要的数据(产品列表块偶然?)设置属性的used_in_product_listing属性就足够了,那么您就不需要将它添加到config.xml中。 – Vinai
默认情况下,选项集合数据未缓存。你可以使用集合缓存来快速实现它http://tweetorials.tumblr.com/post/10203426431/magento-block-caching – Vinai