前提:价格阶梯只能设置一级
需要用到: jquery,transport.js(transport_jquery.js),Ajax.call
html页面
js代码,还需要插入jquery,transport.js(支持ajax插件的)文件
/** * 点选可选属性或改变数量时修改商品价格的函数 */ var goodsId = {$group_buy.goods_id}; // 参加团购产品的id,需要获取团购设置的初始价格 var act_id = {$group_buy.act_id}; // 扩展类别属性 function changePrice() { var attr = getSelectedAttributes(document.forms['ECS_FORMBUY']); var qty = document.forms['ECS_FORMBUY'].elements['number'].value; //alert('act=price&id=' + goodsId +'&act_id='+ act_id + '&attr=' + attr + '&number='); Ajax.call('group_buy.php', 'act=price&id=' + goodsId +'&act_id='+ act_id + '&attr=' + attr + '&number=' + qty, changePriceResponse, 'GET', 'JSON'); } /** * 接收返回的信息 */ function changePriceResponse(res) { if (res.err_msg.length > 0) { alert(res.err_msg); } else { document.forms['ECS_FORMBUY'].elements['number'].value = res.qty; if (document.getElementById('ECS_GOODS_AMOUNT')) document.getElementById('ECS_GOODS_AMOUNT').innerHTML = res.result; //alert(res.result); } } /*新增 商品点击效果*/ function changeAtt(t,src,key) { //alert(key); document.getElementById('spec_value_'+src).checked='checked'; div = document.getElementById("EXP_"+key); elems = div.getElementsByTagName("dl")[0].getElementsByTagName("a"); for(i=0;i<elems.length;i++){ elems[i].className=''; } t.className = "on"; //elems[t].getElementsByTagName("span")[0].style.display="block"; changePrice(); }
php文件 group_buy.php?act=price ,另一个是扩展属性(价格)function group_attr(){}
/*------------------------------------------------------ */ //-- 改变属性、数量时重新计算商品价格 /*------------------------------------------------------ */ elseif (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'price') { include('includes/cls_json.php'); $goods_id = $_GET['id']; $act_id = $_GET['act_id']; $attr = $_GET['attr']; $attr = explode(',',$_GET['attr']); // 注意这里传递过来的值,有可能是1个值或者2个值或者3个值,具体的要看扩展属性有几个阶梯 foreach($attr as $v){ $attr_price[] = group_attr($v); } $total_attr_price = array_sum($attr_price); // 所有扩展分类的总价格 $json = new JSON; $res = array('err_msg' => '', 'result' => '', 'qty' => 1); $attr_id = isset($_REQUEST['attr']) ? explode(',', $_REQUEST['attr']) : array(); $number = (isset($_REQUEST['number'])) ? intval($_REQUEST['number']) : 1; if ($goods_id == 0) { $res['err_msg'] = $_LANG['err_change_attr']; $res['err_no'] = 1; } else { if ($number == 0) { $res['qty'] = $number = 1; } else { $res['qty'] = $number; } // $shop_price = get_final_price($goods_id, $number, true, $attr_id); $group_info = group_buy_info($act_id); // 获得团购的最终价格 $shop_price = $group_info['cur_price']+$total_attr_price; $res['result'] = price_format($shop_price * $number); } die($json->encode($res)); }
// 获取某个商品的扩展属性价格 function group_attr($goods_attr_id){ $sql = 'select attr_price from '.$GLOBALS['ecs']->table('goods_attr').' where goods_attr_id='.$goods_attr_id; $result = $GLOBALS['db']->getOne($sql); return floatval($result ); }
ecshop产品详情,默认改价
html
<script type="text/javascript"> var goods_id = {$goods_id}; var goodsattr_style = {$cfg.goodsattr_style|default:1}; var gmt_end_time = {$promote_end_time|default:0}; {foreach from=$lang.goods_js item=item key=key} var {$key} = "{$item}"; {/foreach} var goodsId = {$goods_id}; var now_time = {$now_time}; <!-- {literal} --> onload = function(){ changePrice(); fixpng(); try { onload_leftTime(); } catch (e) {} } /** * 点选可选属性或改变数量时修改商品价格的函数 */ function changePrice() { var attr = getSelectedAttributes(document.forms['ECS_FORMBUY']); var qty = document.forms['ECS_FORMBUY'].elements['number'].value; Ajax.call('goods.php', 'act=price&id=' + goodsId + '&attr=' + attr + '&number=' + qty, changePriceResponse, 'GET', 'JSON'); } /** * 接收返回的信息 */ function changePriceResponse(res) { if (res.err_msg.length > 0) { alert(res.err_msg); } else { document.forms['ECS_FORMBUY'].elements['number'].value = res.qty; if (document.getElementById('ECS_GOODS_AMOUNT')) document.getElementById('ECS_GOODS_AMOUNT').innerHTML = res.result; } } <!-- {/literal} --> /*新增 商品点击效果*/ function changeAtt(t,src,key) { //alert(key); document.getElementById('spec_value_'+src).checked='checked'; div = document.getElementById("EXP_"+key); elems = div.getElementsByTagName("dl")[0].getElementsByTagName("a"); for(i=0;i<elems.length;i++){ elems[i].className=''; } t.className = "on"; //elems[t].getElementsByTagName("span")[0].style.display="block"; changePrice(); }
php代码
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'price') { include('includes/cls_json.php'); $json = new JSON; $res = array('err_msg' => '', 'result' => '', 'qty' => 1); $attr_id = isset($_REQUEST['attr']) ? explode(',', $_REQUEST['attr']) : array(); $number = (isset($_REQUEST['number'])) ? intval($_REQUEST['number']) : 1; if ($goods_id == 0) { $res['err_msg'] = $_LANG['err_change_attr']; $res['err_no'] = 1; } else { if ($number == 0) { $res['qty'] = $number = 1; } else { $res['qty'] = $number; } $shop_price = get_final_price($goods_id, $number, true, $attr_id); $res['result'] = price_format($shop_price * $number); } die($json->encode($res)); }
总结:
1.要想点击价格有变动,ajax.all 必须得可以调取到值。
2.给一个团购的id,或者到参加团购商品的团购价格、或者属性
3.有了团购价格,再来获取点击扩展属性,获取扩展属性价格
4.团购价格、扩展属性价格都齐全了,利用ecshop本身的js功能function changePriceResponse 给某个id="ECS_GOODS_AMOUNT">{$group_buy.formated_cur_price}</div>值