0

In custom module Observer.php i am trying to get Product url

On view.phtml it gives correct working path like http://websitename.com/brave-gentleman-new-york-belt.html

But same code in Observer.php gives http://websitename.com/brave-gentleman-new-york-belt-120.html

$_product = Mage::getModel('catalog/product')->load($productId);
Mage::log( $_product->getProductUrl());

This code gives brave-gentleman-new-york-belt which is correct but it is not complete url

 $productURL = Mage::getResourceSingleton('catalog/product')
  ->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore());   

This gives the result

$productUrl  = Mage::getBaseUrl().Mage::getResourceSingleton('catalog/product')->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore()).Mage::helper('catalog/product')->getProductUrlSuffix();
1
  • which event is observed ? Commented Oct 17, 2017 at 5:31

2 Answers 2

0

Please try this ...

# returns string "product-url.html"
$productUrl = Mage::getResourceModel('core/url_rewrite')
    ->getRequestPathByIdPath('product/' . $productId, $storeId);
1

You can use below code also

$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$id = 404; // enter your product id
$product = Mage::getModel('catalog/product')->load($id);
$urlPath = $product->getUrlPath();

$productUrl = $baseUrl.$urlPath;

Not the answer you're looking for? Browse other questions tagged or ask your own question.