2011-11-11 26 views
3

我面临访问在多网站/多商店网站上的另一个作用域中指定的属性值的要求。特别是,当Store的标签已设置时,我们需要在前端显示属性的Admin(默认)标签。如何从不同范围访问Magento模型数据

enter image description here

因此,代码应该呈现在网页的一部分管理员列在页面其他部分的十六进制值,并从英语(美国)的文字说明。我怎么做?相反,我已经看到在Store视图中设置了值的实例,但默认情况下为null,即使设置了Store,代码也会返回null。有人可以解释这是如何工作的?

回答

6

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

根据需要调整。

+0

感谢Vinai,优雅和有据可查。我想避免对产品进行全面加载,所以我想我会将该属性添加到我的config.xml中的默认集合属性。此外,是否缓存了属性选项的集合? –

+1

根据您需要的数据(产品列表块偶然?)设置属性的used_in_product_listing属性就足够了,那么您就不需要将它添加到config.xml中。 – Vinai

+0

默认情况下,选项集合数据未缓存。你可以使用集合缓存来快速实现它http://tweetorials.tumblr.com/post/10203426431/magento-block-caching – Vinai