E commerce - February 15, 2009 - 13 Comments
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>
-
<? }
-
}
-
}
-
?>


