【发布时间】:2010-01-19 05:43:39
【问题描述】:
我正在尝试在 Magento 产品视图模板中获取属性集名称。我可以通过$_product->getAttributeText('attribute')获取属性值,但是如何获取属性集名称呢?
我只想显示属于某个属性集的属性。
【问题讨论】:
标签: magento attributes magento-1.9
我正在尝试在 Magento 产品视图模板中获取属性集名称。我可以通过$_product->getAttributeText('attribute')获取属性值,但是如何获取属性集名称呢?
我只想显示属于某个属性集的属性。
【问题讨论】:
标签: magento attributes magento-1.9
只要你有一个产品对象,你就可以像这样访问它的属性集:
$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');
}
希望有帮助!
【讨论】:
$attributeSet 更正为#attributeSetName。看起来很合理,所以我同意了。但是,我不懂这种语言,所以请检查它是否正确。
$attributeSetName = $attributeSetModel->getAttributeSetName();,末尾没有')'
== 更严格。在这种特定情况下,它们都很好。
为了更性感,您可以将其缩短为:
$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
【讨论】:
试试下面的代码:
$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 中查找有关属性集的更多信息。
谢谢
【讨论】:
Joe 的回答需要进行一些修改才能正常工作。
首先应该是 $_product 而不是 $product,其次最后一行有一个错误的 ')'。
下面的代码应该是正确的:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
【讨论】:
如果用户决定稍后更改该文本,则与文本值进行比较可能会出现问题——这在 Magento 中对于属性集很容易做到。另一种选择是使用永远不会改变的底层 id。
你可以通过在数据库中查找attribute_set_id列的值来得到这个
select * from eav_attribute_set;
这个数字也在管理员的编辑链接中,下面用粗体显示
http://.../index.php/admin/catalog_product_set/edit/id/10/key/6fe89fe2221cf2f80b82ac2ae457909ce04c92c51716b3e474ecad672a2ae2f3/
然后,您的代码将简单地使用产品的该属性。基于上面链接中 10 的 id,这只是
if (10 == $_product->getAttributeSetId()) {
//Do work
}
【讨论】: