E commerce
- February 15, 2009
Magento product page : how to display other products from same category
Magento comes with a lot of product relationship features :  implemented as selective lists maintained for each product by the shop owner, related / cross selling and up selling backend features allow some sort of manual product linking that displays product associations on the front end. For example related products are displayed by default in the product page right column, while upsell / crosssell products are displayed on the shopping cart page. These 3 types of product associations are well explained on inchoo’s blog, and  on magento ‘s knowledge base you’ll find user instructions on how to setup  product relationship in the backend.
What we aim here is to setup an automatic  display of related products on a product view page, via the categories they belong to. We build two loops : one that gets categories the product belongs to, and another one inside that gets products of each category.
How to setup category related products in Product view :
- add the following code somewhere in default\template\catalog\product\view.phtml
- Configure the category query according to your catalog by uncommenting the following :
- ->setPage(1, 1) : selects only one category
- ->addFieldToFilter(‘level’,”3″) : selects only 3rd level categories
- ->addFieldToFilter(‘parent_id’,”3″) : select only child categories of no 3
- ->setOrder(“level”) : combined by setPage, returns the lowest level category
- please that the product loop uses the $_product variable, which can be dangerous because this name is widely used. We use it here for comodity reasons because I just took toe display code from Magento’s product list view
-
if ($_product) {
-
// get collection of categories this product is associated with
-
$categories =$_product->getCategoryCollection()
-
// ->setPage(1, 1)
-
//->addFieldToFilter(‘level’,"3")
-
//->addFieldToFilter(‘parent_id’,"3")
-
//->setOrder("level")
-
->load();
-
-
// 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>
-
</div>
-
<?$products = Mage::getResourceModel(‘catalog/product_collection’)
-
->addCategoryFilter($_category)
-
->addAttributeToSelect(‘small_image’);
-
-
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()) ?>">
-
-
<img src=<?=$_imageUrl ?> width="<?=$width?>" height="<?=$height?>"/>
-
<?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: ?>
-
<?php endif; ?>
-
<div class="clear"></div>
-
<div class="description">
-
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><small><?php echo $this->__(‘Learn More’) ?></small></a>
-
</div>
-
</div>
-
<? }
-
}
-
}
-
?>




20 Responses to “Magento product page : how to display other products from same category”
February 16, 2009 - Justin
This works great, except for the fact that it breaks the actual product page you’re on. All the products now share the same name (But they all have their correct title tag and description…), and they all have the same add to cart link.
Halfway there!
February 16, 2009 - admin
Well I don’t experience that bug : are you sure you put the code at the bottom of category/product/view.phtml
otherwise make sure you don’t mix the $_product page variables with the new loop that also uses $_product variables : if you’re not lazy like I am just change the variable name inside the loop and that might solve what looks like a variable name issue
March 30, 2009 - admin
Hi, this is not very hard but requires you to follow the tutorial here
http://www.webmasterbulletin.net/2008/12/how-to-install-magento-free-themes/564
let me know how it goes, is your site public yet ?
April 20, 2009 - How?
How shall this work out, when I only paste the code above into the view.phtml there is the opening tag <?php missing, so I would definitely cause a php error as the last on that page is a closing tag. So, I added the missing php tag before the code right after the last div of the page and the whole product page disappears. I do not know what version you worked with, this is still with 1.2.1.2 and a custom template, but this should also work in any custom view phtml but sadly does not.
April 20, 2009 - admin
@HOW : you have to paste this code after the existing code in view.phtml
May 2, 2009 - nigel
hi i have teh code installed and working, but i have a large number of products per category and as a result it takes a while to load. Do you know how to limit the query to say pick out 4 items and return those
May 2, 2009 - nigel
also to help anyone who may have issues – check the code for any errant formatting after pasting the code – specifically check where the ‘ are supposed to be in areas like
__(‘Out of stock’) ?>
May 3, 2009 - admin
hi nigel, common magento query limit statement : add the following to line 21, should do the job
$products->getSelect()->limit(4)
May 6, 2009 - saphia
In magento how to show product category in the admin with product listing
August 5, 2009 - Deter
If you want to get random products from same category, put at line 21 this code:
$products->getSelect()->order(‘rand()’);
November 2, 2009 - Matt
This is great code. How would you display the category products with same manufacturer?
Thanks!
November 13, 2009 - onionlips
Gave me the direction I needed … thanks!
November 18, 2009 - pankaj
I want to show only “in stock” products on one page & both “in stock” and “out of stock” products on other page from same category. How can i do that…? Please help me on this. Its really an urgent. Waiting for your positive reply.
September 15, 2010 - gggggggg
Can anyone confirm if this works with 1.4.1.1 ?
September 24, 2010 - Pranav
I want to show bestseller or most viewed products at the below part on the product view page, can anybody please help ?
February 2, 2011 - Danny
Great! Really great! Even works in Magento 1.5. Thank you!
April 2, 2011 - vignesh
I have to show only enabled product. it shows all the products
March 1, 2012 - Saurabh
Hello Paul,
Can you help me…
I need to display all enabled product in category view page in Magento.
Thanks
August 11, 2012 - コスプレ衣装
Gredit job,I use this code in my site magento 1.5,and everything is good.