自定义产品属性后,通常可以设置其是否在产品查看页中显示,可显属性将会出现在additional information 中。但是这边我需要在分类列表页中调用产品特定的属性。
在列表页循环获取对应产品的id之后,直接输出产品的所有属性内容会太多,getData等取出的信息又过于简洁。总之用起来不是很爽,于是根据 app/code/core/Mage/Catalog/Block/Product/View/Attributes.php中的 getAdditionalData 方法进行了一些简单的修改, 新方法还是放在getAdditionalData 同一文件中,因为有些继承的方法要用。 代码如下
public function getMeSetData($t_product) { $data = array(); $product = $t_product; $attributes = $product->getAttributes(); foreach ($attributes as $attribute) { $value = $attribute->getFrontend()->getValue($product); if (!$product->hasData($attribute->getAttributeCode())) { $value = Mage::helper('catalog')->__('N/A'); } elseif ((string)$value == '') { $value = Mage::helper('catalog')->__('No'); } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) { $value = Mage::app()->getStore()->convertPrice($value, true); } if (is_string($value) && strlen($value)) { $data[$attribute->getAttributeCode()] = array( 'label' => $attribute->getStoreLabel(), 'value' => $value, 'code' => $attribute->getAttributeCode() ); } } return $data; }
可以看到这边的参数是一个产品对象,但是测试的时候发现,列表页直接使用的产品对象信息并不够全,也无法使用这个方法,所以在获取产品id后重新加载了一下对应的产品,调用方法如下
$p_id = $_product->getId(); $_product = Mage::getModel('catalog/product')->load($p_id); $p_attrs = Mage::getBlockSingleton('catalog/product_view_attributes')->getMeSetData($_product); print_r($p_attrs);
这样取出的产品属性信息就比较全了,也不会过于臃肿。具体用途 各取所需吧
来源:http://vsfor.com/archives/323
最全面的:
$attributes = Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection(); // Localize attribute label (if you need it) $attributes->addStoreLabel(Mage::app()->getStore()->getId()); // Loop over all attributes foreach ($attributes as $attr) { /* @var $attr Mage_Eav_Model_Entity_Attribute */ // get the store label value $label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel(); echo "Attribute: {$label}\n<br>"; // If it is an attribute with predefined values if ($attr->usesSource()) { // Get all option values ans labels $options = $attr->getSource()->getAllOptions(); // Output all option labels and values foreach ($options as $option) { echo " {$option['label']} =========> (Value {$option['value']})\n<br>"; } } else { // Just for clarification of the debug code echo " No select or multiselect attribute\n<br>"; } }
来源: http://stackoverflow.com/questions/9275826/how-to-get-all-active-attributes-of-products-in-magento