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.

cappuccino
  • 2,175
  • 5
  • 26
  • 48

4 Answers4

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.

Kalpesh
  • 5,635
  • 2
  • 23
  • 39
  • 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! – cappuccino Jul 31 '12 at 05:51
  • Thanks Kalpesh, had same issue, lots of digging and found that! brilliant!! – Adam Lesniak Aug 10 '13 at 14:35
  • 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(). – alana314 Feb 26 '15 at 01:50
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;
}
ivantedja
  • 2,553
  • 1
  • 22
  • 22
  • By simply adding `Mage::app()->setCurrentStore('default');` to first line of our method (as suggested) has worked, thanks! :D – Code Slicer Apr 16 '18 at 14:48
7

Try add this when you're getting your collection

$collection->addUrlRewrite();

It has helped me.

karick
  • 123
  • 1
  • 6
  • This worked. But need add $collection->load(); And after $collection->addUrlRewrite(); – Alex Nov 21 '18 at 11:49
4
$id = 10;
Mage::app()->setCurrentStore('admin');
$url = Mage::helper('catalog/product')->getProductUrl($id);
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
test
  • 49
  • 1
  • Your answer certainly is worth a little explanation. [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. – J. Chomel Sep 16 '16 at 12:02