效果如图:
第一步: 在/thmes/你的模板/library/
新建一个库文件: cart_info.lbi
代码:
<meta http-equiv="Content-Type" content="text/html; charset=gbk"> <!-- {if $goods} --> <div class="cat_ul" onMouseOver="this.className='cat_ul on'" onMouseOut="this.className='cat_ul'"> <div class="clearfix divt"> <p class="clearfix ptt"> <span class="cart_sp"><em class="left">{$goods_number} 件</em><em class="right"> ¥{$order_amount} 元</em></span><a href="flow.php" class="js">去结算</a> </p></div> <ul class="cart_box clearfix"> {foreach from=$goods item=goods name=goods key=key} <li {if $key%2 neq 0}class="clearfix nobk"{else} class="clearfix"{/if}> <span class="sgood left"><a href="{$goods.url}" ><img src="{$goods.goods_thumb}" alt="{$goods.goods_name}"></a></span> <p class="left sgoodc"><a class="name" href="{$goods.url}">{$goods.short_name}</a> <span>{$goods.goods_jj}</span> </p> <p class="right sgoodt"> <span>{$goods.goods_price}×{$goods.goods_number}</span><a class="del" href="javascript:" onClick="deleteCartGoods({$goods.rec_id})">[删除]</a></p> </li> {/foreach} <li class="clearfix zllcart"> <span class="ie6left">共 <em>{$goods_number}</em> 件 <span class="pipe">|</span> 价格总计:<em> ¥{$order_amount} 元</em></span><a href="flow.php" class="right">去购物车结算>></a> </li> </ul> </div> <!-- {else} --> <ul class="car_ul onp clearfix"> <p class="clearfix ptt">暂无商品</p> <ul> <!-- {/if} -->
第二步打开: includes/lib_insert.php
大约102行 代码
function insert_cart_info() { $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount' . ' FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->GetRow($sql); if ($row) { $number = intval($row['number']); $amount = floatval($row['amount']); } else { $number = 0; $amount = 0; } $str = sprintf($GLOBALS['_LANG']['cart_info'], $number, price_format($amount, false)); return '<a href="flow.php" title="' . $GLOBALS['_LANG']['view_cart'] . '">' . $str . '</a>'; }
替换为如下代码
function insert_cart_info() { $sql = 'SELECT c.*,g.goods_name,g.goods_thumb,g.goods_id,c.goods_number,c.goods_price' . ' FROM ' . $GLOBALS['ecs']->table('cart') ." AS c ". " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id=c.goods_id ". " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->GetAll($sql); $arr = array(); foreach($row AS $k=>$v) { $arr[$k]['goods_thumb'] =get_image_path($v['goods_id'], $v['goods_thumb'], true); $arr[$k]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($v['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $v['goods_name']; $arr[$k]['url'] = build_uri('goods', array('gid' => $v['goods_id']), $v['goods_name']); $arr[$k]['goods_number'] = $v['goods_number']; $arr[$k]['goods_name'] = $v['goods_name']; $arr[$k]['goods_price'] = price_format($v['goods_price']); $arr[$k]['rec_id'] = $v['rec_id']; } $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount' . ' FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->GetRow($sql); if ($row) { $number = intval($row['number']); $amount = floatval($row['amount']); } else { $number = 0; $amount = 0; } $GLOBALS['smarty']->assign('str',sprintf($GLOBALS['_LANG']['cart_info'], $number, price_format($amount, false))); $GLOBALS['smarty']->assign('goods',$arr); $GLOBALS['smarty']->assign('goods_number',$number); $GLOBALS['smarty']->assign('order_amount',$amount); $output = $GLOBALS['smarty']->fetch('library/cart_info.lbi'); return $output; }
第三步: 打开js/common.js
最后面添加:
// 自定义购物车删除 function deleteCartGoods(rec_id) { Ajax.call('delete_cart_goods.php', 'id='+rec_id, deleteCartGoodsResponse, 'POST', 'JSON'); } /** * 接收删除后返回的信息 */ function deleteCartGoodsResponse(res) { if (res.error) { alert(res.err_msg); } else { document.getElementById('ECS_CARTINFO').innerHTML = res.content; } }
第四步: 打开 thmes/你的模板/style.css
最后添加:
/*购物车*/ .cart{ position:absolute; z-index:9px; right:0px; _right:-5px; top:4px; width:190px; padding-right:5px; height:26px; line-height:26px; } .cart a.js{ text-indent:-999px; overflow:hidden; display:inline-block; height:26px; float:right; width:38px;} .cart_sp { display:block; float:left; height:26px; line-height:26px; width:150px;} .cart_sp .right {_width:70px; padding-right:10px; text-align:right;} .cart_sp .left {_width:70px;text-indent:30px;} .cart_box { display:none;} .on .cart_box { display:block; background:#fff; border:1px solid #ddd; width:300px; margin:5px 0 0 -106px; _margin:5px -8px 0 0;} .ie6left {_float:left;} .ptt {background:url(images/suning.png) no-repeat 0 -80px;} .onp .ptt {text-indent:30px; } .on .ptt,.onp .ptt {_float:right; _width:195px; background-position:0px -80px;} .divt {_width:300px;} .cart_box li { height:auto; text-indent:0px; padding:8px; color:#777; border-bottom:1px dashed #dedede; _height:48px;} .cart_box li.nobk { background:#fafafa;} .cart_box li p.sgoodc { width:163px; overflow:hidden;} .cart_box li p.sgoodc span,.cart_box li p.sgoodt span,.cart_box li p a.del{ display:block; height:18px; line-height:18px; overflow:hidden;} .cart_box li p.sgoodt { width:65px; float:right; text-align:right; overflow:hidden; color:#ff6600;} .cart_box li p a.name { color:#333; text-decoration:none; height:22px; overflow:hidden;} .cart_box li p a:hover.name,.zllcart a:hover.right { text-decoration:underline;} .cart_box li p a.del { color:#777; text-decoration:none; margin-top:5px; } .cart_box li p a:hover.del { color:#333; text-decoration:line-through;} .cart_box li.zllcart { padding:0 8px; border-bottom:0;_height:24px;} .zllcart em,.zllcart a.right { color:#ff6600;text-decoration:none;}
第五步,在根目录 新建一个delete_cart_goods.php文件
<?php define('IN_ECS', true); require(dirname(__FILE__) . '/includes/init.php'); include_once('includes/cls_json.php'); $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => ''); $json = new JSON; if($_POST['id']) { $sql = 'DELETE FROM '.$GLOBALS['ecs']->table('cart')." WHERE rec_id=".$_POST['id']; $GLOBALS['db']->query($sql); } $sql = 'SELECT c.*,g.goods_name,g.goods_thumb,g.goods_id,c.goods_number,c.goods_price' . ' FROM ' . $GLOBALS['ecs']->table('cart') ." AS c ". " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id=c.goods_id ". " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->GetAll($sql); $arr = array(); foreach($row AS $k=>$v) { $arr[$k]['goods_thumb'] =get_image_path($v['goods_id'], $v['goods_thumb'], true); $arr[$k]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($v['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $v['goods_name']; $arr[$k]['url'] = build_uri('goods', array('gid' => $v['goods_id']), $v['goods_name']); $arr[$k]['goods_number'] = $v['goods_number']; $arr[$k]['goods_name'] = $v['goods_name']; $arr[$k]['goods_price'] = price_format($v['goods_price']); $arr[$k]['rec_id'] = $v['rec_id']; } $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount' . ' FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->GetRow($sql); if ($row) { $number = intval($row['number']); $amount = floatval($row['amount']); } else { $number = 0; $amount = 0; } $GLOBALS['smarty']->assign('str',sprintf($GLOBALS['_LANG']['cart_info'], $number, price_format($amount, false))); $GLOBALS['smarty']->assign('goods',$arr); $GLOBALS['smarty']->assign('goods_number',$number); $GLOBALS['smarty']->assign('order_amount',$amount); $result['content'] = $GLOBALS['smarty']->fetch('library/cart_info.lbi'); die($json->encode($result)); ?>
使用下面的代码即可全站调用购物车:
{insert_scripts files='transport.js'} <div class="cart" id="ECS_CARTINFO"> {insert name='cart_info'}
更新缓存即可!