1

I'm loading my related products like this:

 <?php $_relatedProducts = $_product->getRelatedProducts(); ?>


    <?php foreach ($_relatedProducts as $_item):
       $_item = Mage::getModel('catalog/product')->load($_item->getId());  ?>


       <a href="<?php echo $_item->getProductUrl() ?>">
       <?php echo $this->htmlEscape($_item->getName()) ?>
       </a>

    <?php endif; ?>

for any reason i'm getting this weird url

example.com/catalog/product/view/id/393/s/productname/category/4/

with $_item->getProductUrl();

but the product itself has a canonical with the right url, which should be:

example.com/productname

how can i get this url in my loop?

I know that with a normal collection i can add ->addUrlRewrite(); and it works... but here?

Thanks a lot!

  • Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->addUrlRewrite()->load($_item->getId()); In foreach, you can use $_item->getUrlPath(); – Krishna ijjada May 13 '17 at 14:59
  • thanks a lot but could you explain it a little bit more with my code example? i don't get how i should implement that without creating a double loop @Krishati95Dev – user3561012 May 13 '17 at 15:05
  • <?php $_relatedProducts = $_product->getRelatedProducts(); ?> <?php foreach ($_relatedProducts as $_item): $_item = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->addUrlRewrite()->load($_item->getId()); ?> <a href="<?php echo $_item->getUrlPath() ?>"> <?php echo $this->htmlEscape($_item->getName()) ?> </a> <?php endif; ?> – Krishna ijjada May 13 '17 at 15:11
  • tried it exactly like this, but i'm getting the following error: SELECT e.* FROM catalog_product_entity AS e. Any idea why? :-( @Krishati95Dev – user3561012 May 13 '17 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.