Magento按组加载产品属性?

时间:2014-05-29 20:11:44

标签: php magento model-view-controller orm

有没有办法进入foreach循环并遍历Magento中属性组中的所有产品属性?请检查截图。例如,我只想循环设计组属性中的值。

https://s3.amazonaws.com/uploads.hipchat.com/62230/429611/8EOCq5jqCKVUkJR/Screen%20Shot%202014-05-29%20at%204.10.10%20PM.png

由于

1 个答案:

答案 0 :(得分:2)

没有没有内置函数会返回由group组织的产品属性数组。这是缺少的功能之一。您需要在帮助程序或块类中自己创建它。

[编辑]

看起来我错了。有一种方法可以返回给定组的属性。它位于Mage_Catalog_Model_Product::getAttributes()。第一个参数是组ID。所以你可以这样做:

$groupId = Mage::getModel('eav/entity_attribute_group')->getCollection()
    ->addFieldToFilter('attribute_set_id', array('eq' => $_product->getAttributeSetId()))
    ->addFieldToFilter('attribute_group_name', array('eq' => 'General'))
    ->getFirstItem()->getId();

foreach($_product->getAttributes($groupId) as $attribute) {
  //this will return text value even for multiselect
  $attributeVal = $attribute->getFrontend()->getValue($_product);

  //or but you need to handle retriving select and multiselect labels
  $attributeVal = $_product->getData($attribute->getCode());
}

这样可以更容易地从组中获取属性,但我认为它仍然需要自己的帮助器,或者更好的是自己的块,因为phtml文件中的那段代码不是最佳实践。

相关问题