For each product in the cart I'm loading its related products.

$related_product_collection = $_product->getRelatedProductCollection()

But I'm getting the URL of these related products without the category name

<?php echo $relatedProduct->getProductUrl(); ?>

I'm getting

http://myDomain/product1 instead of http://myDomain/categ1/product1

The courses can belong to several categories.

What's the best approach here? Add a rewrite manually? http://myDomain/product1 --> redirects to --> http://myDomain/cat1/product1

Add a loop through all the categories to get the right one for that related product?

Thanks in advance!

1 Answer 1


This is how I eventually fixed this problem.

<?php foreach ($unique_related_products as $id) {
    $relatedProduct = Mage::getModel('catalog/product')->load($id);
    $categoryCollection = $relatedProduct->getCategoryCollection();
    Mage::register('current_category', $categoryCollection->getFirstItem());

 <a href="<?php echo $relatedProduct->getProductUrl(); ?>">

    Mage::unregister('current_category'); //reset the registry to previous state.

I'm getting the 1st category of each product to compose the product URL. Not sure it's the best solution though.

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.