2010-01-19 132 views
29

我想在Magento產品視圖模板中獲取屬性集名稱。我可以通過$_product->getAttributeText('attribute')獲取屬性值,但是如何獲取屬性集名稱?如何獲取屬性集名稱?

我想顯示的屬性,只有當它是屬於某個屬性集。

回答

66

只要你有一個產品對象,你可以訪問它的屬性設置是這樣的:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set"); 
$attributeSetModel->load($product->getAttributeSetId()); 
$attributeSetName = $attributeSetModel->getAttributeSetName(); 

這會給你設置屬性,然後你就可以比較使用的strcmp的名字:

if(0 == strcmp($attributeSetName, 'My Attribute Set')) { 
    print $product->getAttributeText('attribute'); 
} 

希望有幫助!

+0

匿名用戶提供了一個編輯建議,將'$ attributeSet'更正爲'#attributeSetName'。它看起來很合理,所以我批准了它。但是,我不知道這種語言,所以請檢查它是否正確。 – abcd 2011-06-30 22:39:38

+0

最後一行應該是:'$ attributeSetName = $ attributeSetModel-> getAttributeSetName();'末尾沒有')' – Yeroon 2011-08-05 08:18:31

+0

函數strcmp的奇怪選擇?爲什麼不用===直接比較? – 2014-07-10 20:50:35

1

喬的答案需要一些改變才能使其工作。

首先它應該是$ _product而不是$ product,其次是在最後一行有一個錯誤的')'。

下面的代碼應該是正確的:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set"); 
$attributeSetModel->load($_product->getAttributeSetId()); 
$attributeSetName = $attributeSetModel->getAttributeSetName(); 
24

更多sexyness可以縮短到:

$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName(); 
13

試試下面的代碼:

$entityTypeId = Mage::getModel('eav/entity') 
                ->setType('catalog_product') 
                ->getTypeId(); 
$attributeSetName   = 'Default'; 
$attributeSetId     = Mage::getModel('eav/entity_attribute_set') 
                    ->getCollection() 
                    ->setEntityTypeFilter($entityTypeId) 
                    ->addFieldToFilter('attribute_set_name', $attributeSetName) 
                    ->getFirstItem() 
                    ->getAttributeSetId(); 
echo $attributeSetId; 

查找更多信息屬性在following article中設置。這是很容易在Magento的屬性集做 - 如果用戶決定以後更改文本

感謝

0

相較於文本值可能會有問題。另一種選擇是使用底層ID,而不會改變。

您可以通過查找使用

select * from eav_attribute_set; 

此編號也是在管理員的編輯鏈接是以下粗體

http://.../index.php/admin/catalog_product_set/edit/id/10在數據庫中的attribute_set_id列的價值得到這個 /按鍵/ 6fe89fe2221cf2f80b82ac2ae457909ce04c92c51716b3e474ecad672a2ae2f3/

您的代碼將然後只需使用對產品的性能。根據上面鏈接中的ID爲10,這只是

if (10 == $_product->getAttributeSetId()) { 
    //Do work 
}