ecshop 详情页获得当前商品所属分类ID和调用同分类下的商品

我们ecshop模板中心在做ecshop二次开发时候有时候在goods.php中要用到当前分类的ID,下面由我们技术写一下实现步骤:在根目录goods.php中找到$smarty->assign(‘goods_rank’, get_goods_rank($goods_id));在这段下面加入

/*获得当前栏目id*/$sql= "select cat_id from ".$GLOBALS[‘ecs’]->table(‘goods’)."where goods_id =’".$goods_id."’"; $thiscat_id = $GLOBALS[‘db’]->getOne($sql);/*获得当前栏目id end*/栏目ID有了,接下来只需要根据ID取得商品就好了,打开includes/lib_goods.php在最下面加入,其实这个跟获取指定栏目下的商品是一样的代码,只不过这个ID是根据产品获取的,如果我们把id写死那就是指定分类下的商品了。代码如下:/*** 获得同分类下的商品** @access public* @param integer $cat_id 分类ID* @param integer $num 数量* @param string $from 来自web/wap的调用* @param string $order_rule 指定商品排序规则* @return array*/function samecat_goods($cat_id = ”, $num = ”){$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, " ."g.is_best, g.is_new, g.is_hot, g.is_promote " .’FROM ‘ . $GLOBALS[‘ecs’]->table(‘goods’) . ‘ AS g ‘ .’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 ".$sql .= " AND (c.parent_id =" . $cat_id. " OR g.cat_id = " . $cat_id ." OR g.cat_id ". db_create_in(array_unique(array_merge(array($cat_id), array_keys(cat_list($cat_id, 0, false))))) .")";$sql .= " ORDER BY rand()";$sql .= " LIMIT $num";$res = $GLOBALS[‘db’]->getAll($sql);$goods = array();foreach ($res AS $idx => $row){$goods[$idx][‘id’] = $row[‘article_id’];$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][‘goods_name’] = $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;}值得注意的是,代码中有这么一句$sql .= " ORDER BY rand()";这句是让商品随机排序,或者说是随机调用了几个商品,因为我们不想让同分类商品下面调出来的猜你还喜欢的商品都是一样的。如果你不想这样就把这句去掉就可以了。好了,获取代码都已经写好了,,接下来就是把这段代码引用到goods.php中,方法是在刚才获取当前栏目ID那段下面加入下面这句$smarty->assign(‘samecat_goods’, samecat_goods($thiscat_id,8)); $thiscat_id就是我们刚才写的获取的当前商品所属分类的id了,如果你把他换成固定值,比如10,那就是获得指定分类id为10的分类下面的商品了。如果你想在首页用那就把这一句写到index.php中。8为调用数量

去看日出,去散步,去欣赏大自然,

ecshop 详情页获得当前商品所属分类ID和调用同分类下的商品

相关文章:

你感兴趣的文章:

标签云: