下面是如何使用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