ecshop在购物车基础上添加立即购买功能
1:goods.dwt模板文件
<a href="javascript:addToCart1({$goods.goods_id})"><img src="images/bnt_liji.gif" />
2:js/common.js
- function addToCart1(goodsId, parentId)
- {
- var goods = new Object();
- var spec_arr = new Array();
- var fittings_arr = new Array();
- var number = 1;
- var formBuy = document.forms['ECS_FORMBUY'];
- var quick = 0;
-
- if (formBuy)
- {
- spec_arr = getSelectedAttributes(formBuy);
- if (formBuy.elements['number'])
- {
- number = formBuy.elements['number'].value;
- }
- quick = 1;
- }
- goods.quick = quick;
- goods.spec = spec_arr;
- goods.goods_id = goodsId;
- goods.number = number;
- goods.parent = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);
- Ajax.call('flow.php?step=add_to_cart1', 'goods=' + goods.toJSONString(), addToCartResponse1, 'POST', 'JSON');
- }
- function addToCartResponse1(result)
- {
- if (result.error > 0)
- {
-
- if (result.error == 2)
- {
- if (confirm(result.message))
- {
- location.href = 'user.php?act=add_booking&id=' + result.goods_id + '&spec=' + result.product_spec;
- }
- }
-
- else if (result.error == 6)
- {
- openSpeDiv(result.message, result.goods_id, result.parent);
- }
- else
- {
- alert(result.message);
- }
- }
- else
- {
- var cartInfo = document.getElementById('ECS_CARTINFO');
- var cart_url = 'flow.php?step=cart';
- if (cartInfo)
- {
- cartInfo.innerHTML = result.content;
- }
- location.href = cart_url;
-
- }
- }
3:根目录下的flow.php
- elseif ($_REQUEST['step'] == 'link_buy')上面添加
-
- elseif ($_REQUEST['step'] == 'add_to_cart1')
- {
- include_once('includes/cls_json.php');
- $_POST['goods'] = json_str_iconv($_POST['goods']);
- if (!emptyempty($_REQUEST['goods_id']) && emptyempty($_POST['goods']))
- {
- if (!is_numeric($_REQUEST['goods_id']) || intval($_REQUEST['goods_id']) <= 0)
- {
- ecs_header("Location:./\n");
- }
- $goods_id = intval($_REQUEST['goods_id']);
- exit;
- }
- $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
- $json = new JSON;
- if (emptyempty($_POST['goods']))
- {
- $result['error'] = 1;
- die($json->encode($result));
- }
- $goods = $json->decode($_POST['goods']);
-
- if (emptyempty($goods->spec) AND emptyempty($goods->quick))
- {
- $sql = "SELECT a.attr_id, a.attr_name, a.attr_type, ".
- "g.goods_attr_id, g.attr_value, g.attr_price " .
- 'FROM ' . $GLOBALS['ecs']->table('goods_attr') . ' AS g ' .
- 'LEFT JOIN ' . $GLOBALS['ecs']->table('attribute') . ' AS a ON a.attr_id = g.attr_id ' .
- "WHERE a.attr_type != 0 AND g.goods_id = '" . $goods->goods_id . "' " .
- 'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';
- $res = $GLOBALS['db']->getAll($sql);
- if (!emptyempty($res))
- {
- $spe_arr = array();
- foreach ($res AS $row)
- {
- $spe_arr[$row['attr_id']]['attr_type'] = $row['attr_type'];
- $spe_arr[$row['attr_id']]['name'] = $row['attr_name'];
- $spe_arr[$row['attr_id']]['attr_id'] = $row['attr_id'];
- $spe_arr[$row['attr_id']]['values'][] = array(
- 'label' => $row['attr_value'],
- 'price' => $row['attr_price'],
- 'format_price' => price_format($row['attr_price'], false),
- 'id' => $row['goods_attr_id']);
- }
- $i = 0;
- $spe_array = array();
- foreach ($spe_arr AS $row)
- {
- $spe_array[]=$row;
- }
- $result['error'] = ERR_NEED_SELECT_ATTR;
- $result['goods_id'] = $goods->goods_id;
- $result['parent'] = $goods->parent;
- $result['message'] = $spe_array;
- die($json->encode($result));
- }
- }
-
-
- if (!is_numeric($goods->number) || intval($goods->number) <= 0)
- {
- $result['error'] = 1;
- $result['message'] = $_LANG['invalid_number'];
- }
-
- else
- {
-
- if (addto_cart($goods->goods_id, $goods->number, $goods->spec, $goods->parent))
- {
- if ($_CFG['cart_confirm'] > 2)
- {
- $result['message'] = '';
- }
- else
- {
- $result['message'] = $_CFG['cart_confirm'] == 1 ? $_LANG['addto_cart_success_1'] : $_LANG['addto_cart_success_2'];
- }
- $result['content'] = insert_cart_info();
- $result['one_step_buy'] = $_CFG['one_step_buy'];
- }
- else
- {
- $result['message'] = $err->last_message();
- $result['error'] = $err->error_no;
- $result['goods_id'] = stripslashes($goods->goods_id);
- if (is_array($goods->spec))
- {
- $result['product_spec'] = implode(',', $goods->spec);
- }
- else
- {
- $result['product_spec'] = $goods->spec;
- }
- }
- }
- $result['confirm_type'] =3;
- die($json->encode($result));
- }