简体   繁体   English

Magento按组加载产品属性?

[英]Magento load product attributes by group?

Is there way to get into a foreach loop and loop through all the product attributes in an attribute group in Magento? 有没有办法进入foreach循环并遍历Magento属性组中的所有产品属性? Please check screenshot. 请检查屏幕截图。 For example, I just wanna loop through the values in the attributes of Design group. 例如,我只想遍历“设计”组的属性中的值。

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

Thanks 谢谢

No there is no built in function that will return you with array of product attributes organized by group. 没有,没有内置函数可以按组将产品属性数组返回给您。 This is one of the missing functionalities. 这是缺少的功能之一。 You need to create it yourself in a helper or a block class. 您需要在帮助程序或块类中自己创建它。

[EDIT] [编辑]

It looks like I was wrong. 看来我错了。 There is a method that will return you attributes from given group. 有一种方法可以返回给定组的属性。 It is in Mage_Catalog_Model_Product::getAttributes() . 它在Mage_Catalog_Model_Product::getAttributes() The first argument there is group id. 第一个参数是组ID。 So you can do something like this: 因此,您可以执行以下操作:

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

This make it easier to get attributes from the group but I think that it still require own helper or, better, own block as that piece of code in phtml file is not the best practice. 这使得从组中获取属性更加容易,但是我认为它仍然需要自己的助手,或者更好的是,自己的块,因为phtml文件中的那段代码不是最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM