Magento getProductUrl () does not return the correct url (random?)

I am using Magento 1.5.0.1, and the getProductUrl () function used in the cross-selling and selling blocks on the product page creates different URLs.

Either the correct URL: / laptop -bag.html Or the wrong one (well it works, but of course it is not a rewrite URL): / Catalog / product / view / ID / 825 / s / laptop bag / category / 16 /

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

Any ideas why this is happening?

I already ran the magento database patch, re-indexed and updated / flushed all the caches.

+8
magento
source share
4 answers

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

UPDATE: As per @ jordan314's comment below, Magento recommends EE clients:

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

+13
source share

An invalid URL is generated because it cannot find the rewritten URL. Perhaps this is caused by the wrong 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 rewrite URL can be found in the table named core_url_rewrite (including store_id information)

If it finds a match value in core_url_rewrite, it will generate the โ€œcorrect urlโ€, otherwise it will execute the code product_id + url + category_id

 $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } 
+3
source share

Try adding this when you receive your collection.

 $collection->addUrlRewrite(); 

It helps me.

+2
source share
 $id = 10; Mage::app()->setCurrentStore('admin'); $url = Mage::helper('catalog/product')->getProductUrl($id); 
+2
source share

All Articles