当前位置 : 主页 > 网络编程 > PHP >

thinkPHP3.2封装购物车操作类

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt 0){ //用户已登录,更新购物车表的数量 $where = array( 'goods_id' = $goods_id, 'goods_attr_ids' = $goods_attr_ids, 'user_id' = $member_id ); //获取到当前商品所在商品表的库存 $goodsInfo = M("Goods")-
gistfile1.txt
  0){
            //用户已登录,更新购物车表的数量
            $where = array(
                'goods_id' => $goods_id,
                'goods_attr_ids' => $goods_attr_ids,
                'user_id' => $member_id
            );
            //获取到当前商品所在商品表的库存
            $goodsInfo = M("Goods")->find($goods_id);
            //获取到购物车商品的库存
            $cartInfo = M("Cart")->where($where)->find();
            //只有购物车商品库存小于商品表库存的情况下,才更新购物车商品的数量
            //并且购物车的商品数量不可以超过200件
            if($cartInfo['goods_number'] < $goodsInfo['goods_number'] && $cartInfo['goods_number'] < 200 ){
                $saveData = array(
                    'cart_id' => $cartInfo['cart_id'],
                    'goods_number' => $cartInfo['goods_number'] + 1
                );

                //更新购物的数量方式一 
                if(M("Cart")->save($saveData)!==false){
                    return true;
                }else{
                    return false;
                }

                //更新购物的数量方式一  setInc('字段',"新增的值") setDec('字段',"减少的值")
                //M("Cart")->where( $where )->setInc("goods_number",1)

            }

        }else{
            //用户未登录
            $key = $goods_id.'-'.$goods_attr_ids;
            $cookieData = cookie('cartData')?cookie('cartData'):array();
            //判断$key是否存在$cookieData的下标中
            if($cookieData[$key]){
                //说明之前cookie中有数据,则数量加1
                $cookieData[$key] += 1;
                //重新把数据存储到cookie中去,有效期为一个星期
                cookie('cartData',$cookieData,time() + 86400*7);
                return true;
            }
            
        }
    }

    //离线购物车商品转存到在线购物车
    public function moveOffCartToOnlineCart(){
        $member_id = session('member_id');
        //先取出离线购物车的数据
        $cookieData = cookie('cartData')?cookie('cartData'):array();
        foreach ($cookieData as $key => $goods_number) {
            $key = explode('-',$key);
            $where = array(
                'goods_id' => $key[0],
                'goods_attr_ids' => $key[1],
                'user_id' => $member_id
            );
            //判断在线购物车(购物车表)之前有没有加入相同的商品
            $result = M("Cart")->where($where)->find();
            if($result){
                //进来了说明在线购物车有相同商品,直接加数量
                $saveData = array(
                    //更新数量
                    'cart_id' => $result['cart_id'],
                    //之前购买的数量加现在买的数量
                    'goods_number' => $result['goods_number']+$goods_number
                );
                M('Cart')->save($saveData);
            }else{
                //进来了说明在线购物车没有相同商品,直接加商品即可
                //直接在之前的where数组中加一个数量即可
                $where['goods_number'] = $goods_number;
               M("Cart")->add( $where );
            }

            //不管购物车中之前是否有相同的商品,还是新加的商品,转存之后,离线购物车中的商品都应该清除      
            cookie("cartData",null);


        }

    }
    
	/**
	 *  加入商品到购物车
	 *  @param $goods_id 商品的goods_id
	 *  @param $goods_attr_ids 商品的属性id
	 *  @param $goods_number 商品的数量
	 */
	public function addGoodsToCart($goods_id,$goods_attr_ids,$goods_number){
		//加入购物车考虑两种情况 1.用户登陆-存储到购物表中 2、用户未登陆-存储到cookie中
    	$member_id = session("member_id");
    	if($member_id > 0){
    		//用户已经登陆情况
    		$condition = array(
    			//定义查询的条件数组
    			'goods_id' => $goods_id,
    			'goods_attr_ids' => $goods_attr_ids,
    			'user_id' => $member_id,
    			'_logic' => 'and'
    		);
    		//判断购物表中用户有没有加入过相同的商品
    		$result = M("Cart")->where($condition)->find();
    		if($result){
    			//购物车有相同商品,数量加$number
    			return M('Cart')->where($where)->setInc('goods_number',$goods_number);
    		}else{
    			//购物车没有相同商品,直接将此商品加入购物车
    			$addData= array(
    				'goods_id' => $goods_id,
    				'goods_attr_ids' => $goods_attr_ids,
    				'goods_number' => $goods_number,
    				'user_id' => $member_id
    			);
    			return  M("Cart")->add($addData);
    		}
    	}else{
    		//用户未登陆情况
    		//cookie存储商品的结构, 
    		/*
    		array(
    			//数组下标 goods_id-goods_attr_ids => 值 goods_number
				"goods_id-goods_attr_ids" => goods_number
    		)
    		*/
    		$cartData = cookie('cartData')?cookie('cartData'):array();
    		$key = $goods_id.'-'.$goods_attr_ids;
            //判断之前有没有买过,有则加数量
    		if($cartData[$key]){
    			$cartData[$key] += $goods_number; //加数量
    		}else{
    			$cartData[$key] = $goods_number; // 直接加商品
    		}
            //存储到cookie中,存储一个星期
    		cookie("cartData",$cartData,time() + 86400 * 7); 
            return true;
    	}
	}

	/**
	 *  获取购物车中的商品
	 */
    public function getCartGoodsData(){
    	$member_id = session("member_id");
    	//获取购物车的商品
    	if($member_id > 0){
    		//用户登陆
    		$condition = array(
    			'user_id' => $member_id
    		);
    		//把购物车中的商品赋值给当前购物对象的cartData属性,用于保存购物车中的商品
    		$cartData = M('Cart')->where($condition)->select(); 
    	}else{
    		//用户未登陆
    		//cookie存储数据的结构,下标为 goods_id-goods_attr_ids => 值goods_number
    		$cookieData = cookie('cartData')?cookie('cartData'):array();
    		$cartData = array();
    		foreach ($cookieData as $k => $v) {
    			$key = explode('-',$k);
    			$cartData[] = array(
    				'goods_id'=>$key[0],
    				'goods_attr_ids'=>$key[1],
    				'goods_number'=>$v
    			);
    		}

    	}
    	//获取购物车商品的信息和商品的单选属性信息
    	foreach ($cartData as $k => $v) {
    		//获取商品基本信息保存到购物车每个商品Goods属性中
    		$cartData[$k]['Goods'] = D("Goods")->find($v['goods_id']);
    		//获取商品单选属性信息保存到购物车每个商品的GoodsAttr属性中
    		if($v['goods_attr_ids']){
    			//有单选属性 (每个单选属性通过group_CONCAT()链接在一起去,并通过隔开)
    			$cartData[$k]['GoodsAttr'] = D('GoodsAttr')
								->field("group_CONCAT( t2.attr_name,' : ',t1.attr_value,'+',t1.attr_price,'元' separator '') as attr,sum(t1.attr_price) as attr_total_price")
								->join("t1 left join sh_attribute t2 on t1.attr_id = t2.attr_id ")
								->where("t1.goods_attr_id in ({$v['goods_attr_ids']})")
								->find();
    		}else{
    			//无单选属性设置为空字符即可
    			$cartData[$k]['GoodsAttr'] = '';
    		}
			
    	}
        //返回购物车数据
        return $cartData;
    }

    /**
     * 清空购物车中的商品
     */
    public function clearCart(){
        $member_id = session("member_id")?:0;
        if($member_id > 0){
            //用户登录了
            return M("Cart")->where("user_id=" .$member_id)->delete();
        }else{
            //未登录情况
            cookie('cartData',null,time() - 1);
            return true;
        }
    }

    /**
     * 购物车商品的删除
     */
    public function delCartGoods($goods_id,$goods_attr_ids){
        $member_id = session("member_id")?:0;
        //判断用户是否登陆
        if($member_id > 0){
            //已登录
            $condition = array(
                'user_id' => $member_id,
                'goods_attr_ids' => $goods_attr_ids,
                'goods_id' => $goods_id
            );
            return M('Cart')->where($condition)->delete(); //删除
        }else{
            //未登录
            $cartData = cookie('cartData')?cookie('cartData'):array();
            $key = $goods_id.'-'.$goods_attr_ids;
            unset($cartData[$key]); //直接删除已有的键值
            //删除之后再在存储到cookie中,存储一个星期
            cookie("cartData",$cartData, 86400 *7); 
            return true;
        }
    }

    /**
     * 获取购物车商品总数
     * @return  返回购物车商品的总数
     */
    public function getCartCount(){
        return count( $this->getCartGoodsData() );
    }


    

}
网友评论