2015-03-31 44 views
0

我有一個自定義屬性,我需要在購物車中顯示。該屬性是一個下拉像這樣:Magento - 在config.xml中檢索購物車中的自定義屬性LABEL

Attribute Code : section 
Catalog Input Type for Store Owner : Dropdown 
Options: 
    ID/VALUE = 67 LABEL = warehouse 
    ID/VALUE = 69 LABEL = showroom 
    ID/VALUE = 70 LABEL = stockroom 

要顯示此我有一個自定義模塊與我的config.xml中像這樣:

<global> 
<sales> 
    <quote> 
     <item> 
      <product_attributes> 
       <section/> 
      </product_attributes> 
     </item> 
    </quote> 
</sales> 

而且在車我可以打電話:

$_item->getProduct()->getSection(); 

這將返回該屬性的ID /值(即67),但我希望能夠獲得標籤(即倉庫)

我知道我可以從ID獲得一個標籤,像這樣:

$_product = Mage::getModel('catalog/product'); 
$attr = $_product->getResource()->getAttribute("section"); 
if ($attr->usesSource()) { 
    $_label = $attr->getSource()->getOptionText("67"); 
} 

但我想通過我的模塊,以獲取標籤,所以我可以剪切的額外databae查詢,無需加載的產品型號再次。我的購物車每個訂單可以有20件以上的商品,所以使用這種最後一種方法可以稍微減慢它的速度。

回答

0

您可以使用$_item->getProduct()->getAttributeText('section')來代替,這更清晰,但我沒有看到性能。我認爲,如果您將產品加載到包含該屬性的集合中,那麼它將不需要針對每個產品/屬性分別調用數據庫。

+0

Spot on。性能似乎也沒有受到影響。謝謝 – 2015-06-01 14:57:08

+0

如果您想獲得StoreLabel而不是AttributeText,您會做什麼?當我的代碼碰到頁面時,我似乎無法獲取StoreLabel。 '$ _item-> getProduct->的getAttribute( 'attribute_code') - > getStoreLabel();' – Xander 2015-07-23 15:49:50

0

我假設你有權訪問Mage_Sales_Model_Quote_Item對象。如果是這樣,你可以嘗試下面的代碼片段:

$_resource = $_item->getProduct()->getResource(); 
$optionValue = $_resource 
    ->getAttributeRawValue($_item->getProduct()->getId(), 'section', Mage::app()->getStore()->getId()); 
$optionLabel = $_resource 
    ->getAttribute('section') 
    ->getSource() 
    ->getOptionText($optionValue); 
相關問題