当前位置:首页 > CMS教程 > ecshop > 列表

ecshop在购物车基础上添加立即购买功能

发布:smiling 来源: PHP粉丝网  添加日期:2014-11-15 09:05:29 浏览: 评论:0 

1:goods.dwt模板文件

<a href="javascript:addToCart1({$goods.goods_id})"><img src="images/bnt_liji.gif" />

2:js/common.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.   var quick     = 0; 
  9.   // 检查是否有商品规格  
  10.   if (formBuy) 
  11.   { 
  12.     spec_arr = getSelectedAttributes(formBuy); 
  13.     if (formBuy.elements['number']) 
  14.     { 
  15.       number = formBuy.elements['number'].value; 
  16.     } 
  17.  quick = 1; 
  18.   } 
  19.   goods.quick    = quick; 
  20.   goods.spec     = spec_arr; 
  21.   goods.goods_id = goodsId; 
  22.   goods.number   = number; 
  23.   goods.parent   = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId); 
  24.   Ajax.call('flow.php?step=add_to_cart1', 'goods=' + goods.toJSONString(), addToCartResponse1, 'POST', 'JSON'); 
  25. } 
  26. function addToCartResponse1(result) 
  27. { 
  28.   if (result.error > 0) 
  29.   { 
  30.     // 如果需要缺货登记,跳转 
  31.     if (result.error == 2) 
  32.     { 
  33.       if (confirm(result.message)) 
  34.       { 
  35.         location.href = 'user.php?act=add_booking&id=' + result.goods_id + '&spec=' + result.product_spec; 
  36.       } 
  37.     } 
  38.     // 没选规格,弹出属性选择框 
  39.     else if (result.error == 6) 
  40.     { 
  41.       openSpeDiv(result.message, result.goods_id, result.parent); 
  42.     } 
  43.     else 
  44.     { 
  45.       alert(result.message); 
  46.     } 
  47.   } 
  48.   else 
  49.   { 
  50.     var cartInfo = document.getElementById('ECS_CARTINFO'); 
  51.     var cart_url = 'flow.php?step=cart'; 
  52.     if (cartInfo) 
  53.     { 
  54.       cartInfo.innerHTML = result.content; 
  55.     } 
  56.  location.href = cart_url; 
  57.      
  58.   } 
  59. } 

3:根目录下的flow.php

  1. elseif ($_REQUEST['step'] == 'link_buy')上面添加 
  2.  
  3. elseif ($_REQUEST['step'] == 'add_to_cart1') 
  4. { 
  5.     include_once('includes/cls_json.php'); 
  6.     $_POST['goods'] = json_str_iconv($_POST['goods']); 
  7.     if (!emptyempty($_REQUEST['goods_id']) && emptyempty($_POST['goods'])) 
  8.     { 
  9.         if (!is_numeric($_REQUEST['goods_id']) || intval($_REQUEST['goods_id']) <= 0) 
  10.         { 
  11.             ecs_header("Location:./\n"); 
  12.         } 
  13.         $goods_id = intval($_REQUEST['goods_id']); 
  14.         exit; 
  15.     } 
  16.     $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => ''); 
  17.     $json  = new JSON; 
  18.     if (emptyempty($_POST['goods'])) 
  19.     { 
  20.         $result['error'] = 1; 
  21.         die($json->encode($result)); 
  22.     } 
  23.     $goods = $json->decode($_POST['goods']); 
  24.     /* 检查:如果商品有规格,而post的数据没有规格,把商品的规格属性通过JSON传到前台 */ 
  25.     if (emptyempty($goods->spec) AND emptyempty($goods->quick)) 
  26.     { 
  27.         $sql = "SELECT a.attr_id, a.attr_name, a.attr_type, ". 
  28.             "g.goods_attr_id, g.attr_value, g.attr_price " . 
  29.         'FROM ' . $GLOBALS['ecs']->table('goods_attr') . ' AS g ' . 
  30.         'LEFT JOIN ' . $GLOBALS['ecs']->table('attribute') . ' AS a ON a.attr_id = g.attr_id ' . 
  31.         "WHERE a.attr_type != 0 AND g.goods_id = '" . $goods->goods_id . "' " . 
  32.         'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id'; 
  33.         $res = $GLOBALS['db']->getAll($sql); 
  34.         if (!emptyempty($res)) 
  35.         { 
  36.             $spe_arr = array(); 
  37.             foreach ($res AS $row) 
  38.             { 
  39.                 $spe_arr[$row['attr_id']]['attr_type'] = $row['attr_type']; 
  40.                 $spe_arr[$row['attr_id']]['name']     = $row['attr_name']; 
  41.                 $spe_arr[$row['attr_id']]['attr_id']     = $row['attr_id']; 
  42.                 $spe_arr[$row['attr_id']]['values'][] = array( 
  43.                                                             'label'        => $row['attr_value'], 
  44.                                                             'price'        => $row['attr_price'], 
  45.                                                             'format_price' => price_format($row['attr_price'], false), 
  46.                                                             'id'           => $row['goods_attr_id']); 
  47.             } 
  48.             $i = 0; 
  49.             $spe_array = array(); 
  50.             foreach ($spe_arr AS $row) 
  51.             { 
  52.                 $spe_array[]=$row; 
  53.             } 
  54.             $result['error']   = ERR_NEED_SELECT_ATTR; 
  55.             $result['goods_id'] = $goods->goods_id; 
  56.             $result['parent'] = $goods->parent; 
  57.             $result['message'] = $spe_array; 
  58.             die($json->encode($result)); 
  59.         } 
  60.     } 
  61.  
  62.     /* 检查:商品数量是否合法 */ 
  63.     if (!is_numeric($goods->number) || intval($goods->number) <= 0) 
  64.     { 
  65.         $result['error']   = 1; 
  66.         $result['message'] = $_LANG['invalid_number']; 
  67.     } 
  68.     /* 更新:购物车 */ 
  69.     else 
  70.     { 
  71.         // 更新:添加到购物车 
  72.         if (addto_cart($goods->goods_id, $goods->number, $goods->spec, $goods->parent)) 
  73.         { 
  74.             if ($_CFG['cart_confirm'] > 2) 
  75.             { 
  76.                 $result['message'] = ''; 
  77.             } 
  78.             else 
  79.             { 
  80.                 $result['message'] = $_CFG['cart_confirm'] == 1 ? $_LANG['addto_cart_success_1'] : $_LANG['addto_cart_success_2']; 
  81.             } 
  82.             $result['content'] = insert_cart_info(); 
  83.             $result['one_step_buy'] = $_CFG['one_step_buy']; 
  84.         } 
  85.         else 
  86.         { 
  87.             $result['message']  = $err->last_message(); 
  88.             $result['error']    = $err->error_no; 
  89.             $result['goods_id'] = stripslashes($goods->goods_id); 
  90.             if (is_array($goods->spec)) 
  91.             { 
  92.                 $result['product_spec'] = implode(',', $goods->spec); 
  93.             }//开源软件:phpfensi.com 
  94.             else 
  95.             { 
  96.                 $result['product_spec'] = $goods->spec; 
  97.             } 
  98.         } 
  99.     } 
  100.     $result['confirm_type'] =3; 
  101.     die($json->encode($result)); 
  102. }

Tags: ecshop购物车 ecshop立即购买

分享到: