欢迎访问 夜阑小雨 我的学习碎片档案,这里记录了我的学习内容和工作中经验,希望给您带去帮助。

ecshop 使用ajax更新购物车数量for 2.7

javascript 夜阑小雨 444℃ 0评论

1
 name=”goods_number[{$goods.rec_id}]”

 
 <input class=”TextInput” name=”goods_number[{$goods.rec_id}]” id=”goods_number_{$goods.rec_id}” value=”{$goods.goods_number}” type=”text” size=”4″ onkeydown=”showdiv(this)”/>

 
置换成… (依据个人使用的模组不同会有些许差异)

<input class=”TextInput” onblur=”changePrice(document.getElementById(‘goods_number_{$goods.rec_id}’).value,{$goods.rec_id})” name=”goods_number[{$goods.rec_id}]” id=”goods_number_{$goods.rec_id}” value=”{$goods.goods_number}” size=”4″ id=”ECS_FORMBUY”>
 
搜寻

<!– {if $step eq “cart”} –>
 
在下面加入

<script type=”text/javascript”>

/**


*/

function changePrice(number,rec_id)

{

//var attr = getSelectedAttributes(document.forms[‘ECS_FORMBUY’]);

// var qty = document.forms[‘ECS_FORMBUY’].elements[‘number’].value;

Ajax.call(‘flow.php’, ‘step=update_cart_ajax&rec_id=’ + rec_id +’&number=’ + number, changePriceResponse, ‘GET’, ‘JSON’);

}

/**

*/

function changePriceResponse(res)

{

document.getElementById(‘goods_number_’ + res.rec_id).value = res.goods_number;

if (res.error > 0)

{

alert(res.content);

}

else

{

document.getElementById(‘subtotal_’+res.rec_id).innerHTML = res.subtotal;//商

document.getElementById(‘cart_amount_desc’).innerHTML = res.cart_amount_desc;//

document.getElementById(‘market_amount_desc’).innerHTML = res.market_amount_desc;//购

 

 
//var cartInfo = document.getElementById(‘ECS_CARTINFO’);

//if (cartInfo)

//{

  //cartInfo.innerHTML = res.content;

//}

 

}

}

</script>
复制代码
搜寻

{$goods.subtotal}
复制代码
替换为

<div id=”subtotal_{$goods.rec_id}”>{$goods.subtotal}</div>
复制代码
搜寻

{$shopping_money}
复制代码
替换为

<span id=”cart_amount_desc”>{$shopping_money}</span>
复制代码
搜寻

{$market_price_desc}
复制代码
替换为

<span id=”market_amount_desc”>{$market_price_desc}</span>
复制代码
搜寻

{$lang.update_cart}
复制代码
会找到类似这段… (依据个人使用的模组不同会有些许差异)

<input name=”submit” type=”submit” class=”bnt_num6″ value=”{$lang.update_cart}” />
复制代码
将上面整段移除

搜寻

value=”update_cart”
复制代码
会找到类似这段… (依据个人使用的模组不同会有些许差异)

<input type=”hidden” name=”step” value=”update_cart” />
 
将上面整段移除

存档关闭後上传…

开启 flow.php

 

elseif ($_REQUEST[‘step’] == ‘update_cart’)
复制代码
在上面加入

elseif($_REQUEST[‘step’] == ‘update_cart_ajax’)

{

/*—————————————————— */

//– AJAX更新购物车

/*—————————————————— */

include_once(‘includes/cls_json.php’);

$json = new JSON();

$result = array(‘error’ => ”, ‘content’ => ”);

$rec_id = $_GET[‘rec_id’];

$number = $_GET[‘number’];

 

$goods_number = array($_GET[‘rec_id’] => $_GET[‘number’]);

if (isset($goods_number) && is_array($goods_number))

{

if(flow_update_cart_ajax($goods_number)){

die($json->encode(flow_update_cart_ajax($goods_number)));

}

}

/*   */

$cart_goods = get_cart_goods();

#

$subtotal = $GLOBALS[‘db’]->getONE(“select goods_price * goods_number AS subtotal from “.$GLOBALS[‘ecs’]->table(‘cart’).” where rec_id = $rec_id”);

#购物车商品总金额
$result[‘goods_number’] = $number;

$result[‘subtotal’] = price_format($subtotal, false);

$result[‘cart_amount_desc’] = sprintf($_LANG[‘shopping_money’], $cart_goods[‘total’][‘goods_price’]);

$result[‘market_amount_desc’] = sprintf($_LANG[‘than_market_price’], $cart_goods[‘total’][‘market_price’], $cart_goods[‘total’][‘saving’], $cart_goods[‘total’][‘save_rate’]

);

$result[‘rec_id’] = $rec_id;

//$result[‘content’] = insert_cart_info();    //

die($json->encode($result));

}
 
搜寻

function flow_update_cart($arr)
 
在上面加入

function flow_update_cart_ajax($arr)

{

foreach ($arr AS $key => $val)

{

$val = intval(make_semiangle($val));

$sql = “SELECT `goods_id`, `goods_attr_id`, `extension_code`,`goods_number` FROM” .$GLOBALS[‘ecs’]->table(‘cart’).

” WHERE rec_id=’$key’ AND session_id='” . SESS_ID . “‘”;

$goods = $GLOBALS[‘db’]->getRow($sql);

$sql = “SELECT g.goods_name, g.goods_number “.

“FROM ” .$GLOBALS[‘ecs’]->table(‘goods’). ” AS g, “.

$GLOBALS[‘ecs’]->table(‘cart’). ” AS c “.

“WHERE g.goods_id = c.goods_id AND c.rec_id = ‘$key'”;

$row = $GLOBALS[‘db’]->getRow($sql);

$result[‘error’] = ‘1’;

$result[‘rec_id’] = $key;

/* 数量是否小於0 */

if ($val <= 0)

{

$result[‘goods_number’] = $goods[‘goods_number’];

$result[‘content’] = $GLOBALS[‘_LANG’][‘goods_number_not_int’];

return $result;

}

/*  */

if (intval($GLOBALS[‘_CFG’][‘use_storage’]) > 0 && $goods[‘extension_code’] != ‘package_buy’)

{

if ($row[‘goods_number’] < $val)

{

$result[‘goods_number’] = $goods[‘goods_number’];

$result[‘content’] =sprintf($GLOBALS[‘_LANG’][‘stock_insufficiency’], $row[‘goods_name’],$row[‘goods_number’], $row[‘goods_number’]);

return $result;

}

}

elseif (intval($GLOBALS[‘_CFG’][‘use_storage’]) > 0 && $goods[‘extension_code’] == ‘package_buy’)

{

if (judge_package_stock($goods[‘goods_id’], $val))

{

$result[‘content’] =$GLOBALS[‘_LANG’][‘package_stock_insufficiency’];

return $result;

}

}

/*   */

$sql = “SELECT a.goods_number, a.rec_id FROM ” .$GLOBALS[‘ecs’]->table(‘cart’) . ” AS b “.

“LEFT JOIN ” . $GLOBALS[‘ecs’]->table(‘cart’) . ” AS a “.

“ON a.parent_id = b.goods_id AND a.session_id = ‘” . SESS_ID . “‘ AND a.extension_code <> ‘package_buy'”.

“WHERE b.rec_id = ‘$key'”;

$fittings = $GLOBALS[‘db’]->getAll($sql);

if ($val > 0)

{

foreach ($fittings AS $k => $v)

{

if ($v[‘goods_number’] != null && $v[‘rec_id’] != null)

{

/*   */

$num = ($v[‘goods_number’]) > $val ? $val : $v[‘goods_number’];

 

$sql = “UPDATE ” . $GLOBALS[‘ecs’]->table(‘cart’) .

” SET goods_number = ‘$num’ WHERE rec_id = $v[rec_id]”;

$GLOBALS[‘db’]->query($sql);

}

}

 

if ($goods[‘extension_code’] == ‘package_buy’)

{

/*   */

$sql = “UPDATE ” .$GLOBALS[‘ecs’]->table(‘cart’).

” SET goods_number = ‘$val’ WHERE rec_id=’$key’ AND session_id='” . SESS_ID . “‘”;

}

else

{

$attr_id = empty($goods[‘goods_attr_id’]) ? array() : explode(‘,’, $goods[‘goods_attr_id’]);

$goods_price = get_final_price($goods[‘goods_id’], $val, true, $attr_id);

 

/*   */

$sql = “UPDATE ” .$GLOBALS[‘ecs’]->table(‘cart’).

” SET goods_number = ‘$val’, goods_price = ‘$goods_price’ WHERE rec_id=’$key’ AND session_id='” . SESS_ID . “‘”;

}

}

else

{

if (is_object($fittings) && $fittings->goods_number != null && $fittings->rec_id != null)

{

$sql = “DELETE FROM ” . $GLOBALS[‘ecs’]->table(‘cart’). ” WHERE rec_id=$fittings[rec_id]”;

$GLOBALS[‘db’]->query($sql);

}

 

$sql = “DELETE FROM ” .$GLOBALS[‘ecs’]->table(‘cart’).

” WHERE rec_id=’$key’ AND session_id='” .SESS_ID. “‘”;

}

 

$GLOBALS[‘db’]->query($sql);

}

 

 
$sql = “DELETE FROM ” . $GLOBALS[‘ecs’]->table(‘cart’) . ” WHERE session_id = ‘” .SESS_ID. “‘ AND is_gift <> 0”;

$GLOBALS[‘db’]->query($sql);

}
复制代码
存档关闭後上传…

转载请注明:夜阑小雨 » ecshop 使用ajax更新购物车数量for 2.7

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址