2010-01-19 135 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 
}