Earthtracker's Blog

Just another WordPress.com weblog

Magento Canonical URL problem from outside of main category solve

In magento , if you add a product in categories multiple categories, and like to display product from outside of the categories, it will no reflect the correct url, it will create a url with the domain name + product url title,It will cause duplicate contents in google, to resolve the problem, here is the solution,My special thanks to http://stackoverflow.com/questions/10997108/magento-getproducturl-is-not-returning-the-right-url/17895371#17895371

$cat_count=count($_categories);

   if($cat_count>0){

   if($_categories[0]==2) // here 2 is root category id

   $cat_count=$cat_count-1;

   else

   $cat_count=$cat_count-2;}

   $_categories = $_product->getCategoryIds();

   $_category = Mage::getModel('catalog/category')->load($_categories[$cat_count]); 

   $caturl=explode('.html', $_category->getUrlPath());      
      
   $product_url = Mage::getUrl($caturl[0]).basename($_product->getProductUrl()); 

Advertisements
Leave a comment »