
时间:2019-09-05 编辑:lexiaoyao
大家都知道ECSHOP首页的推荐、热卖和新品三个板块的商品都是从所有商品里读取出来的。如果我想“在ecshop首页调用指定分类的推荐、热卖和新品?”该怎么办呢,下面教你一种方法:
找到ecshop的includes/lib_goods.php,
把SQL语句改一下,与category表关联即可
将
$sql = 'SELECT g.goods_id,g.goods_name, g.goods_name_style,g.market_price, g.shop_price AS org_price, g.promote_price, ' .
修改为
$sql = 'SELECT g.goods_id,g.cat_id,c.parent_id,g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
继续找到
'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .在它下面加一句
'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON c.cat_id = g.cat_id ' .然后将
if (!empty($cats)){
$sql .= " AND (" . $cats . " OR " . get_extension_goods($cats) .")";}改为
if (!empty($cats)){
$sql .= " AND (c.parent_id =" . $cats. " OR " . get_extension_goods($cats) .")";}这个是和分类表建立关联,调出商品所在分类的上级分类
然后在index.php中增加下面代码,这样写是会不显示的
$smarty->assign('chot_goods_372', get_category_recommend_goods('hot','372')); //指定分类下的热销商品 应该这样写
$smarty->assign('chot_goods_372', get_category_recommend_goods('hot',372)); //指定分类下的热销商品注意这个372是一级分类的ID,然后在模板中调用即可
includes/lib_goods.php,index.php这两个文件都可以写
/**
* 获得指定分类下的推荐商品
*
* @access public
* @param string $type 推荐类型,可以是 best, new, hot, promote
* @param string $cats 分类的ID
* @param integer $brand 品牌的ID
* @param integer $min 商品价格下限
* @param integer $max 商品价格上限
* @param string $ext 商品扩展查询
* @return array
*/
function get_category_recommend_goods2($type = '', $cats = '', $brand = 0, $min =0, $max = 0, $ext='')
{
$brand_where = ($brand > 0) ? " AND g.brand_id = '$brand'" : '';
$price_where = ($min > 0) ? " AND g.shop_price >= $min " : '';
$price_where .= ($max > 0) ? " AND g.shop_price <= $max " : '';
// $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
// "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
// 'promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name ' .
// 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
// 'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
// "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
// "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
// 'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' . $brand_where . $price_where . $ext;
$sql = 'SELECT g.goods_id,g.cat_id,c.parent_id,g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
'promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name ' .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON c.cat_id = g.cat_id ' .
"LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
"ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' . $brand_where . $price_where . $ext;
// echo $sql;
// exit;
$num = 0;
$type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot', 'promote'=>'recommend_promotion');
$num = get_library_number($type2lib[$type]);
switch ($type)
{
case 'best':
$sql .= ' AND is_best = 1';
break;
case 'new':
$sql .= ' AND is_new = 1';
break;
case 'hot':
$sql .= ' AND is_hot = 1';
break;
case 'promote':
$time = gmtime();
$sql .= " AND is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time'";
break;
}
// if (!empty($cats))
// {
// $sql .= " AND (" . $cats . " OR " . get_extension_goods($cats) .")";
// }
if (!empty($cats))
{
$sql .= " AND (c.parent_id =" . $cats. " OR " . get_extension_goods($cats) .")";
}
$order_type = $GLOBALS['_CFG']['recommend_order'];
$sql .= ($order_type == 0) ? ' ORDER BY g.sort_order, g.last_update DESC' : ' ORDER BY RAND()';
$res = $GLOBALS['db']->selectLimit($sql, $num);
$idx = 0;
$goods = array();
while ($row = $GLOBALS['db']->fetchRow($res))
{
if ($row['promote_price'] > 0)
{
$promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
}
else
{
$goods[$idx]['promote_price'] = '';
}
$goods[$idx]['id'] = $row['goods_id'];
$goods[$idx]['name'] = $row['goods_name'];
$goods[$idx]['brief'] = $row['goods_brief'];
$goods[$idx]['brand_name'] = $row['brand_name'];
$goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$goods[$idx]['market_price'] = price_format($row['market_price']);
$goods[$idx]['shop_price'] = price_format($row['shop_price']);
$goods[$idx]['thumb'] = get_pc_url().'/'.get_image_path($row['goods_id'], $row['goods_thumb'], true);
$goods[$idx]['goods_img'] = get_pc_url().'/'.get_image_path($row['goods_id'], $row['goods_img']);
$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'], $row['goods_name_style']);
$idx++;
}
return $goods;
}index.dwt
<li class="li_list">
{foreach from=$chot_goods_5 item=goods}
<div class="index_pro1">
<div class="products_kuang1">
<a href="{$goods.url}" title="{$goods.name|escape:html}" target="_blank">
<img src="{$goods.thumb}" alt="{$goods.name|escape:html}">
</a>
</div>
<div class="goods_name1">
<a href="{$goods.url}" target="_blank" title="{$goods.name|escape:html}">{$goods.name|escape:html}</a>
</div>
<div class="price1">
<a href="javascript:addToCart({$goods.id})" class="btns1">
购物车
</a>
<span>{$goods.shop_price}</span>
<em>{$goods.market_price}</em>
</div>
</div>
{/foreach}
</li>同理,新品,推荐都可以调,只要把hot改为new 或者best就可以了