我正在尝试在 Magento 产品视图模板中获取属性集名称。我可以通过 获取属性值$_product->getAttributeText('attribute')
,但是如何获取属性集名称?
我只想显示属于某个属性集的属性。
我正在尝试在 Magento 产品视图模板中获取属性集名称。我可以通过 获取属性值$_product->getAttributeText('attribute')
,但是如何获取属性集名称?
我只想显示属于某个属性集的属性。
只要你有一个产品对象,你就可以像这样访问它的属性集:
$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');
}
希望有帮助!
为了更性感,您可以将其缩短为:
$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;
在以下文章中查找有关属性集的更多信息。
谢谢
乔的答案需要进行一些更改才能使其正常工作。
首先它应该是 $_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
}