1

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; ?>
6
  • can you add your list.phtml file code here? Commented Oct 14, 2015 at 16:21
  • @AkhileshPatel Added
    – wickywills
    Commented Oct 14, 2015 at 16:31
  • I didn't see the code which you tried. Also did you tried to clear cache after change? Commented Oct 14, 2015 at 16:37
  • @AkhileshPatel Apologies, I've been hacking things around a bit. Code now added. The rest of the page displays fine, the problem is only with the attributes as described. And yes, all caches are disabled, and I have flushed both of the other caches.
    – wickywills
    Commented Oct 14, 2015 at 16:44
  • can you try directly like <?php echo $_product->getAttributeText('mounting_box_depth'); See if that print anything or not Commented Oct 14, 2015 at 16:58

2 Answers 2

2

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>';
?>
0

A few things to get this working:

Any attribute that you want to display on the product pages need to have "Visible on Product View Page on Front-end" set to Yes, otherwise they won't be included in the query to JOIN all of the product attributes together, and you can't access in $_product without reloading the product.

You'll want to get the attribute label, by store, from the attribute model by changing this

/* @var $child Mage_Eav_Model_Entity_Attribute */
$attributeCodes[] = $attribute->getAttributeCode(); 

To this:

$storeId = Mage::app()->getStore()->getId();
//....//
/* @var $child Mage_Eav_Model_Entity_Attribute */
$attributeCodes[ $attribute->getAttributeCode() ] = $attribute->getStoreLabel($storeId); 

At that point you should know the label for each attribute, and you'll have the attribute code as the array key to then feed to getAttributeText()

1
  • Unfortunately I'm still unable to get any of the values for the attributes. I do receive the labels though, so some progress there. As a test, I am just trying to use echo $_product->getAttributeText('mounting_box_depth'); - don't get anything returned from this. Sorry, I've only been using Magento for a week!
    – wickywills
    Commented Oct 15, 2015 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.