Magento – How to display custom attribute in related product in related.phtml

attributesmagento-1.8magento-enterpriseproduct-relationstheme

I need to display custom attribute in related product.

I have tried get value of custom attribute in related product from related.phtml file.

app/design/frontend/base/default/catalog/product/list/related.phtml file.

But not getting print custom attribute. My custom attribute name is "testattr".

 foreach($this->getItems() as $_item): ?>

// Start tried Displaying custom attribute

echo "Testattr::" . $_item->getTestattr();

// End tried Displaying custom attribute



            <li class="item">
                <?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
                    <?php if (!$_item->getRequiredOptions()): ?>
                        <input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
                    <?php endif; ?>
                <?php endif; ?>
                <div class="product">
                    <a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
                    <div class="product-details">
                        <p class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></p>
                        <?php echo $this->getPriceHtml($_item, true, '-related') ?>
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a>
                        <?php endif; ?>
                    </div>
                </div>
            </li>
        <?php endforeach ?>

Any one can help me? i need to display custom attribute value in related product right side sidebar in default magento.

Best Answer

You should override the getItems() function from that particular block to include the attribute you want to use with addAttributeToSelect(). Block is catalog/product_list_related.

public function getItems()
{
    return $this->_itemCollection;
}

Check the _prepareData() function in the same block and there you will see how attributes are selected.

protected function _prepareData()
{
    $product = Mage::registry('product');
    /* @var $product Mage_Catalog_Model_Product */
    $this->_itemCollection = $product->getRelatedProductCollection()
        ->addAttributeToSelect('required_options')
        ->setPositionOrder()
        ->addStoreFilter()
    ;

At some point you will find a hacky solution to load every product inside the loop and get the attribute you need from there. That is not a good idea, dont follow that advice if you want to have a decent performance.

Related Topic