31

我正在尝试在 Magento 产品视图模板中获取属性集名称。我可以通过 获取属性值$_product->getAttributeText('attribute'),但是如何获取属性集名称?

我只想显示属于某个属性集的属性。

4

5 回答 5

73

只要你有一个产品对象,你就可以像这样访问它的属性集:

$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');
}

希望有帮助!

于 2010-01-19T16:56:39.617 回答
25

为了更性感,您可以将其缩短为:

$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
于 2011-06-29T17:36:07.643 回答
14

试试下面的代码:

$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;

在以下文章中查找有关属性集的更多信息。

谢谢

于 2011-11-18T13:34:21.703 回答
1

乔的答案需要进行一些更改才能使其正常工作。

首先它应该是 $_product 而不是 $product,其次在最后一行有一个错误的 ')'。

以下代码应该是正确的:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
于 2011-03-11T15:44:28.547 回答
0

如果用户决定稍后更改该文本,则与文本值进行比较可能会出现问题——这在 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
}
于 2012-02-25T10:28:55.537 回答