我们知道,Magento网站点击一个产品分类(Category)后会直接显示产品列表(Product Listings),无论这个产品分类下面有没有子分类(subcategory)。但是有些网站,因为产品多、分类多,希望在分类页面显示子分类而不是直接显示产品,例如下图: 最近恰好有位朋友有这方面的需求,现把解决方法记录如下:(注:Magento版本为1.702,其它版本未测试。)
创建一个 sub-categories.phtml 文件(内容在下面文档中),上传到 app/design/frontend/base/default/template/catalog/navigation/sub-categories.phtml <div id="categories"> <div class="col_full"> <div class="listing" > <?php $_maincategorylisting=$this->getCurrentCategory()?> <?php $_categories=$this->getCurrentChildCategories()?> <?php if($_categories->count()):?> <? foreach ($_categories as $_category):?> <? if($_category->getIsActive()): $cur_category=Mage::getModel('catalog/category')->load($_category->getId()); $layer = Mage::getSingleton('catalog/layer'); $layer->setCurrentCategory($cur_category); $catName = $this->getCurrentCategory()->getName(); if($_imageUrl=!$this->getCurrentCategory()->getThumbnailImageUrl()): ?> <div class="category-box"> <div class="category-image-box"> <a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $this->getSkinUrl('images/subcategory-default.jpg') ?>"></a> </div> <div class="category-name"> <p><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $catName ?></a></p> </div> </div> <?endif?> <? if($_imageUrl=$this->getCurrentCategory()->getThumbnailImageUrl()):?> <?php /* Displays the subcategory image */ ?> <div class="category-box"> <div class="category-image-box"> <a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="media/catalog/category/<?php echo $_imageUrl?>"height="160"></a> </div> <div class="category-name"> <p><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></p> </div> </div> <? endif; endif;?> <?endforeach?> <?php /* This resets the category back to the original pages category **** If this is not done, subsequent calls on the same page will use the last category **** in the foreach loop */ ?> <?php $layer->setCurrentCategory($_maincategorylisting); ?> <?endif;?> </div> <br clear=all> </div> </div> 找到/app/code/core/Mage/Catalog/Model/Category.php 这个文件,在大约491行找到这段代码 public function getImageUrl() { $url = false; if ($image = $this->getImage()) { $url = Mage::getBaseUrl(’media’).’catalog/category/’.$image; } return $url; }在它下面加上如下代码:
/** * Retrieve thumbnail image URL * * @return string */ public function getThumbnailImageUrl($fullpath = false) { $url = false; if ($image = $this->getThumbnail()) { if ($fullpath == true) { $url = Mage::getBaseUrl(’media’).’catalog/category/’.$image; } else { $url = $image; } } return $url; }保存后上传到/app/code/local/Mage/Catalog/Model/Category.php
登录Magento网站后台,创建一个 Static Block Block Title: Sub Category Listing Identifier: sub-categories Content: {{block type="catalog/navigation" template="catalog/navigation/sub-categories.phtml"}} 到产品分类管理页面 (Catalog -> Manage Categories)选择一个有子分类的主分类,在 Display Settings 选项下 Display mode: Static block only CMS Block: Sub Category Listing Is Anchor: No 给每个子分类添加一个 thumbnail image
最后根据目标调整css。刷新缓存。
