【电商网站】将商品加入购物车代码

来源:互联网 发布:网络运营专员工作内容 编辑:程序博客网 时间:2024/04/29 10:33
/** * 添加产品到购物车 * @param array $info  * @param string $type * @param int $member_id */function add_item($info, $type = 'cookie') {if ($type == 'cookie') {$items = zp_json_decode ( zp_getcookie ( CART_COOKIE_NAME ), true );if(is_array($items)) {//数组情况,product_spec_id对应gt_product_spec表中product_spec_id字段的值,num-产品数量$items [] = array ('id' => $info ['product_spec_id'], 'num' => $info ['quantity'] );} else {//非数组情况$items = array(array ('id' => $info ['product_spec_id'], 'num' => $info ['quantity']));}//将购物车中产品进行json后,再放入cookie中return zp_setcookie ( CART_COOKIE_NAME, zp_json_encode ( $items ), CART_COOKIE_TIME );} elseif ($type == 'db') {$item = array ('member_id' => intval($info ['member_id']), 'product_spec_id' => intval($info ['product_spec_id']),//对应gt_product_spec表中product_spec_id字段'quantity' => intval($info ['quantity']), //产品数量'addtime' => time (), //添加时间'lastmodify'=> time ()//修改时间);$sql = $this->db->get_insert_db_sql ( TBL_PRE . "cart", $item );//加入购物车return $this->db->query ( $sql );}}


2.检查指定商品是否已加入购物车中

/** * 检查指定产品是否已加入购物车中 * @param $member_id *  * @return int 购物车中产品数量 */function check_item($product_spec_id, $type = 'cookie', $member_id = 0) {$product_spec_id = intval($product_spec_id);$member_id = intval($member_id);if ($type == 'cookie') {$items = zp_json_decode ( zp_getcookie ( CART_COOKIE_NAME ), true );if (! is_array ( $items )) { // cookie不存在return 0;}foreach ( $items as $key => $value ) {if ($value ['id'] == $product_spec_id) {return $value['num']; // 返回cookie中此产品数量break;}}} elseif ($type == 'db') {$sql = "SELECT quantity FROM " . TBL_PRE . "cart WHERE member_id = $member_id AND product_spec_id = $product_spec_id";return $this->db->get_one ( $sql );}return false;}

3.更新购物车中产品数量

/** * 更新购物车内产品数量 *  * @param array $info * @param string $action + 增加 / s 指定值  * @param string $type */function update_item($info, $type = 'cookie', $action = "+") {if($type == 'cookie') {$items = zp_json_decode ( zp_getcookie ( CART_COOKIE_NAME), true );foreach ( $items as $key => $value ) {if ($value ['id'] == $info ['product_spec_id']) {if ($action == "+") {$result = $value ['num'] + $info ['quantity'];} else {$result = $info ['quantity'];}if ($result < 1) $result = 1; // 不可小于1$items [$key] ['num'] = intval($result);break;}}return zp_setcookie ( CART_COOKIE_NAME, zp_json_encode ( $items ), CART_COOKIE_TIME );} elseif ($type == 'db') {$item = array ('member_id' => intval($info ['member_id']), 'quantity' => intval($info ['quantity']), 'product_spec_id' => intval($info ['product_spec_id']), 'lastmodify' => time () );if ($action == "+") {$sql1 = "SELECT quantity FROM ". TBL_PRE. "cart " ." WHERE product_spec_id = " . $item['product_spec_id'] . " AND member_id = " . $item['member_id'];$_curnum = $this->db->get_one($sql1);$result = intval($_curnum) + $item['quantity'];} else {$result = $item['quantity'];}if ( $result < 1 ) $result = 1; //不可小于1$sql = "UPDATE " . TBL_PRE . "cart" . " SET quantity = $result, lastmodify = " . $item ['lastmodify'] . " WHERE member_id = " . $item ['member_id'] . " AND product_spec_id = " . $item ['product_spec_id'];//dump($sql);return $this->db->query ( $sql );}}

4.删除购物车内指定商品

/** * 删除购物车内指定产品 * @param $product_id * @param $type * @param $member_id */function drop($product_spec_id, $type = "cookie", $member_id = 0) {$member_id = intval ( $member_id );$product_spec_id = intval ($product_spec_id);if ($type == 'cookie') {$items = zp_json_decode ( zp_getcookie ( CART_COOKIE_NAME ), true );if (! empty ( $items )) {foreach ( $items as $key => $value ) {if ($value ['id'] == $product_spec_id) {unset ( $items [$key] );break;}}if (!empty($items)){    return zp_setcookie ( CART_COOKIE_NAME, zp_json_encode ( $items ), CART_COOKIE_TIME );} else {return zp_setcookie ( CART_COOKIE_NAME, "", CART_COOKIE_TIME );}} else {return zp_setcookie ( CART_COOKIE_NAME, "", CART_COOKIE_TIME );}} elseif ($type == 'db') {$sql = "DELETE FROM " . TBL_PRE . "cart  WHERE product_spec_id = $product_spec_id AND member_id = $member_id";return $this->db->query ( $sql );}}

5.清空购物车

/** * 清空购物车 * @param $type * @param $member_id */function clear($type ="cookie",$member_id=0){$member_id = intval($member_id);if ($type == 'cookie') {return zp_setcookie ( CART_COOKIE_NAME, "", CART_COOKIE_TIME );} else {$sql = "DELETE FROM " . TBL_PRE . "cart WHERE member_id = $member_id";return $this->db->query($sql);}}

6.合并用户cookie和数据库中的购物车数据

/** * 合并用户cookie和数据库中的购物车数据 * @param $member_id */function merger_item($member_id) {$member_id = intval ( $member_id );if (!$member_id) {return false;}$cookie_items = zp_json_decode ( zp_getcookie ( CART_COOKIE_NAME ), true );if (is_array($cookie_items) && !empty($cookie_items)){$sql = "SELECT product_spec_id FROM " . TBL_PRE . "cart WHERE member_id = $member_id";$db_items = $this->db->get_all ( $sql );// 将数据转换为以product_id为健名索引的数组$db_items = zp_specify_array_key($db_items,"product_spec_id");foreach ( $cookie_items as $item ) {if (array_key_exists ( $item ['id'], $db_items )) { // 已存在,只更新数量$sql = "UPDATE " . TBL_PRE . "cart SET `quantity` = quantity+" . intval($item ['num']) . "WHERE member_id = $member_id AND product_spec_id = " . intval($item ['id']);$this->db->query ( $sql );} else { // 不存在,插入一条新记录$item = array ('member_id' => $member_id, 'product_spec_id' => intval($item ['id']), 'quantity' => intval($item ['num']), 'addtime' => time (), 'lastmodify' => time () );$sql = $this->db->get_insert_db_sql ( TBL_PRE . "cart", $item );$this->db->query ( $sql );}}return zp_setcookie ( CART_COOKIE_NAME, '', CART_COOKIE_TIME );}}

7.添加商品到收藏夹:member_id、product_id、is_buy、create_time

/** * 添加到收藏夹 */function add_to_collect($info){$info = array('member_id' => intval($info['member_id']),'product_id' => intval($info['product_id']),'is_buy' => intval($info['is_buy']));// 先检查是否收藏,如果已经收藏过,则不用再添加$tbl_collect = TBL_PRE ."member_collection";$sql = "SELECT id FROM $tbl_collect WHERE member_id = " .$info['member_id']. " AND product_id =".$info['product_id'];if (!$this->db->get_all($sql)){$sql = "INSERT INTO $tbl_collect  SET member_id = " .$info['member_id'].", product_id = " .$info['product_id'].", is_buy = ".$info['is_buy'].", create_time = ".time();return $this->db->query($sql);} else {return true;}}

8.获取用户收藏夹中数据

/** * 获取用户收藏夹数据 */function get_collect($member_id,$num=5){$member_id = intval($member_id);$tbl_collect = TBL_PRE ."member_collection";$sql = "SELECT product_id FROM $tbl_collect WHERE member_id = $member_id ORDER BY id DESC LIMIT 0,$num ";$_spids = $this->db->get_all($sql);foreach ($_spids as $val){$product_mod = M::load_model('product');$result[] = $product_mod->get_product_info($val['product_id']);//通过产品id获取产品信息}return $result;}

9.获取仓库中的库存数量

/**2013-01-08* 获取第一仓某个货品的库存量*/function get_firstwarehouse_stock($product_spec_id = 0,$first_wm_id=0){$tbl_goods_warehouse =  TBL_PRE . "goods_warehouse_index";//fix_stock:锁定库存      stock:存储量$sql = "SELECT stock - fix_stock FROM $tbl_goods_warehouse WHERE product_spec_id = $product_spec_id AND warehouse_id = $first_wm_id ";$remain_num = $this->db->get_one($sql);return $remain_num;}

10.检查货品库存是否满足某一值$num

/** * 检查货品库存是否满足某一值$num *  * @param int $id 货品ID * @return int $num 与库存对比的数字 */function check_stock($id,$num) {$id = intval ( $id );/* 2011-11-10 by sandy * 产品添加进购物车时,需要记录用户事件,要用到产品ID,检查库存时 * 顺便把product_id 取出 减少查询次数   */$sql = "SELECT stock,fix_stock,product_id FROM " . TBL_PRE . "product_spec WHERE product_spec_id=$id";$stock_info= $this->db->get_row ( $sql );if (!$stock_info) { // 产品不存在$result = array('result' => -1,'stock' => 0);} else {$_current = $stock_info ["stock"] - $stock_info ["fix_stock"];if ($_current < $num) { // 库存不足$result = array('result' => 0,'stock'=>$_current);} else {$result = array('result' => 1,'stock'=>$_current,'product_id'=>$stock_info['product_id']);}}return $result;}

11.添加货品到购物车

/** * 添加物品到购物车,登录用户和非登录用户都可添加 * 登录用户添加到数据库,非登录用户添加到cookie */function add_action() {//点击添加购物车按钮时,传递过来两个参数$product_spec_id = isset ( $_GET ['spid'] ) ? intval ( $_GET ['spid'] ) : 0;//产品规格id:product_spec_id,对应product_spec_id表中product_spec_id字段的值$quantity = isset ( $_GET ['num'] ) ? intval ( $_GET ['num'] ) : 0;//产品数量if (! $product_spec_id || ! $quantity) {$this->redirect_msg("参数错误");exit ();}// fix 添加商品时,应该先将添加的数量与购物车内已有的数量进行加总后再检查库存$curr_num = $this->cart_mod->check_item($product_spec_id);//购物车内已有该产品数量// 产品是否存在,库存是否足够$product_mod = M::load_model ( 'product' );$product_stock = $product_mod->check_stock ( $product_spec_id, $quantity + $curr_num );//库存是否足够,查询的gt_product_spec表if ($product_stock['result'] == - 1) {$this->redirect_msg( "抱歉,该产品不存在或已经下架!" );exit ();}if ($product_stock['result'] == 0) {if ($curr_num > 0){$msg = "抱歉,该产品库存不足,您的购物袋内已经有 $curr_num 件该产品";} else {$msg = "抱歉,该产品库存不足";}$this->redirect_msg($msg);exit ();}//库存充足的情况// 未登录,写入cookie. json格式 [{"id":1,"num":1}];if (! $this->visitor->is_login ()) {//未登录的情况,存入cookie//$info = array ('product_spec_id' => $product_spec_id, 'quantity' => $quantity );if (! $this->cart_mod->check_item ( $product_spec_id )) {//购物车中已有产品数量,如果不存在$this->cart_mod->add_item ( $info ); // 不存在,添加到购物车} else {$this->cart_mod->update_item ( $info ); // 已存在,更新购物车中数量}} else {//已登录的情况,存入购物车表// 已登录,此处不必再对cookie和数据库中数据进行合并$info = array ('member_id' => $this->visitor->get ( 'member_id' ), //会员id'product_spec_id' => $product_spec_id, //货品id'quantity'=> $quantity //货品数量);if (! $this->cart_mod->check_item ( $product_spec_id, 'db', $info ['member_id'] )) {//购物车中已有产品数量为0的情况if (!$this->cart_mod->add_item ( $info, 'db' )) {//添加产品到购物车$this->redirect_msg("添加商品失败" );exit();}} else {//购物车中已有产品数量不为0的情况,更新产品数量if (!$this->cart_mod->update_item ( $info, 'db' )) {$this->redirect_msg("添加商品失败" );exit();}}}$this->redirect_msg("商品已添加到购物袋", 'c=cart');}

11.获取产品的实际库存

/** * 获取产品的实际库存 * @param $product_id */function get_stock_sum($product_id){$tbl_product_spec = TBL_PRE . "product_spec";$sql_stock = "SELECT SUM((stock-fix_stock)) as real_stock_sum   FROM $tbl_product_spec    WHERE product_id = $product_id AND is_stop = 0 AND status = 1 GROUP BY product_id";return $this->db->get_one($sql_stock);}

12.获取产品的基本信息:

/** * 获取一件产品基本信息 (介绍,品牌,分类,扩展属性,规格名称) * @param int $id 产品ID */function get_base_info($id,$is_filter = true,$img_domain = RES_DOMAIN) {$result = array();$id = intval ( $id );if (!$id)return;if ($is_filter){$_filter = " AND p.is_check = 1"; // 是否审核 1=已经审核 0=没有审核 -1=审核不通过} else {$_filter = "";}    $tbl_product = TBL_PRE . "products";    $tbl_goods = TBL_PRE . "goods";    $tbl_brand = TBL_PRE . "goods_brand";    $tbl_category = TBL_PRE . "goods_category";    $tbl_type = TBL_PRE . "goods_type";    $tbl_spec = TBL_PRE ."goods_specification";    $sql = " SELECT a.*, b.brand_name, b.brand_name_second,b.brand_logo,b.brand_simple_intro, b.url_path,c.category_name,c.url_path AS cate_url_path, sp.spec_name, sp.spec_memo      FROM   (SELECT g.*,p.product_id,p.url_id, p.bn, p.bar_code, p.color_id, p.color_text,p.small_list_page_product_pic_default,  p.small_list_page_product_pic_replace,p.is_new, p.is_promotion,p.is_newstyle,p.is_limit,p.is_classic,p.is_new_spring,p.act_id,   p.product_name, p.price as product_price, p.market_price as product_market_price, p.intro,   p.editor_intro,p.size_image_path, p.size_intro, p.video_path, p.size_image_name,    p.status as pstatus, p.is_stop as pisstop     FROM $tbl_product p     LEFT JOIN $tbl_goods g ON p.goods_id = g.goods_id     WHERE p.product_id = $id $_filter) a      LEFT JOIN $tbl_brand b ON a.brand_id = b.brand_id     LEFT JOIN $tbl_category c ON a.category_id = c.category_id     LEFT JOIN $tbl_spec sp ON a.spec_id = sp.spec_id";$result['base'] = $this->db->cache_row($sql);//var_dump($result['base']);if (empty($result['base'])){return array();}// 拼接视频,品牌logo等资源路径if(!empty($result['base']['size_image_name'])){ // 尺寸图片先检查是否直接输入了地址,再检查直接上传的图片路径$result['base']['size_image_path'] = VIDEO_DOMAIN."size_pic/".$result['base']['size_image_name'];} else {!empty($result['base']['size_image_path']) && $result['base']['size_image_path'] = RES_DOMAIN.$result['base']['size_image_path'];}!empty($result['base']['brand_logo']) && $result['base']['brand_logo'] = RES_DOMAIN.$result['base']['brand_logo'];!empty($result['base']['video_path']) && $result['base']['video_path'] = VIDEO_DOMAIN.$result['base']['video_path'];!empty($result['base']['small_list_page_product_pic_default']) && $result['base']['small_list_page_product_pic_default'] = RES_DOMAIN.$result['base']['small_list_page_product_pic_default'];!empty($result['base']['small_list_page_product_pic_replace']) && $result['base']['small_list_page_product_pic_replace'] = RES_DOMAIN.$result['base']['small_list_page_product_pic_replace'];// 产品图片$result['images'] = $this->get_product_images($id,$img_domain);// 产品规格派生的细分产品(货品,即尺码)信息$result['items'] = $this->get_product_items($id);reset($result['items']);$result['default_item'] = current($result['items']); // 第一条数据作为默认// 计算该商品各规格(尺码)库存合计,如果所有规格(尺码)都无库存,前端则不提供数量选框 2011-11-14 fix by sandy$result['stock_total'] = 0;foreach($result['items'] as $v){$result['stock_total'] += $v['stock'];}return $result;}

13.获取某一分类从根节点到自身的层级结构:

/** * 获取某一分类从根节点到自身的层级结构 *  * @param int $category_id */function get_category_tree($category_id){$category_id = intval($category_id);if(!$category_id){return array();}$tbl_category = TBL_PRE .'goods_category';$full_path_str = ''; // 获取上级路径$sql = "SELECT category_path FROM $tbl_category WHERE category_id = $category_id";//父级分类id:26、11$pre_path_str = $this->db->get_one($sql);if(!empty($pre_path_str)){$full_path_str = $pre_path_str.$category_id;//26、11、2} else {$full_path_str = $category_id;//2}// 防止数据不正确,先切分为数组,强制类型转换$ids = explode(",",$full_path_str);$result = array();foreach ($ids as $key=>$id){$result[] = $this->db->cache_row("SELECT category_id,category_name,url_path FROM $tbl_category WHERE category_id=".intval($id));}return $result;}


原创粉丝点击