众所周知,ECSHOP的价格区间是在后台设置一个“价格区间个数”,然后由程序来自动平分,很多ECSHOP网店主都认为这样做,有很大不尽人意的地方。
也有人提供过修改方法,好像只是在模板文件里手动写死几个区间的一种方法,还是不能自由定义。
下面我要讲的方法是可以在ECSHOP后台自由定义价格区间(例如:1-30,31-200,201-1000,1001-5000,5000以上),前台自动体现的一种方法,而且每个类别的价格区间可以任意设置,让价格区间更人性化。
首先我们来修改ECSHOP的数据库结构,目的是给 ecs_category 增加一个新字段,
进入 后台 》 数据库管理 》SQL查询,执行下面的SQL语句
ALTER TABLE `ecs_category` ADD `grade_define` VARCHAR( 255 ) NOT NULL AFTER `grade`
注意:这里我使用的是 默认数据库前缀ecs_,如果你的数据库前缀不是 ecs_ , 请修改之。
2)、
打开 /admin/templates/category_info.htm
找到
{$lang.grade}: |
{$lang.notice_grade}
|
将它修改为
自定义价格区间: |
以 半角 , 间隔,如: 1-30,31-200,201-1000,1001-5000,5000以上
|
3)、
接着打开 /admin/category.php 文件,搜索
$cat['grade'] = !empty($_POST['grade']) ? intval($_POST['grade']) : 0;
一共能搜到两处,在每一处的下面都增加代码(注意是每一处)
$cat['grade_define'] = !empty($_POST['grade_define']) ? trim($_POST['grade_define']) : ''; //自定义价格区间 by zhangyh
到此,后台管理部分修改完毕。
4)、
下面开始修改前台部分
打开 /category.php
找到
if ($cat['grade'] == 0 && $cat['parent_id'] != 0)
{
$cat['grade'] = get_parent_grade($cat_id); //如果当前分类级别为空,取最近的上级分类
}
if ($cat['grade'] > 1)
{
$sql = "SELECT min(g.shop_price) AS min, max(g.shop_price) as max ".
" FROM " . $ecs->table('goods'). " AS g ".
" WHERE ($children OR " . get_extension_goods($children) . ') AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 ';
//获得当前分类下商品价格的最大值、最小值
$row = $db->getRow($sql);
// 取得价格分级最小单位级数,比如,千元商品最小以100为级数
$price_grade = 0.0001;
for($i=-2; $i<= log10($row['max']); $i++)
{
$price_grade *= 10;
}
//跨度
$dx = ceil(($row['max'] - $row['min']) / ($cat['grade']) / $price_grade) * $price_grade;
if($dx == 0)
{
$dx = $price_grade;
}
for($i = 1; $row['min'] > $dx * $i; $i ++);
for($j = 1; $row['min'] > $dx * ($i-1) + $price_grade * $j; $j++);
$row['min'] = $dx * ($i-1) + $price_grade * ($j - 1);
for(; $row['max'] >= $dx * $i; $i ++);
$row['max'] = $dx * ($i) + $price_grade * ($j - 1);
$sql = "SELECT (FLOOR((g.shop_price - $row[min]) / $dx)) AS sn, COUNT(*) AS goods_num ".
" FROM " . $ecs->table('goods') . " AS g ".
" WHERE ($children OR " . get_extension_goods($children) . ') AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 '.
" GROUP BY sn ";
$price_grade = $db->getAll($sql);
foreach ($price_grade as $key=>$val)
{
$temp_key = $key + 1;
$price_grade[$temp_key]['goods_num'] = $val['goods_num'];
$price_grade[$temp_key]['start'] = $row['min'] + round($dx * $val['sn']);
$price_grade[$temp_key]['end'] = $row['min'] + round($dx * ($val['sn'] + 1));
$price_grade[$temp_key]['price_range'] = $price_grade[$temp_key]['start'] . ' - ' . $price_grade[$temp_key]['end'];
$price_grade[$temp_key]['formated_start'] = price_format($price_grade[$temp_key]['start']);
$price_grade[$temp_key]['formated_end'] = price_format($price_grade[$temp_key]['end']);
$price_grade[$temp_key]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_grade[$temp_key]['start'], 'price_max'=> $price_grade[$temp_key]['end'], 'filter_attr'=>$filter_attr_str), $cat['cat_name']);
if (isset($_REQUEST['price_min']) && $price_grade[$temp_key]['start'] == $price_min && $price_grade[$temp_key]['end'] == $price_max)
{
$price_grade[$temp_key]['selected'] = 1;
}
else
{
$price_grade[$temp_key]['selected'] = 0;
}
}
$price_grade[0]['start'] = 0;
$price_grade[0]['end'] = 0;
$price_grade[0]['price_range'] = $_LANG['all_attribute'];
$price_grade[0]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>0, 'price_max'=> 0, 'filter_attr'=>$filter_attr_str), $cat['cat_name']);
$price_grade[0]['selected'] = empty($price_max) ? 1 : 0;
$smarty->assign('price_grade', $price_grade);
}
将它替换为
if($cat['grade_define'])
{
$price_grade=array();
$price_grade[0]['start'] = 0;
$price_grade[0]['end'] = 0;
$price_grade[0]['price_range'] = $_LANG['all_attribute'];
$price_grade[0]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand,
'price_min'=>0, 'price_max'=> 0, 'filter_attr'=>$filter_attr_str), $cat['cat_name']);
$price_grade[0]['selected'] = empty($price_max) ? 1 : 0;
$grade_temp=explode(",", $cat['grade_define']);
foreach($grade_temp as $grade_temp_key=>$grade_temp_val)
{
$grade_temp_key2=$grade_temp_key+1;
$grade_temp=explode("-", $grade_temp_val);
$price_grade[$grade_temp_key2]['start']=intval($grade_temp[0]);
$price_grade[$grade_temp_key2]['end']=$grade_temp[1] ?
intval($grade_temp[1]) : "999999999";
$price_grade[$grade_temp_key2]['price_range'] = $grade_temp_val;
$price_grade[$grade_temp_key2]['url'] = build_uri('category', array('cid'=>$cat_id,
'bid'=>$brand, 'price_min'=>$price_grade[$grade_temp_key2]['start'],
'price_max'=> $price_grade[$grade_temp_key2]['end'],
'filter_attr'=>$filter_attr_str), $cat['cat_name']);
if (isset($_REQUEST['price_min'])
&& $price_grade[$grade_temp_key2]['start'] == $price_min
&& $price_grade[$grade_temp_key2]['end'] == $price_max)
{
$price_grade[$grade_temp_key2]['selected'] = 1;
}
else
{
$price_grade[$grade_temp_key2]['selected'] = 0;
}
}
$smarty->assign('price_grade', $price_grade);
}
最后一步,找到
return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc, style, grade, filter_attr, parent_id FROM ' . $GLOBALS['ecs']->table('category') .
" WHERE cat_id = '$cat_id'");
将之修改为
return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc, style, grade, grade_define, filter_attr, parent_id FROM ' . $GLOBALS['ecs']->table('category') .
" WHERE cat_id = '$cat_id'");