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

ecshop增加购买直接进入购物车与默认购物流程并存

发布:smiling 来源: PHP粉丝网  添加日期:2014-06-18 16:39:16 浏览: 评论:0 

ecshop如何设置了购买停留在商品页面之后,将ecshop商品加入购物车的时候,就不会直接进入购物车,这样使用购物车非常不方便,我们为了既保持该页面功能,又能进入购物车功能,所以增加了另外一个购买按扭,点购买之后,可以直接进入ecshop的购物车,这样只牵涉到修改ecshop模板,对ecshop二次开发稍微修改就可以做到.

1:goods.dwt模板

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

Tags: ecshop增加购买 购物流程

分享到:

相关文章