函数保存在include/lib_goods.php文件,调用了ecshop自带的函数get_children($cat_id)调用指定分类下所有子分类的id,该函数在文件include/lib_common.php文件内;
具体代码如下:
/** * 获得指定分类下的推荐商品 * sun04zh3-20130321 * @access public * @param string $type 推荐类型,可以是 best, new, hot, promote * @param string $cats 分类的ID * @param integer $num 显示的产品数量 * @return array */ function get_category_recommend_goods_cat_id($type = '',$cat_id, $num) { $sql = 'Select g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price, g.promote_price, ' . "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, " . "g.is_best, g.is_new, g.is_hot, g.is_promote " . 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . "Where g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND ".get_children($cat_id); switch ($type) { case 'best': $sql .= ' AND g.is_best = 1'; break; case 'new': $sql .= ' AND g.is_new = 1'; break; case 'hot': $sql .= ' AND g.is_hot = 1'; break; case 'promote': $time = gmtime(); $sql .= " AND is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time'"; break; } $sql .=" LIMIT $num"; $res = $GLOBALS['db']->getAll($sql); $goods = array(); foreach ($res AS $idx => $row) { $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]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']); $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]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']); $goods[$idx]['market_price'] = price_format($row['market_price']); $goods[$idx]['shop_price'] = price_format($row['shop_price']); $goods[$idx]['thumb'] = empty($row['goods_thumb']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_thumb']; $goods[$idx]['goods_img'] = empty($row['goods_img']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_img']; $goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']); } return $goods; }
gategory.php文件调用:
$smarty->assign('best_goods_rmb', get_category_recommend_goods_cat_id('best',8 ,8));
gategory.dwt文件调用:
<meta http-equiv="Content-Type" content="text/html; charset=GBK"> <!-- {if $best_goods_rmb} --> <!--{foreach from=$best_goods_rmb item=rmb}--> <li><a href="{$rmb.url}"><img src="{$rmb.thumb}" alt="{$rmb.name|escape:html}" width="150" height="123" /></a><p><a href="{$rmb.url}" title="{$rmb.name|escape:html}">{$rmb.short_style_name}</a></p><em><!-- {if $rmb.promote_price neq ""} -->{$rmb.promote_price}<!-- {else}-->{$rmb.shop_price}<!--{/if}--></em></li> <!--{/foreach}--> <!-- {/if} -->