Magento网站分类页面显示子分类而不是产品的方法

我们知道,Magento网站点击一个产品分类(Category)后会直接显示产品列表(Product Listings),无论这个产品分类下面有没有子分类(subcategory)。但是有些网站,因为产品多、分类多,希望在分类页面显示子分类而不是直接显示产品,例如下图:
这里写图片描述
最近恰好有位朋友有这方面的需求,现把解决方法记录如下:(注:Magento版本为1.702,其它版本未测试。)

  1. 创建一个 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>
  1. 找到/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

  1. 登录Magento网站后台,创建一个 Static Block
Block Title: Sub Category Listing
Identifier: sub-categories
Content: {{block type="catalog/navigation" template="catalog/navigation/sub-categories.phtml"}}

这里写图片描述

  1. 到产品分类管理页面 (Catalog -> Manage Categories)

选择一个有子分类的主分类,在 Display Settings 选项下
Display mode: Static block only
CMS Block: Sub Category Listing
Is Anchor: No
给每个子分类添加一个 thumbnail image

最后根据目标调整css。刷新缓存。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值