php购物车类

    <?php 
    /**
     * 购物车类
     * 功能:
     * 添加商品
     * 删除商品
     * 商品列表
     * 查看商品
     * 商品+1
     * 商品-1
     * 商品种类
     * 商品个数
     * 商品小计
     * 商品总计
     */
    class cart{
        protected static $ins = null;
        protected $basket;
        protected function __construct(){

        }
        protected static function getIns(){
            if (self::$ins == null) {
                # code...
                self::$ins = new self();
            }
            return self::$ins;
        }
        /**/
        public static function getCar(){
            if( !isset($_SESSION['car']) || !($_SESSION['car'] instanceof self) ){
                $_SESSION['car'] = self::getIns();
            }
            return $_SESSION['car'];
        }
        /**
         * 添加商品
         */
        public function addItems($goods_id,$goods_name,$shop_price,$goods_num,$goods_thumb){
            /*判断id是否存在*/
            if (!$this->hasItems($goods_id)) {
                # code...
                $this->basket[$goods_id] = array(
                    'goods_id'=>$goods_id,
                    'goods_name'=>$goods_name,
                    'shop_price'=>$shop_price,
                    'goods_num'=>$goods_num,
                    'goods_thumb'=>$goods_thumb
                    );
            }else{
                $this->basket[$goods_id]['goods_num'] += $goods_num;
            }
        }
        /**
         * 删除一种商品
         */
        public function delItems($goods_id){
            if ($this->hasItems($goods_id)) {
                unset($this->basket[$goods_id]);
            }
        }
        protected function hasItems($goods_id){
            return array_key_exists($goods_id,$this->basket);
        }
        /**
         * 列出商品
         */
        public function listItems(){
            return $this->basket;
        }
        /**
         * 清空购物车
         */
        public function clear(){
            $this->basket = array();
        }
        /**
         * 计算商品种类
         */
        public function cnt(){
            return count($this->basket);
        }
        /**
         * 计算商品个数
         */
        public function getCount(){
            $count = 0;
            if (empty($this->basket)) {
                # code...
                return $count;
            }
            foreach ($this->basket as $v) {
                # code...
                $count += $v['goods_num'];
            }
            return $count;
        }
        /**
         * 计算商品总价格
         */
        public function getPrice(){
            $total = 0;
            if (empty($this->basket)) {
                # code...
                return $total;
            }
            foreach ($this->basket as $v) {
                # code...
                $total += $v['shop_price']*$v['goods_num'];
            }
            return $total;
        }
        /**
         * 修改商品数量
         */
        public function upItems($goods_id){
            if(!$this->hasItems($goods_id)){
                return '你的商品id号不存在';
            }else if($this->basket[$goods_id]['goods_num'] <= 0){
                $this->delItems($goods_id);
            }else{
                $this->basket[$goods_id]['goods_num'] = $goods_num;
            }
        }
        /**
         * 商品数量+1
         */
        public function addOne($goods_id){
            if(!$this->hasItems($goods_id)){
                return '你的商品id号不存在';
            }else{
                $this->basket[$goods_id]['goods_num'] += 1;
            }
        }
        /**
         * 商品数量-1
         */
        public function delOne($goods_id){
            if(!$this->hasItems($goods_id)){
                return '你的商品id号不存在';
            }else{
                $this->basket[$goods_id]['goods_num'] -= 1;
            }
        }
    }

?>

 

posted @ 2015-11-05 16:18  人间最美二月天  阅读(432)  评论(0编辑  收藏  举报