ECSHOP商品详情页面如何实现直接购买和放入到购物车同时存在

一、打开网站根目录 js/common.js 文件增加代码:

function addToPay(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'];
// 检查是否有商品规格
if (formBuy)
{
spec_arr = getSelectedAttributes(formBuy);
if (formBuy.elements['number'])
{
number = formBuy.elements['number'].value;
}
}
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(), addToCartResponse2, 'POST', 'JSON');
}


function addToCartResponse2(result)

{
if (result.error > 0)
{
// 如果需要缺货登记,跳转
if (result.error == 2)
{
if (confirm(result.message))
{
location.href = 'user.php?act=add_booking&id=' + result.goods_id;
}
}
// 没选规格,跳到商品详情页
else if (result.error == 9)
{
location.href = 'goods.php?id=' + result.goods_id;
}
else
{
alert(result.message);
}
}
else
{
var cartInfo = document.getElementByIdx_x('ECS_CARTINFO');
if (cartInfo)
{
cartInfo.innerHTML = result.content;
}
location.href = 'flow.php?step=checkout';

}

}

 

二、打开根目录下文件 flow.php 查找代码

elseif ($_REQUEST['step'] == 'link_buy')
{
$goods_id = intval($_GET['goods_id']);

if (!cart_goods_exists($goods_id,array()))
{
addto_cart($goods_id);
}
ecs_header("Location:./flow.php\n");
exit;
}

在后面增中代码:

elseif ($_REQUEST['step'] == 'add_to_cart1'){
include_once('includes/cls_json.php');
if (!empty($_REQUEST['goods_id']) && empty($_POST['goods']))
{
if (!is_numeric($_REQUEST['goods_id']) || intval($_REQUEST['goods_id']) <= 0)
{
header("location:./\n");
}
$goods_id = intval($_REQUEST['goods_id']);
exit;
}
$result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
$json = new JSON;
if (empty($_POST['goods']))
{
$result['error'] = 1;
die($json->encode($result));
}
$goods = $json->decode($_POST['goods']);

if (empty($goods->spec))
{
$sql = "SELECT COUNT(*) " .
"FROM " . $ecs->table('goods_attr') . " AS ga, " .
$ecs->table('attribute') . " AS a " .
"WHERE ga.attr_id = a.attr_id " .
"AND ga.goods_id = '" . $goods->goods_id . "' " .
"AND a.attr_type = 1";
if ($db->getOne($sql) > 0)
{
$result['error'] = 9;
$result['goods_id'] = $goods->goods_id;
die($json->encode($result));
}
}

clear_cart();

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);
}
}
$result['confirm_type'] = 4;
die($json->encode($result));



}

 

这样就可以了,试试。

posted on 2012-10-16 13:19  ECshop商城二次开发博客  阅读(3438)  评论(0编辑  收藏  举报

导航