所有产品(类别和子类别产品)都依靠magento的类别列表页面

时间:2014-03-16 07:00:35

标签: magento collections

我想显示所有类别和子类别下的产品的数量。我不能使用工具栏来实现那个目的。我知道默认的magento工具栏有代码,但我想使用自定义代码才能获得count.i试试通过调用块方法getTotalNum()来获取集合但没有成功。谢谢提前

1 个答案:

答案 0 :(得分:1)

list.phtml中,您可以尝试以下代码:

//get the current layer
$layer = Mage::getSingleton('catalog/layer');

//get the current category
$currentCategory = $this->getLayer()->getCurrentCategory();

//get the subcategories
$categoriesChildren = $currentCategory->getChildrenCategories(); //means get your children
$categoriesSiblings = $currentCategory->getParentCategory()->getChildrenCategories(); //means get your siblings
//(decide if you want to use siblings or children, here we use children)

//get the product collection for the current layer
$productCollection = Mage::getResourceModel('catalog/product_collection'); 
$layer->prepareProductCollection($productCollection);

//count up
$productCollection->addCountToCategories($categoriesChildren); //note the function accepts a collection of categories and counts them all

//display
echo("<ol>".PHP_EOL);
foreach ($categoriesChildren as $key=>$_category){
  if($_category->getIsActive()){
    echo("<li>".PHP_EOL);
    echo("<a href=\"".$this->getCategoryUrl($_category)."\">".$this->htmlEscape($_category->getName()));
    echo("(".$_category->getProductCount().")</a>".PHP_EOL);
    echo("</li>".PHP_EOL);
  }
}
echo("</ol>".PHP_EOL);
//done

我想你可能想稍微定制一下。