Magento – magento 1.9 not getting the correct product url in sub category page

categoryhomemagento-1.9producturl

I created menu like category1 having product1 and product2 category2 having sub-category1 and sub-category2. In home page product links are showing product1.html as well as category page but in sub-category page in showing like this

catalog/product/view/id/9/s/test/category/15/

Please help me for this one. My code:

 $_helper = Mage::helper('catalog/category');
   $_categories = $_helper->getStoreCategories();  
$currentCategory = Mage::registry('current_category');

if (count($_categories) > 0):

foreach($_categories as $_category): 
        <li>
            <a href=" echo $_helper->getCategoryUrl($_category) ">
                 echo $_category->getName() 
            </a>

          $cur_category = Mage::getModel('catalog/category')->load($_category->getId()); 

        $subCats = Mage::getModel('catalog/category') ->load($_category->getId())->getChildren(); 
        $subCatIds = explode(',',$subCats);

          if (count($subCatIds) > 1): 
         <ul>
           foreach($subCatIds as $subCatId): 
            $subCat = Mage::getModel('catalog/category')->load($subCatId); 
             if($subCat->getIsActive()): 
             <li>
              <a href=" echo $subCat->getUrl(); ">
               echo $subCat->getName(); 
              </a>
             </li>
             endif; 
            endforeach; 
          </ul>
         endif; 

         $_productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category)->setOrder('entity_id','ASC'); 
             if (count($_productCollection) > 0): 
                <ul>
                     foreach($_productCollection as $_product): 
                     $cur_product = Mage::getModel('catalog/product')->load($_product->getId());

                     if ($cur_product->getStatus()) {


                        <li>
                         <a href=" echo  $cur_product->getProductUrl();"> echo $this->htmlEscape($cur_product->getName())</a> 
                        </li>
                 } 
                     endforeach; 
                </ul>

              endif;    
        </li>
     endforeach; 
</ul>
 endif; 

Best Answer

Try to reindex data from System -> Index management and see if you still have an issue.

Or you can try this way also

    $collection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($current_category); 
    $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->setOrder('entity_id','ASC')
            ->addUrlRewrite($current_category->getId())
Related Topic