2013-08-10 3 views
0

Je voudrais que Magento affiche le nombre total de produits incluant les produits dans les sous-catégories. Par exemple. si la catégorie Main a deux produits et une sous-catégorie a cinq produits.Magento catégorie compte

i.e. .:

  • catégorie (12)
  • Sous-catégorie A (5)
  • SUB CATÉGORIE B (5)

Comment puis-je faire cela?

Répondre

1

Essayez avec ce code.

<ul>    
      <?php 
      // This is category id 
      $id = 42; 

      $cat = Mage::getModel('catalog/category')->load($id); 
      $subcats = $cat->getChildren(); 
      foreach(explode(',',$subcats) as $subCatid): 
      $_category = Mage::getModel('catalog/category')->load($subCatid); 
      if($_category->getIsActive()): 
       $productCount = Mage::getModel('catalog/category')->load($_category->getId())->getProductCount();?> 

     <li><a href="<?php echo $_category->getURL();?>"><span><?php echo $_category->getName();?> 
<?php echo '('.$productCount.')'?></span></a></li> 

     <?php endif; 
     endforeach;?> 
     </ul> 
0

Sinon, vous pouvez utiliser cette fonction.

public function getProductCount($category) 
{ 
$prodCollection = Mage::getResourceModel(’catalog/product_collection’)->addCategoryFilter($category); 
Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($prodCollection); 
Mage::getSingleton(’catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($prodCollection); 
return $prodCollection->count(); 
} 
+0

salut J'ai besoin de votre aide à ce sujet. Je n'arrive pas à obtenir le travail – Stifboy

+0

Avez-vous essayé avec les solutions ci-dessus? – Sukeshini