ecshop增加直接购买的功能

2009-08-04 12:25 来源:www.chinab4c.com 作者:ecshop专家

     最近帮一个朋友做电子商务系统的修改,牵涉到修改ecshop购物流程。在ecshop中,他想实现既可以放在购物车里面,也可以实现直接购买,就是购买方式共存的两种方式.首先,需要在goods.php中增加一个直接购买的按扭.

    1:<a href="javascript:addToCart1({$goods.goods_id})"><img src="images/add_cart.gif" alt="Add to cart" width="87" height="20" /></a>

    2:增加直接购买的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'];

  // 检查是否有商品规格
  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(), addToCartResponse1, 'POST', 'JSON');
}

   3:增加返回ajax值的js

   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;
      }
    }
    // 没选规格,跳到商品详情页
    else if (result.error == 9)
    {
      location.href = 'goods.php?id=' + result.goods_id;
    }
    else
    {
      alert(result.message);
    }
  }
  else
  {
    var cartInfo = document.getElementById('ECS_CARTINFO');

    if (cartInfo)
    {
      cartInfo.innerHTML = result.content;
    }
   location.href = 'flow.php';
  
  }
}

 4:在flow.php中增加直接购买的代码

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']);

    /* 如果商品有规格,而post的数据没有规格,跳到商品详情页 */
    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));
        }
    }

    /* 如果是一步购物,先清空购物车 */
    if ($_CFG['one_step_buy'] == '1')
    {
        clear_cart();
    }
    else
    {
        if (cart_goods_exists($goods->goods_id, $goods->spec, CART_GENERAL_GOODS))
        {
            // 商品已经存在于购物车中,返回错误信息
            $result['error']   = 1;
            $result['message'] = $_LANG['goods_exists'];

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

    最后你就能发现,这个站可以有两种购买方式共存,一种是直接购买,还有一种就是放在购物车里面。

   相关文章:

   ecshop购物车的实现

  修改ecshop购物车退出后保

  ecshop购物车修改购买数量

   来源:中国B4C电子商务