Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 418
cappuccino Avatar asked Jul 30 '12 14:07

cappuccino


1 Answers

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;
}
like image 142
ivantedja Avatar answered Nov 17 '22 01:11

ivantedja