I use the following code to display products from same category in my view page.

// if the product is associated with any category
if ($categories->count()) 
foreach ($categories as $_category)
  $cur_category = Mage::getModel('catalog/category')->load($_category->getId());
?><div style="clear:both">
<h2>Dans la même collection :<?=$cur_category->getName()?></h2>
<?$products = Mage::getResourceModel('catalog/product_collection')

 foreach ( $products as $productModel ) 
    $_product = Mage::getModel('catalog/product')->load($productModel->getId());
      $width=135; $height=135;
     $_imageUrl =  $this->helper('catalog/image')->init($productModel, 'small_image')->resize($width, $height);
  <div class="product-shop">
   <h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<?php echo $this->htmlEscape($_product->getName())?></a></h5>

 <img src=<?=$_imageUrl ?> width="<?=$width?>" height="<?=$height?>"/>
<?php echo $this->getPriceHtml($_product, true) ?>
   <?php if($_product->isSaleable()): ?>
 <button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>
 <?php else: ?>
  <div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
     <?php endif; ?>
     <div class="clear"></div>
      <div class="description">
       <?php echo nl2br($_product->getShortDescription()) ?>
    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><small><?php echo $this->__('Learn More') ?></small></a>
<?  }  

The above code displays the randomly products from the same category.

I have this issue where I need to add some custom attributes bellow in the view page, but my custom attributes always displays different values.

I assume this could be because of the loop of the above code.

some thing with the:

<?  }  

can anybody help me with this?

  • The question is not clear as it is. What do you expect the code do and what does it actually do? Also the code is pretty unreadable, can you strip it down to a minumum working example? Commented Aug 31, 2015 at 12:21
  • here is the link where i found the code, and the code works just fine. blog.plazathemes.com/archives/2602 I used the motioned code in my view page. Also, i have some custom attributes (specifications of the article), which are displayed just bellow the latest products from the same category. The issue with the custom attributes is that custom attributes have wrong values. Not the ones specified in the filed in magento. I guess this is because of the above code. (see link)
    – user15151
    Commented Aug 31, 2015 at 12:28
  • If it works, what is your question? Commented Aug 31, 2015 at 12:29
  • code works, but it breaks my custom attributes values. values in my custom attributes are not displayed correct. And the values are changing every time i refresh the page.
    – user15151
    Commented Aug 31, 2015 at 12:35
  • There are some issues with how you load the collection and the products. Check this out, maybe it helps you: magento.stackexchange.com/a/78700/243 Commented Aug 31, 2015 at 12:38

1 Answer 1


To display other products from the same category, here is my modified code, which I assume can be used in any view.phtml. In my case it works. Just use your css, to make it look good.

<!--Related products-->
    <?php $categories = $_product->getCategoryIds(); ?> 
    <?php $result = array(); 
    foreach($categories as $cat_id) { 
    $category = Mage::getModel('catalog/category'); 
    $collection = $category->getProductCollection(); 
    foreach ($collection as $product) { 
    $result[] = $product->getId(); 
<div class="related-prod-conteiner"> 
    <ul class="r-prod-list-horizontal"> 
        <?php if(sizeof($result) >= 5) 
        $ourneed = array_rand($result,5); 
        foreach($ourneed as $cc) 
        $thisproduct= Mage::getModel('catalog/product')->load($result[$cc]); 
                <div class="item-content-image">
                    <a href="<?php echo $thisproduct->getProductUrl()?>"><img class="thumbnail" src="<?php echo Mage::helper('catalog/image')->init($thisproduct, 'thumbnail')->resize(150, 150)?>" alt="<?php echo $thisproduct->getName()?>"></a>
                <div class="item-content-name"> 
                    <a href="<?php echo $thisproduct->getProductUrl()?>">
                    <h2 class="product-name">
                        <div class="three-dots-3">
                            <span class='ellipsis_text'><?php echo $thisproduct->getName()?></span>
                <div class="item-content-price"> 
                    $product_block = new Mage_Catalog_Block_Product;
                    echo $product_block->getPriceHtml($thisproduct);
                <div class="item-content-rate">
                    <?php echo $this->_reviewsHelperBlock->getSummaryHtml($thisproduct, short); ?>
        <?php } ?> 
        <?php }else { 
        foreach($result as $cc) 
        $thisproduct= Mage::getModel('catalog/product')->load($cc); 
        <a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a> </li> 
        <?php } 
    <!--/Related products-->

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.