ECSHOP全站调用购物车显示商品图片,价格,数量,总价等详细信息

2016-07-07 16:02 来源:www.chinab4c.com 作者:ecshop专家



参考: http://bbs.ecshop.com/viewthread.php?tid=154246&highlight=%B9%BA%CE%EF%B3%B5

将步骤重新梳理了一下:

支持原创,感谢分享!

效果图:

ecshop.jpg

第一步: 在/thmes/你的模板/library/
新建一个库文件:cart_info.lbi
代码:
  1. <!-- {if $goods} -->
  2. <div class="cat_ul" onMouseOver="this.className='cat_ul on'" onMouseOut="this.className='cat_ul'">
  3. <div class="clearfix divt">
  4. <p class="clearfix ptt">
  5. <span class="cart_sp"><em class="left">{$goods_number} 件</em><em class="right"> ¥{$order_amount} 元</em></span><a href="flow.php" class="js">去结算</a>
  6. </p></div>
  7. <ul class="cart_box clearfix">
  8. {foreach from=$goods item=goods name=goods key=key}
  9. <li {if $key%2 neq 0}class="clearfix nobk"{else} class="clearfix"{/if}>
  10. <span class="sgood left"><a href="{$goods.url}" ><img src="{$goods.goods_thumb}" alt="{$goods.goods_name}"></a></span>
  11. <p class="left sgoodc"><a class="name" href="{$goods.url}">{$goods.short_name}</a>
  12. <span>{$goods.goods_jj}</span>
  13. </p>
  14. <p class="right sgoodt">
  15. <span>{$goods.goods_price}×{$goods.goods_number}</span><a class="del" href="javascript:" onClick="deleteCartGoods({$goods.rec_id})">[删除]</a></p>
  16. </li>
  17. {/foreach}
  18. <li class="clearfix zllcart">
  19. <span class="ie6left">共 <em>{$goods_number}</em> 件 <span class="pipe">|</span> 价格总计:<em> ¥{$order_amount} 元</em></span><a href="flow.php" class="right">去购物车结算>></a>
  20. </li>
  21. </ul>
  22. </div>
  23. <!-- {else} -->
  24. <ul class="car_ul onp clearfix">
  25. <p class="clearfix ptt">暂无商品</p>
  26. <ul>
  27. <!-- {/if} -->
复制代码


文件下载: cart_info.rar (715 Bytes)

第二步打开: includes/lib_insert.php
大约102行
  1. function insert_cart_info() {
复制代码


一直到:

  1. /**
  2. * 调用指定的广告位的广告
复制代码


之间的代码全部替换为:

  1. /**
  2. * 调用购物车信息
  3. *
  4. * @accesspublic
  5. * @returnstring
  6. */
  7. function insert_cart_info()
  8. {
  9. $sql = 'SELECT c.*,g.goods_name,g.goods_thumb,g.goods_id,c.goods_number,c.goods_price' .
  10. ' FROM ' . $GLOBALS['ecs']->table('cart') ." AS c ".
  11. " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id=c.goods_id ".
  12. " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'";
  13. $row = $GLOBALS['db']->GetAll($sql);
  14. $arr = array();
  15. foreach($row AS $k=>$v)
  16. {
  17. $arr[$k]['goods_thumb']=get_image_path($v['goods_id'], $v['goods_thumb'], true);
  18. $arr[$k]['short_name']= $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  19. sub_str($v['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $v['goods_name'];
  20. $arr[$k]['url'] = build_uri('goods', array('gid' => $v['goods_id']), $v['goods_name']);
  21. $arr[$k]['goods_number'] = $v['goods_number'];
  22. $arr[$k]['goods_name']= $v['goods_name'];
  23. $arr[$k]['goods_price']= price_format($v['goods_price']);
  24. $arr[$k]['rec_id'] = $v['rec_id'];
  25. }
  26. $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount' .
  27. ' FROM ' . $GLOBALS['ecs']->table('cart') .
  28. " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'";
  29. $row = $GLOBALS['db']->GetRow($sql);
  30. if ($row)
  31. {
  32. $number = intval($row['number']);
  33. $amount = floatval($row['amount']);
  34. }
  35. else
  36. {
  37. $number = 0;
  38. $amount = 0;
  39. }
  40. $GLOBALS['smarty']->assign('str',sprintf($GLOBALS['_LANG']['cart_info'], $number, price_format($amount, false)));
  41. $GLOBALS['smarty']->assign('goods',$arr);
  42. $GLOBALS['smarty']->assign('goods_number',$number);
  43. $GLOBALS['smarty']->assign('order_amount',$amount);
  44. $output = $GLOBALS['smarty']->fetch('library/cart_info.lbi');
  45. return $output;
  46. }
复制代码


第三步: 打开js/common.js
最后面添加:
  1. // 自定义购物车删除
  2. function deleteCartGoods(rec_id)
  3. {
  4. Ajax.call('delete_cart_goods.php', 'id='+rec_id, deleteCartGoodsResp**e, 'POST', 'JSON');
  5. }
  6. /**
  7. * 接收删除后返回的信息
  8. */
  9. function deleteCartGoodsResp**e(res)
  10. {
  11. if (res.error)
  12. {
  13. alert(res.err_msg);
  14. }
  15. else
  16. {
  17. document.getElementById('ECS_CARTINFO').innerHTML = res.content;
  18. }
  19. }
复制代码

其中的星号**替换为o+n+s 中间的+号去掉,这三个字母是被屏蔽的.
第四步: 打开 thmes/你的模板/style.css
最后添加:
  1. /*购物车*/
  2. .cart{ position:absolute; z-index:9px; right:0px; _right:-5px; top:4px; width:190px; padding-right:5px; height:26px; line-height:26px;
  3. }
  4. .cart a.js{ text-indent:-999px; overflow:hidden; display:inline-block; height:26px; float:right; width:38px;}
  5. .cart_sp { display:block; float:left; height:26px; line-height:26px; width:150px;}
  6. .cart_sp .right {_width:70px; padding-right:10px;text-align:right;}
  7. .cart_sp .left {_width:70px;text-indent:30px;}
  8. .cart_box { display:none;}
  9. .on .cart_box { display:block; background:#fff; border:1px solid #ddd; width:300px; margin:5px 0 0 -106px; _margin:5px -8px 0 0;}
  10. .ie6left {_float:left;}
  11. .ptt {background:url(images/suning.png) no-repeat 0 -80px;}
  12. .onp .ptt {text-indent:30px; }
  13. .on .ptt,.onp .ptt {_float:right; _width:195px; background-position:0px -80px;}
  14. .divt {_width:300px;}
  15. .cart_box li { height:auto; text-indent:0px; padding:8px; color:#777; border-bottom:1px dashed #dedede; _height:48px;}
  16. .cart_box li.nobk { background:#fafafa;}
  17. .cart_box li p.sgoodc { width:163px; overflow:hidden;}
  18. .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;}
  19. .cart_box li p.sgoodt { width:65px; float:right; text-align:right; overflow:hidden; color:#ff6600;}
  20. .cart_box li p a.name { color:#333; text-decoration:none; height:22px; overflow:hidden;}
  21. .cart_box li p a:hover.name,.zllcart a:hover.right { text-decoration:underline;}
  22. .cart_box li p a.del { color:#777; text-decoration:none; margin-top:5px; }
  23. .cart_box li p a:hover.del { color:#333; text-decoration:line-through;}
  24. .cart_box li.zllcart { padding:0 8px; border-bottom:0;_height:24px;}
  25. .zllcart em,.zllcart a.right { color:#ff6600;text-decoration:none;}
复制代码

第五步,在根目录 新建一个delete_cart_goods.php文件,
  1. <?php
  2. define('IN_ECS', true);
  3. require(dirname(__FILE__) . '/includes/init.php');
  4. include_once('includes/cls_json.php');
  5. $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
  6. $json= new JSON;
  7. if($_POST['id'])
  8. {
  9. $sql = 'DELETE FROM '.$GLOBALS['ecs']->table('cart')." WHERE rec_id=".$_POST['id'];
  10. $GLOBALS['db']->query($sql);
  11. }
  12. $sql = 'SELECT c.*,g.goods_name,g.goods_thumb,g.goods_id,c.goods_number,c.goods_price' .
  13. ' FROM ' . $GLOBALS['ecs']->table('cart') ." AS c ".
  14. " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id=c.goods_id ".
  15. " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'";
  16. $row = $GLOBALS['db']->GetAll($sql);
  17. $arr = array();
  18. foreach($row AS $k=>$v)
  19. {
  20. $arr[$k]['goods_thumb']=get_image_path($v['goods_id'], $v['goods_thumb'], true);
  21. $arr[$k]['short_name']= $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  22. sub_str($v['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $v['goods_name'];
  23. $arr[$k]['url'] = build_uri('goods', array('gid' => $v['goods_id']), $v['goods_name']);
  24. $arr[$k]['goods_number'] = $v['goods_number'];
  25. $arr[$k]['goods_name']= $v['goods_name'];
  26. $arr[$k]['goods_price']= price_format($v['goods_price']);
  27. $arr[$k]['rec_id'] = $v['rec_id'];
  28. }
  29. $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount' .
  30. ' FROM ' . $GLOBALS['ecs']->table('cart') .
  31. " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'";
  32. $row = $GLOBALS['db']->GetRow($sql);
  33. if ($row)
  34. {
  35. $number = intval($row['number']);
  36. $amount = floatval($row['amount']);
  37. }
  38. else
  39. {
  40. $number = 0;
  41. $amount = 0;
  42. }
  43. $GLOBALS['smarty']->assign('str',sprintf($GLOBALS['_LANG']['cart_info'], $number, price_format($amount, false)));
  44. $GLOBALS['smarty']->assign('goods',$arr);
  45. $GLOBALS['smarty']->assign('goods_number',$number);
  46. $GLOBALS['smarty']->assign('order_amount',$amount);
  47. $result['content'] = $GLOBALS['smarty']->fetch('library/cart_info.lbi');
  48. die($json->encode($result));
  49. ?>
复制代码


文件下载: delete_cart_goods.rar (940 Bytes)

使用下面的代码即可全站调用购物车:


  1. {insert_scripts files='transport.js'}
  2. <div class="cart" id="ECS_CARTINFO">
  3. {insert name='cart_info'}
复制代码

更新缓存即可!

回答:
很不错的经验贴,谢谢分享

好 很不错

支持了呢

谢谢了 用上来

谢谢啊,楼主大爱~

楼主大爱,谢谢啦

楼主niubility

楼主英明,大爱无疆

楼主斯嗷一戴斯 suaoyidesi

路过看起来很不错

谢谢楼主 这个CSS为什么写的这么复杂

支持共享