Question

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

$related_product_collection = $_product->getRelatedProductCollection()
                                        ->addUrlRewrite()
                                        ->AddStoreFilter();

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!

Was it helpful?

Solution

This is how I eventually fixed this problem.

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

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

<?php 
    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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top