简体   繁体   中英

How to display product Attribute Group Name on Magento Product page attribute tab?

i did some coding to group the product attributes on frontend and show their groups names above theme like this:

  • attgroup 1
    • attribute 1
    • attribute 2
    • ...
  • attgroup 2
    • attribute 3
    • attribute 4
    • ...

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/MY_TEMPLATE/default/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 did this but nothing is changed on product attribute tap on frontend product page.

im using magento 1.9.1 ce and custom template

First try to simple way

foreach($product->getAttributes() as $att){    
    $group_id   = $att->getData('attribute_group_id');
$group = Mage::getModel('eav/entity_attribute_group')->load($group_id);                 var_dump($group);
    } 

or please try to below code....

Get attribute set ID programmatically..

$sDefaultAttributeSetId = Mage::getSingleton('eav/config')
    ->getEntityType(Mage_Catalog_Model_Product::ENTITY)
    ->getDefaultAttributeSetId();

Get group name programmatically..

$attributeSetId = 10;
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetId)
->setSortOrder()
->load();

$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName          = $group->getAttributeGroupName();
$groupId            = $group->getAttributeGroupId();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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