Question

Hope somebody can help me out here.

What I'm try to do, is to show related products, when you're landing on a products page.

Let's say:

I have a categori, the name is computer. In that computer category, I have 3 products a Acer laptop, a HP laptop and a Apple macbook air.

If my customer click on the mac - how can I then show the related products? (eg. the Acer and the HP laptop).

I don't want to edit the catelog.xml - allready tryed that, whitout any luck.

I allready got som php file - And it sort-of working, but when the customer lands on one of the product page, they also sees the product, that they're reading about - how can I remove the current product from the list?

<?php
        $currentCategoryId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
        $product_id = $this->getProduct()->getId();

        echo $product_id;

        $categoryid = $currentCategoryId;

        $category = new Mage_Catalog_Model_Category();
        $category->load($categoryid);
        $collection = $category->getProductCollection();
        $collection->addAttributeToSelect('*');

        foreach ($collection as $_product) { ?>

            <a href="<?php echo $_product->getProductUrl(); ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

    <?php } ?>

It takes all the products from the entire category, and then displays them on the current productpage. I really hope someone can help me out.

Was it helpful?

Solution

You could try

1) using filter

 ...
 $collection->addAttributeToSelect('*');
 $collection->addAttributeToFilter('entity_id', array('neq', $product_id));

2) using if statement to filter out the id

 ....
 foreach ($collection as $_product) { ?>
     <?php if($_product->getId() != $product_id) : ?>
        <a href="<?php echo $_product->getProductUrl(); ?>">...</a>
     <?php endif;?>
<?php } ?>

OTHER TIPS

You can use it like this in view.phtml

 $related_prods = $_product->getRelatedProductIds()

foreach($related_prods as $related)

{

$_rel = Mage::getModel('catalog/product')->load($related);

echo $_rel->getName() . " " . $_rel->getSku();

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top