How to display Attribute Group Name on Product page?
I created Attribute Sets with more Grouped attributes inside. In admin, the custom attributes are displayed in groups (tabs) like I created them. But, on the product page, all the attributes are listed together, without displaying the Attribute Group Name before listing the Attributes in that Group.
How 开发者_StackOverflow中文版can I display also the Attribute Group names, not only the attributes? If you could show me on the default template, I will do it accordingly in my custom template.
Thank you!
Ok, I found an answer and I hope it will be useful to others in search for the same thing. First, I'm using Magento 1.5.0. Second, I found the answer in German here, with an extension already created, but the installation failed. So, I added /app/code/local/Mage/Catalog/Block/Product/View/Attributesgroups.php with the following code:
<?php
class Mage_Catalog_Block_Product_View_Attributesgroups extends Mage_Core_Block_Template
{
protected $_product = null;
function getProduct()
{
if (!$this->_product) {
$this->_product = Mage::registry('product');
}
return $this->_product;
}
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
// TODO this is temporary skipping eco taxes
if (is_string($value)) {
if (strlen($value) && $product->hasData($attribute->getAttributeCode())) {
if ($attribute->getFrontendInput() == 'price') {
$value = Mage::app()->getStore()->convertPrice($value,true);
} elseif (!$attribute->getIsHtmlAllowedOnFront()) {
$value = $this->htmlEscape($value);
}
$group = 0;
if( $tmp = $attribute->getData('attribute_group_id') ) {
$group = $tmp;
}
$data[$group]['items'][ $attribute->getAttributeCode()] = array(
'label' => $attribute->getFrontend()->getLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
$data[$group]['attrid'] = $attribute->getId();
}
}
}
}
// Noch Titel lesen
foreach( $data AS $groupId => &$group ) {
$groupModel = Mage::getModel('eav/entity_attribute_group')->load( $groupId );
$group['title'] = $groupModel->getAttributeGroupName();
}
return $data;
}
}
Then, I created the /app/design/frontend/default/YOUR_TEMPLATE/template/catalog/product/view/attributesgroups.phtml file with the following content:
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additionalgroup = $this->getAdditionalData()): ?>
<div class="box-collateral box-additional">
<h2><?php echo $this->__('Additional Information') ?></h2>
<?php $i=0; foreach ($_additionalgroup as $_additional): $i++; ?>
<h3><?php echo $this->__( $_additional['title'] )?></h3>
<table class="data-table" id="product-attribute-specs-table-<?php echo $i?>">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional['items'] as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
<?php endforeach; ?>
</div>
<?php endif;?>
Last step was to modify /app/design/frontend/default/YOUR_TEMPLATE/layout/catalog.xml in line 223, and replaced
<block type="catalog/product_view_attributes" name="product.attributes" as="additional" template="catalog/product/view/attributes.phtml">
with
<block type="catalog/product_view_attributesgroups" name="product.attributes" as="additional" template="catalog/product/view/attributesgroups.phtml">
I repeat, this answer does NOT belong to me, I just translated what I found. Many thanks to the beautiful people who ended my three days of search with a clean and simple answer: WebGuys.DE Also, thanks to @rpSetzer who cared to help!
Iterate through all the attributes and create an array of groups. In each group you put the attributes that belong to it. Then it's simple to display them the way you wanted.
Here is an implementation that is very close to what you need.
Thanks for translation : additionnal information for those who may need :
For 1st step, if the folder /app/code/local/Mage/Catalog/Block/Product/View/ doesn't exist, create it ! with correct persmissions and place the file Attributesgroups.php there
If you have different store views (for different languages), and you want to use correct translation for each attribute label, here is want you need to do : In the file Attributesgroups.php Replace
'label' => $attribute->getFrontend()->getLabel(),
with'label' => $attribute->getStoreLabel(),
精彩评论