Magento 1.9 – List Attribute Names and Values for a Group

attribute-setattributesmagento-1.9

On the product listing page, I would like a bullet point list within each product to show attribute name and value (for a particular group – "Specification) e.g.

Specification

  • Fixing Centres: 120.6mm
  • Size: 147.9mm x 87.4mm x 31mm

I have already added these attributes and assigned values to them within Magento. Using the code below (found and tweaked with a bit of searching here), I can also output an array containing the attribute name, however I can't seem to get the 'front end' version of the name and the value:

$setId = $_product->getAttributeSetId(); // Attribute set Id
$groups = Mage::getModel('eav/entity_attribute_group')
    ->getResourceCollection()
    ->setAttributeSetFilter($setId)
    ->setSortOrder()
    ->load();

$attributeCodes = array();
foreach ($groups as $group) {
    if($group->getAttributeGroupName() == 'Specification'){ // set name
    //$groupName          = $group->getAttributeGroupName();
    //$groupId            = $group->getAttributeGroupId();

    $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
        ->setAttributeGroupFilter($group->getId())
        ->addVisibleFilter()
        ->checkConfigurableProducts()
        ->load();
        if ($attributes->getSize() > 0) {
            foreach ($attributes->getItems() as $attribute) {
                /* @var $child Mage_Eav_Model_Entity_Attribute */
                $attributeCodes[] = $attribute->getAttributeCode();  
            }
        } 
    }
}
print_r($attributeCodes);

The above outputs Array ( [0] => mounting_box_depth [1] => fixing_centres [2] => size )

To get the value, I tried

$attributeValue = Mage::getModel('catalog/product')
            ->load($_product->getId())
            ->getAttributeText(attributeCodes[0]);

But this doesn't seem to return anything at all. Any ideas?

As requested, list.phtml in its entirety below:

<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <?php //echo $this->getToolbarHtml() ?>

    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>

        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <div class="productBlock clearfix">

                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                    <div class="productBlock__compareLink plain-link">
                        <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Compare this product') ?></a><a href="<?php echo $_product->getProductUrl() ?>" class="view blue-text">View</a>
                    <!-- .productBlock__compareLink --></div>
                    <?php endif; ?>

                    <div class="productBlock__image">
                        <a href="<?php echo $_product->getProductUrl() ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(230); ?>" width="230" height="230" alt=""></a>
                    <!-- .productBlock__image --></div>

                    <div class="productBlock__description">
                        <h2 class="productBlock__description-name plain-link"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                        <div class="price-box-wrap">

                                <?php $promoType = strtolower($_product->getAttributeText('promo_type')); ?>
                                <?php if( stripos($promoType,'featured') !== false ) : ?>
                                <img class="is-featured" src="<?php echo $this->getSkinUrl('images/featured-product.png'); ?>" alt="<?php echo $this->__('Featured Product') ?>" width="121" height="45">
                                <?php endif; ?>

                                <?php echo $this->getPriceHtml($_product, true) ?>
                        <!-- .price-box --></div>
                        <ul class="check-list attribute-list">
                                <li><strong>Mounting Box Depth(Min):</strong> 25mm</li>
                                <li><strong>Fixing Centres:</strong> 120.6mm</li>
                                <li><strong>Size:</strong> 147.9mm x 87.4mm x 31mm</li>
                                <li><strong>Switch Type:</strong> Double Pole</li>

                        </ul>
                        <?php 
                        $setId = $_product->getAttributeSetId(); // Attribute set Id
                        $groups = Mage::getModel('eav/entity_attribute_group')
                            ->getResourceCollection()
                            ->setAttributeSetFilter($setId)
                            ->setSortOrder()
                            ->load();

                        $attributeCodes = array();
                        foreach ($groups as $group) {
                            if($group->getAttributeGroupName() == 'Specification'){ // set name
                            //$groupName          = $group->getAttributeGroupName();
                            //$groupId            = $group->getAttributeGroupId();

                            $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
                                ->setAttributeGroupFilter($group->getId())
                                ->addVisibleFilter()
                                ->checkConfigurableProducts()
                                ->load();
                                if ($attributes->getSize() > 0) {
                                    foreach ($attributes->getItems() as $attribute) {
                                        /* @var $child Mage_Eav_Model_Entity_Attribute */
                                        $attributeCodes[] = $attribute->getAttributeCode();  
                                    }
                                } 
                            }
                        }
                        print_r($attributeCodes);

                        $attributeValue = Mage::getModel('catalog/product')
                            ->load($_product->getId())
                            ->getAttributeText($attributeCodes[0]);

                        echo "The value: " . $attributeValue;
                        ?>
                    <!-- .productBlock__description --></div>

                    <div class="productBlock__actions">
                        <div class="productBlock__actions-shipping">
                            <div class="logos">
                                <img src="<?php echo $this->getSkinUrl('images/logo-british-general.png'); ?>" width="46" height="40" alt="">
                            </div>
                            <hr>
                            <h5>Free shipping</h5>
                            Lorem ipsum dolor sit amet
                        <!-- .productBlock__actions-shipping --></div>
                        <div class="productBlock__actions-buttons">
                            <hr>
                            <?php if($_product->isSaleable()): ?>
                                <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button medium green btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Basket') ?></span></span></button>
                            <?php else: ?>
                                <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                            <?php endif; ?>
                            <ul class="add-to-links">
                                <li><a href="" class="link-compare button medium light-grey">More information</a></li>
                            </ul>
                        <!-- .productBlock__actions-buttons --></div>
                <!-- .productBlock__actions --></div>
                <!-- .productBlock --></div>
            <!-- .item --></li>

        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>

    <div class="toolbar-bottom">
        <?php //echo $this->getToolbarHtml() ?>
    </div>
</div>
<?php endif; ?>

Best Answer

Managed to get the desired output with the following:

<?php 
$setId = $_product->getAttributeSetId(); // Attribute set Id
$groups = Mage::getModel('eav/entity_attribute_group')
    ->getResourceCollection()
    ->setAttributeSetFilter($setId)
    ->setSortOrder()
    ->load();

$attributeCodes = array();
foreach ($groups as $group) {
    if($group->getAttributeGroupName() == 'Specification'){ // set name
    //$groupName          = $group->getAttributeGroupName();
    //$groupId            = $group->getAttributeGroupId();

    $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
        ->setAttributeGroupFilter($group->getId())
        ->addVisibleFilter()
        ->checkConfigurableProducts()
        ->load();
        if ($attributes->getSize() > 0) {
            foreach ($attributes->getItems() as $attribute) {
                /* @var $child Mage_Eav_Model_Entity_Attribute */
                $attributeCodes[] = array('code' => $attribute->getAttributeCode(), 'type' => $attribute->getBackendType(), 'label' => $attribute->getFrontendLabel());  
                //Mage::Log($attribute->debug());

            }
        } 
    }
}

echo '<ul class="check-list attribute-list">';

foreach ($attributeCodes as $attributeCode) {
    $attrValue =  $_product->getData($attributeCode['code']);
    if(isset($attrValue)){
        echo '<li><strong>'. $attributeCode['label'] .': </strong>'. $attrValue .'</li>';
    } // if
} // foreach

echo '</ul>';
?>
Related Topic