Magento getProductUrl() is not returning the right url (random?)
Asked Answered
E

4

12

I am using Magento 1.5.0.1 and the getProductUrl() function used in the cross sell and up sell blocks on the product page is throwing up different URL formats.

Either the correct url like: /laptop-bag.html Or the wrong one (well it works, but of course its not the rewrite URL): /catalog/product/view/id/825/s/laptop-bag/category/16/

Sometimes both cross sell and up sell blocks return the correct URL, sometimes both use the longer version, and in some cases, one uses the correct and the other uses the long version??

Any ideas why this is happening?

I have already run a magento database repair, reindexed, and refreshes / flushed all caches.

Elementary answered 30/7, 2012 at 14:53 Comment(0)
T
18

Try $product->getUrlPath() instead of $product->getProductUrl()

UPDATE: As per below comment by @jordan314, Magento recommends to EE customers:

The url_path attribute is no longer used as of 1.13 but is still available for backward-compatibility, and Magento will not assign a value to it for new products, so it's not recommended to continue using it. Perhaps you could try using $product->getProductUrl() instead.

Trujillo answered 30/7, 2012 at 21:9 Comment(3)
Thanks! I can see what happened now, essentially it happens when you are viewing a product from a category and the related / upsell products do not exist in the same category. By using the urlpath, it gets it from the root. Works like a charm!Elementary
Thanks Kalpesh, had same issue, lots of digging and found that! brilliant!!Dunkle
I'm using EE. According to Magento Support, "The url_path attribute is no longer used as of 1.13 but is still available for backward-compatibility, and Magento will not assign a value to it for new products, so it's not recommended to continue using it. Perhaps you could try using $product->getProductUrl() instead." So I would use $product->getProductUrl() again instead of $product->getUrlPath().Beslobber
Y
7

The incorrect url is generated because it can't find the rewritten url. Maybe it is caused because incorrect store_id. eg:

$id = 290;
Mage::app()->setCurrentStore('default');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";

//change store id
Mage::app()->setCurrentStore('admin');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";

result:

store_id: 1
http://local.com/surestep-pro-diabetic-test-strips-50-strips-professional-care.html
store_id: 0
https://local.com/index.php/catalog/product/view/id/290/s/surestep-pro-diabetic-test-strips-50-strips-professional-care/

The correct url rewrite can be found in table named core_url_rewrite (including the information about the store_id)

If it found match value in core_url_rewrite, it will generate 'the correct url' else it will concat the product_id + url key + category_id

$routePath = 'catalog/product/view';
$routeParams['id']  = $product->getId();
$routeParams['s']   = $product->getUrlKey();
if ($categoryId) {
    $routeParams['category'] = $categoryId;
}
Yu answered 30/7, 2012 at 16:25 Comment(1)
By simply adding Mage::app()->setCurrentStore('default'); to first line of our method (as suggested) has worked, thanks! :DPiegari
M
7

Try add this when you're getting your collection

$collection->addUrlRewrite();

It has helped me.

Mullein answered 30/3, 2016 at 6:3 Comment(1)
This worked. But need add $collection->load(); And after $collection->addUrlRewrite();Inflexed
S
4
$id = 10;
Mage::app()->setCurrentStore('admin');
$url = Mage::helper('catalog/product')->getProductUrl($id);
Statics answered 16/9, 2016 at 11:43 Comment(1)
Your answer certainly is worth a little explanation. including an explanation really helps to improve the quality of your post.Sludgy

© 2022 - 2024 — McMap. All rights reserved.