在一个页面控制ecshop购物车所有操作

2009-07-27 19:27 来源:www.chinab4c.com 作者:ecshop专家

      上段时间,有个朋友让我将ecshop购物车的所有操作都放在一个界面操作,进行购买,删除,清空,删除。其实这个也不是很复杂,可以借用ecshop的ajax功能进行时间。

     1:建立test.php页面。

      define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

$smarty -> display('test.dwt');

2:test.dwt

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<meta name="Keywords" content="{$keywords}" />
<meta name="Description" content="{$description}" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>{$page_title}</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
<link rel="shortcut icon" href="favicon.ico" />
<link rel="icon" href="animated_favicon.gif" type="image/gif" />
<link href="{$ecs_css_path}" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="RSS|{$page_title}" href="{$feed_url}" />
{* 包含脚本文件 *}
{insert_scripts files='common.js'}
{insert_scripts files='transport.js'}

<!-- #BeginLibraryItem "/library/page_header.lbi" --><!-- #EndLibraryItem -->
</head>
<body>

<!-- #BeginLibraryItem "/library/test.lbi" -->

 

 <!-- #EndLibraryItem -->
</body>
</html>
 

3:test.lbi

 <meta http-equiv="Content-Type" content="text/html; charset=gbk">

 

<table width="173" border="1" align="center" cellpadding="0" cellspacing="0">
 <form name="form1" method="post" action=""> <tr>
    <td width="169" height="67">输入编号
     
        <label>
          <input id="addt1" type="text" size="7">
        </label>
      <br>购买数量: <input id="addt1_n" type="text" size="7">
    </td>
  </tr>
  <tr>
    <td height="26"><label>
      <input type="button" name="Submit" value="增加" onClick="addToCart1()">
      <input type="button" name="Submit22" value="删除" onClick="deletecart()">
    </label></td>
  </tr>
 
 
  <tr>
    <td height="101"><div  id="ECS_CARTINFO">
 {insert name='cart_info'}
</div></td>
  </tr>
  <tr>
    <td height="43"><input type="button" name="Submit2" value="清空"  onClick="delete_all_cart()"><input type="button" name="Submit2" value="结算" onClick="location.href='flow.php?step=checkout'" ></td>
  </tr>
  </form>
</table>
 

4:增加js函数,js/common.js

 function addToCart1( parentId)

 
  var goods        = new Object();
  var spec_arr     = new Array();
  var fittings_arr = new Array();
  var number       = 1;
  var formBuy      = document.forms['ECS_FORMBUY'];
 goodsId = document.getElementById('addt1').value;
 number = document.getElementById('addt1_n').value;
 if(number == ''){
  number = 1;
 }
 
  // 检查是否有商品规格
  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(), addToCartResponse, 'POST', 'JSON');
}
function deletecart( parentId)
{
 
 
   
  var goods        = new Object();
  var spec_arr     = new Array();
  var fittings_arr = new Array();
  var number       = 1;
  var formBuy      = document.forms['ECS_FORMBUY'];
  goodsId = document.getElementById('addt1').value
  // 检查是否有商品规格
  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=delete_goods', 'goods=' + goods.toJSONString(), addToCartResponse, 'POST', 'JSON');
}
function delete_all_cart()
{
 
    Ajax.call('flow.php?step=delete_all_goods', 'goods=' + 1, addToCartResponse, 'POST', 'JSON');
}

  4:增加flow.php动作

   elseif ($_REQUEST['step'] == 'add_to_cart1')
{
    /*------------------------------------------------------ */
    //-- 添加商品到购物车
    /*------------------------------------------------------ */
    include_once('includes/cls_json.php');
    $_POST['goods'] = json_str_iconv($_POST['goods']);

    if (!empty($_REQUEST['goods_id']) && empty($_POST['goods']))
    {
        if (!is_numeric($_REQUEST['goods_id']) || intval($_REQUEST['goods_id']) <= 0)
        {
            ecs_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的数据没有规格,把商品的规格属性通过JSON传到前台 */
    if (empty($goods->spec))
    {
        $sql = "SELECT a.attr_id, a.attr_name, a.attr_type, ".
            "g.goods_attr_id, g.attr_value, g.attr_price " .
        'FROM ' . $GLOBALS['ecs']->table('goods_attr') . ' AS g ' .
        'LEFT JOIN ' . $GLOBALS['ecs']->table('attribute') . ' AS a ON a.attr_id = g.attr_id ' .
        "WHERE a.attr_type != 0 AND g.goods_id = '" . $goods->goods_id . "' " .
        'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';

        $res = $GLOBALS['db']->getAll($sql);

        if ($res)
        {
            $spe_arr = array();
            foreach ($res AS $row)
            {
                $spe_arr[$row['attr_id']]['attr_type'] = $row['attr_type'];
                $spe_arr[$row['attr_id']]['name']     = $row['attr_name'];
                $spe_arr[$row['attr_id']]['attr_id']     = $row['attr_id'];
                $spe_arr[$row['attr_id']]['values'][] = array(
                                                            'label'        => $row['attr_value'],
                                                            'price'        => $row['attr_price'],
                                                            'format_price' => price_format($row['attr_price'], false),
                                                            'id'           => $row['goods_attr_id']);
            }
            $i = 0;
            $spe_array = array();
            foreach ($spe_arr AS $row)
            {
                $spe_array[]=$row;
            }
            $result['error']   = ERR_NEED_SELECT_ATTR;
            $result['goods_id'] = $goods->goods_id;
            $result['message'] = $spe_array;

            die($json->encode($result));
        }
    }

    /* 如果是一步购物,先清空购物车 */
    if ($_CFG['one_step_buy'] == '1')
    {
        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'] = !empty($_CFG['cart_confirm']) ? $_CFG['cart_confirm'] : 2;
    $result['confirm_type'] = 4;
     $result['goods_id'] = stripslashes($goods->goods_id);
    die($json->encode($result));
}
elseif ($_REQUEST['step'] == 'delete_goods')
{
    include_once('includes/cls_json.php');
    $_POST['goods'] = json_str_iconv($_POST['goods']);
     $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']);
     $gid  = intval($goods -> goods_id);
     global $sess;
    
     $rec_id = $db -> getOne("select rec_id from ".$ecs->table('cart')." where goods_id = ".$gid." and session_id = '".$sess -> session_id."'");
     flow_drop_cart_goods($rec_id);
     $result['confirm_type'] = 4;
      $result['content'] = insert_cart_info();
 die($json->encode($result));
}
elseif ($_REQUEST['step'] == 'delete_all_goods')
{
    include_once('includes/cls_json.php');
  
     $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
    $json  = new JSON;

  
  
     $sql = "DELETE FROM " . $ecs->table('cart') . " WHERE session_id='" . SESS_ID . "'";
    $db->query($sql);
     $result['confirm_type'] = 4;
      $result['content'] = insert_cart_info();
 die($json->encode($result));
}

   这样就在test.php中可以对ecshop的购物车进行所有的操作

上海租车