1

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; 
2

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())
1

For getting correct product url at category page,

You need to join url rewrite manager to product collection by adding to addUrlRewrite($categoryId) with sending category id as parameter at that function.

Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($cur_cat->getId())->addUrlRewrite($cur_ca->getId());

Magento are added category url to product url whenever magento backend setting is

Use Categories Path for Product URLs =Yes

At Admin>System>Configuration>Catalog>Search Engine Optimizations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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