Magento. Get full product URL.
Will return full product url with categories path in any place on site.
/**
* Get full path of url including category
*
* @param Mage_Catalog_Model_Product $product
* @return string url
*/
public function getFullProductUrl($product)
{
if(is_object($product) && $product->getSku()) {
$proArr = $product->getCategoryIds();
try{
$query = "SELECT `request_path`
FROM `core_url_rewrite`
WHERE `product_id`='" . $product->getEntityId() . "'
".((end($proArr)) ? 'AND `category_id`='.end($proArr) : '')."
AND `store_id`='" . Mage::app()->getStore()->getId() . "';
";
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$result = $read->fetchRow($query);
return Mage::getUrl('') . $result['request_path'];
} catch(Exception $e){
$allCategoryIds = $product->getCategoryIds();
$lastCategoryId = end($allCategoryIds);
$lastCategory = Mage::getModel('catalog/category')->load($lastCategoryId);
$lastCategoryUrl = $lastCategory->getUrl();
$fullProductUrl = str_replace(Mage::getStoreConfig('catalog/seo/category_url_suffix'), '/', $lastCategoryUrl) . basename( $product->getUrlKey() ) . Mage::getStoreConfig('catalog/seo/product_url_suffix');
return $fullProductUrl;
}
}
return false;
}