ecshop增加直接购买的功能

2012-05-19 浏览:1152
ecshop增加直接购买的功能
评论:(0)复制地址

客户需要修改ecshop物流程。在ecshop中,想实现既可以放在购物车里面,也可以实现直接购买,就是购买方式共存的两种方式.首先,需要在goods.php中增加一个直接购买的按扭.
    1:

  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
  

  1. function addToCart1(goodsId, parentId)
     
  2. {
     
  3.   var goods        = new Object();
     
  4.   var spec_arr     = new Array();
     
  5.   var fittings_arr = new Array();
     
  6.   var number       = 1;
     
  7.   var formBuy      = document.forms['ECS_FORMBUY'];
     
  8.   // 检查是否有商品规格
     
  9.   if (formBuy)
     
  10.   {
     
  11.     spec_arr = getSelectedAttributes(formBuy);
     
  12.     if (formBuy.elements['number'])
     
  13.     {
     
  14.       number = formBuy.elements['number'].value;
     
  15.     }
     
  16.   }
     
  17.   goods.spec     = spec_arr;
     
  18.   goods.goods_id = goodsId;
     
  19.   goods.number   = number;
     
  20.   goods.parent   = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);
     
  21.   Ajax.call('flow.php?step=add_to_cart1', 'goods=' + goods.toJSONString(), addToCartResponse1, 'POST', 'JSON');
     
  22. }
复制代码

  3:增加返回ajax值的js
  

  1. function addToCartResponse1(result)
     

  2.  
  3.   if (result.error > 0)
     
  4.   {
     
  5.     // 如果需要缺货登记,跳转
     
  6.     if (result.error == 2)
     
  7.     {
     
  8.       if (confirm(result.message))
     
  9.       {
     
  10.         location.href = 'user.php?act=add_booking&id=' + result.goods_id;
     
  11.       }
     
  12.     }
     
  13.     // 没选规格,跳到商品详情
     
  14.     else if (result.error == 9)
     
  15.     {
     
  16.       location.href = 'goods.php?id=' + result.goods_id;
     
  17.     }
     
  18.     else
     
  19.     {
     
  20.       alert(result.message);
     
  21.     }
     
  22.   }
     
  23.   else
     
  24.   {
     
  25.     var cartInfo = document.getElementByIdx_x('ECS_CARTINFO');
     
  26.     if (cartInfo)
     
  27.     {
     
  28.       cartInfo.innerHTML = result.content;
     
  29.     }
     
  30.    location.href = 'flow.php';
     
  31.    
     
  32.   }
     
  33. }
复制代码

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

  1. elseif ($_REQUEST['step'] == 'add_to_cart1'){
     
  2.   include_once('includes/cls_json.php');
     
  3.     if (!empty($_REQUEST['goods_id']) && empty($_POST['goods']))
     
  4.     {
     
  5.         if (!is_numeric($_REQUEST['goods_id']) || intval($_REQUEST['goods_id']) <= 0)
     
  6.         {
     
  7.             header("location:./\n");
     
  8.         }
     
  9.         $goods_id = intval($_REQUEST['goods_id']);
     
  10.         exit;
     
  11.     }
     
  12.     $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
     
  13.     $json  = new JSON;
     
  14.     if (empty($_POST['goods']))
     
  15.     {
     
  16.         $result['error'] = 1;
     
  17.         die($json->encode($result));
     
  18.     }
     
  19.     $goods = $json->decode($_POST['goods']);
     
  20.    
     
  21.     if (empty($goods->spec))
     
  22.     {
     
  23.         $sql = "SELECT COUNT(*) " .
     
  24.                 "FROM " . $ecs->table('goods_attr') . " AS ga, " .
     
  25.                           $ecs->table('attribute') . " AS a " .
     
  26.                 "WHERE ga.attr_id = a.attr_id " .
     
  27.                 "AND ga.goods_id = '" . $goods->goods_id . "' " .
     
  28.                 "AND a.attr_type = 1";
     
  29.         if ($db->getOne($sql) > 0)
     
  30.         {
     
  31.             $result['error']   = 9;
     
  32.             $result['goods_id'] = $goods->goods_id;
     
  33.             die($json->encode($result));
     
  34.         }
     
  35.     }
     
  36.    
     
  37.     if ($_CFG['one_step_buy'] == '1')
     
  38.     {
     
  39.         clear_cart();
     
  40.     }
     
  41.     else
     
  42.     {
     
  43.         if (cart_goods_exists($goods->goods_id, $goods->spec, CART_GENERAL_GOODS))
     
  44.         {
     
  45.             // 商品已经存在于购物车中,返回错误信息
     
  46.             $result['error']   = 1;
     
  47.             $result['message'] = $_LANG['goods_exists'];
     
  48.             die($json->encode($result));
     
  49.         }
     
  50.     }
     
  51.    
     
  52.     if (!is_numeric($goods->number) || intval($goods->number) <= 0)
     
  53.     {
     
  54.         $result['error']   = 1;
     
  55.         $result['message'] = $_LANG['invalid_number'];
     
  56.     }
     
  57.     else
     
  58.     {
     
  59.         
     
  60.         if (addto_cart($goods->goods_id, $goods->number, $goods->spec, $goods->parent))
     
  61.         {
     
  62.             if ($_CFG['cart_confirm'] > 2)
     
  63.             {
     
  64.                 $result['message'] = '';
     
  65.             }
     
  66.             else
     
  67.             {
     
  68.                 $result['message'] = $_CFG['cart_confirm'] == 1 ? $_LANG['addto_cart_success_1'] : $_LANG['addto_cart_success_2'];
     
  69.             }
     
  70.             $result['content'] = insert_cart_info();
     
  71.             $result['one_step_buy'] = $_CFG['one_step_buy'];
     
  72.         }
     
  73.         else
     
  74.         {
     
  75.             $result['message']  = $err->last_message();
     
  76.             $result['error']    = $err->error_no;
     
  77.             $result['goods_id'] = stripslashes($goods->goods_id);
     
  78.         }
     
  79.     }
     
  80.     $result['confirm_type'] = 4;
     
  81.     die($json->encode($result));
     

  82.  
  83. }
复制代码

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

评论:(0)复制地址

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。