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

/**
	 * 添加产品到购物车
	 * @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;
	}


  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android电商应用中加入购物车动画可以通过以下步骤实现: 1. 在布局文件中创建一个ImageView或者其他的动画控件。 2. 创建一个Drawable资源文件,用于定义加入购物车的动画效果。 3. 在Java代码中获取ImageView控件,并使用AnimationUtils加载Drawable资源文件,然后将动画绑定到ImageView上。 4. 在加入购物车的点击事件中启动动画,将商品图片从列表中的位置移动到购物车的位置,并同时播放加入购物车的动画效果。 以下是一个简单的示例代码: 1. 在布局文件中添加ImageView控件: ``` <ImageView android:id="@+id/cartIcon" android:layout_width="64dp" android:layout_height="64dp" android:src="@drawable/cart_icon" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:layout_marginBottom="16dp"/> ``` 2. 创建Drawable资源文件cart_add.xml: ``` <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"> <scale android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="1.5" android:toYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:duration="300" android:interpolator="@android:anim/accelerate_interpolator"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" android:startOffset="300" android:interpolator="@android:anim/accelerate_interpolator"/> </set> ``` 3. 在Java代码中获取ImageView控件并绑定动画: ``` ImageView cartIcon = findViewById(R.id.cartIcon); Animation animation = AnimationUtils.loadAnimation(this, R.anim.cart_add); cartIcon.setAnimation(animation); ``` 4. 在加入购物车的点击事件中启动动画: ``` buttonAddToCart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 将商品图片从列表中的位置移动到购物车的位置 // ... // 播放加入购物车的动画效果 cartIcon.startAnimation(animation); } }); ``` 以上是一个简单的示例,您可以根据实际需求进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值