2014-09-06 42 views
0

產品(電影)具有屬性「流派」(多選),例如「喜劇,動作,幻想」。在list.phtml文件中,我附加了一些php代碼來顯示每部電影的所有流派。但我只獲得第一個OptionId。Magento - 如何通過選項值獲取屬性搜索的選項ID?

<?php 
    $AttrName = explode(",",$_product->getResource()->getAttribute('genre')->getFrontend()->getValue($_product)); 
    $attr = $_product->getResource()->getAttribute('genre'); 
    foreach($AttrName as $optvl): 
    echo $attr->getSource()->getOptionId($optvl); 
    endforeach; 
?> 

這個循環有什麼問題?

回答

1

請參考以下片斷

$productModel = Mage::getModel('catalog/product')->load($id_of_product); 
$attr = $productModel->getResource()->getAttribute("genre"); 
$exploded = explode(',',$productModel->getGenre()); 
foreach ($exploded as $key => $value) { 
    if ($attr->usesSource()) { 
     $genre_title[] = $attr->getSource()->getOptionText($value); 
    }  
} 

    print_r($genre_title); 
+0

謝謝你的精神食糧。 – Juri 2014-09-06 21:11:15

相關問題