有一个需求,做出新产品页面,然后产品页面下面是所有的一级分类以及一级分类下面的新产品的个数。分享代码如下:
<?php
//// top Seller
if($_category->getName() == "New Arrivals"){
echo '<div class="fuckyoufm">';
$currid = 3;
$allcategory = Mage::getModel('catalog/category')->load($currid);
$category_model = Mage::getModel('catalog/category'); //get category model
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$all_child_categories = Mage::getModel('catalog/category')->getCategories($currid);
foreach($all_child_categories as $cate){
if($currid!=$cate->getId()){
$cat_id = $cate->getId(); // category id
$category = Mage::getModel('catalog/category')->load($cat_id);
$_products = $category->
getProductCollection()->
addCategoryFilter($category)->
addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))->
addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')->
addAttributeToSelect('*');
$tyr = Mage::getModel('catalog/category')->load($cat_id);
echo "<a href='".Mage::getBaseUrl('web').$tyr->getUrlKey().Mage::helper('catalog/category')->getCategoryUrlSuffix()."'><span class='tuy'>".$tyr->getName()."(".$_products->getSize().")</span></a>";
}
}
echo '<div style="clear:both;"></div>';
echo "</div>";
}
?>
OK,如果root cateory的id为2,那么把$currid = 3; 改为$currid = 2;
NICE!