ecshop产品分类页调用当前分类排行榜

2016-09-07 22:03 来源:www.chinab4c.com 作者:ecshop专家

category.php

添加函数:

 


/**
* 调用产品分类的销售排行榜
*
* @access  public
* @param   string  $cats   查询的分类
* @return  array
*/
function get_pro_top10($cats = '1')
{
    $where = !empty($cats) ? "AND (g.cat_id = $cats OR " . get_extension_goods($cats) . ") " : '';
    /* 排行统计的时间 */
    switch ($GLOBALS['_CFG']['top10_time'])
    {
        case 1: // 一年
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 365 * 86400) . "'";
        break;
        case 2: // 半年
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 180 * 86400) . "'";
        break;
        case 3: // 三个月
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 90 * 86400) . "'";
        break;
        case 4: // 一个月
            $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 30 * 86400) . "'";
        break;
        default:
            $top10_time = '';
    }
    $sql = 'SELECT g.goods_id, g.goods_name, g.shop_price, g.goods_thumb, SUM(og.goods_number) as goods_number ' .
           'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g, ' .
                $GLOBALS['ecs']->table('order_info') . ' AS o, ' .
                $GLOBALS['ecs']->table('order_goods') . ' AS og ' .
           "WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 $where $top10_time " ;
    //判断是否启用库存,库存数量是否大于0
    if ($GLOBALS['_CFG']['use_storage'] == 1)
    {
        $sql .= " AND g.goods_number > 0 ";
    }
    $sql .= ' AND og.order_id = o.order_id AND og.goods_id = g.goods_id ' .
           "AND o.order_status = '" . OS_CONFIRMED . "' " .
           "AND (o.pay_status = '" . PS_PAYED . "' OR o.pay_status = '" . PS_PAYING . "') " .
           "AND (o.shipping_status = '" . SS_SHIPPED . "' OR o.shipping_status = '" . SS_RECEIVED . "') " .
           'GROUP BY g.goods_id ORDER BY goods_number DESC, g.goods_id DESC LIMIT ' . $GLOBALS['_CFG']['top_number'];
    $arr = $GLOBALS['db']->getAll($sql);
    for ($i = 0, $count = count($arr); $i < $count; $i++)
    {
        $arr[$i]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                    sub_str($arr[$i]['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $arr[$i]['goods_name'];
        $arr[$i]['url']        = build_uri('goods', array('gid' => $arr[$i]['goods_id']), $arr[$i]['goods_name']);
        $arr[$i]['thumb'] = get_image_path($arr[$i]['goods_id'], $arr[$i]['goods_thumb'],true);
        $arr[$i]['price'] = price_format($arr[$i]['shop_price']);
    }
    return $arr;
}

 

查找

 

$smarty->assign('top_goods',        get_top10());                  // 销售排行

 

下面
增加:
$smarty->assign('get_pro_top',        get_pro_top10($cat_id));                  // 销售排行

模板调用:
默认模板

 

<div class="box">
<div class="box_2">
  <div class="top10Tit"></div>
  <div class="top10List clearfix">
  <!-- {foreach name=top_goods from=$get_pro_top item=goods}-->
  <ul class="clearfix">
<img src="../images/top_{$smarty.foreach.top_goods.iteration}.gif" class="iteration" />
<!-- {if $smarty.foreach.top_goods.iteration<4}-->
      <li class="topimg">
      <a href="{$goods.url}"><img src="{$goods.thumb}" alt="{$goods.name|escape:html}" class="samllimg" /></a>
      </li>
<!-- {/if} --> 
      <li {if $smarty.foreach.top_goods.iteration<4}class="iteration1"{/if}>
      <a href="{$goods.url}" title="{$goods.name|escape:html}">{$goods.short_name}</a><br />
      {$lang.shop_price}<font class="f1">{$goods.price}</font><br />
      </li>
    </ul>
  <!-- {/foreach} -->
  </div>
</div>
</div>
<div class="blank5"></div>

(责任编辑:chinab4c)