2 added 785 characters in body
source | link

Instead of using $product_url = $site_url.$product->getUrlPath($category); those.

You use below code

 $product = Mage::getModel('catalog/product')->load($product_id);
 $product_url = '';
 $cats = $product->getCategoryIds();
 if (sizeof($cats)) {
     $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
   if($category->getId()){
    $product->setCategory($category);
   }
 }
$product_url = $product->getProductUrl();

Or via ProductCollection with addUrlRewrite() function:

$product = Mage::getModel('catalog/product')->load($product_id);
 $product_url = $product->getProductUrl();
 $cats = $product->getCategoryIds();

 if (sizeof($cats)) {
   $collection = Mage::getResourceModel('catalog/product_collection');
   $collection->addIdFilter($product->getId());
   if($collection->getSize() >= 1):
      /* Add addUrlRewrite for add category url with product */
       $collection->addUrlRewrite((int)$cats[0]);
       $ProductWithCategory = $collection->getFirstItem();
       $product_url = $ProductWithCategory->getProductUrl();
   endif;  
 }

Instead of using $product_url = $site_url.$product->getUrlPath($category); those.

You use below code

 $product = Mage::getModel('catalog/product')->load($product_id);
 $product_url = '';
 $cats = $product->getCategoryIds();
 if (sizeof($cats)) {
     $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
   if($category->getId()){
    $product->setCategory($category);
   }
 }
$product_url = $product->getProductUrl();

Instead of using $product_url = $site_url.$product->getUrlPath($category); those.

You use below code

 $product = Mage::getModel('catalog/product')->load($product_id);
 $product_url = '';
 $cats = $product->getCategoryIds();
 if (sizeof($cats)) {
     $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
   if($category->getId()){
    $product->setCategory($category);
   }
 }
$product_url = $product->getProductUrl();

Or via ProductCollection with addUrlRewrite() function:

$product = Mage::getModel('catalog/product')->load($product_id);
 $product_url = $product->getProductUrl();
 $cats = $product->getCategoryIds();

 if (sizeof($cats)) {
   $collection = Mage::getResourceModel('catalog/product_collection');
   $collection->addIdFilter($product->getId());
   if($collection->getSize() >= 1):
      /* Add addUrlRewrite for add category url with product */
       $collection->addUrlRewrite((int)$cats[0]);
       $ProductWithCategory = $collection->getFirstItem();
       $product_url = $ProductWithCategory->getProductUrl();
   endif;  
 }
1
source | link

Instead of using $product_url = $site_url.$product->getUrlPath($category); those.

You use below code

 $product = Mage::getModel('catalog/product')->load($product_id);
 $product_url = '';
 $cats = $product->getCategoryIds();
 if (sizeof($cats)) {
     $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
   if($category->getId()){
    $product->setCategory($category);
   }
 }
$product_url = $product->getProductUrl();