2011-11-11 25 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