PHP Shopping Cart Category

Source: Internet
Author: User

<?PHP/** * Shopping cart category*/Session_Start();classcart{Private Static $ins=NULL; Private $items=Array(); protected function__construct () {}//Get Instance    protected Static functionGetins () {if(! (Self::$insinstanceof Self)) { Self::$ins=NewSelf (); }        returnSelf::$ins; }    //Put the cart's singleton into session     Public Static functionGetcart () {if(!isset($_session[' cart ']) || ! ($_session[' Cart '] (instanceof Self)) {$_session[' cart '] = self::Getins (); }        return  $_session[' Cart ']; }    /** * Add Item * @param int $id Item ID * @param string $name product name * @param float $price commodity price * @param int $num quantity of goods purchased*/     Public functionAddItem ($id,$name,$price,$num=1){        #The item is already in the shopping, add its quantity        if($this->hasitem ($id)){            $this->inccartnum ($id,$num); return true; }        $this->items[$id] =Array(            ' id ' =$id, ' name ' = =$name, ' price ' =$price, ' num ' =$num        ); return true; }    /** * Empty cart*/     Public functionClearcart () {$this->items =Array(); }    /** * Determine if a product exists * @param int $id Product ID*/     Public functionHasitem ($id){        return array_key_exists($id,$this-items); }    /** * Modify the number of items in your cart * @param int $id commodity ID * @param int $num The number of changes to a product, that is, to change the quantity of an item directly to $num*/     Public functionModifycartnum ($id,$num=1){        if(!$this->hasitem ($id)){            return false; }        $this->items[$id[' num '] =$num; }    /** * 1 increase in product volume*/     Public functionInccartnum ($id,$num=1){        if($this->hasitem ($id)){            $this->items[$id[' num '] + =$num; }    }    /** * 1 reduction in product volume*/     Public functionDeccartnum ($id,$num=1){        if($this->hasitem ($id)){            $this->items[$id[' num ']-=$num; }        if($this->items[$id[' Num '] < 1){            $this->deletecartgoods ($id); }    }    /** * Delete items*/     Public functionDeletecartgoods ($id){        return unset($this->items[$id]); }    /** * Check the number of shopping cart product categories*/     Public functionGetCount () {return Count($this-items); }    /** * Check the number of shopping cart items*/     Public functionGetgoodsnum () {if($this->getcount = = 0)return0; $sum= 0; foreach($this->items as $item){            $sum+=$item[' Num ']; }        return $sum; }    /** * Check the total amount of shopping cart items*/     Public functiongetgoodspricetotal () {if($this->getcount = = 0)return0; $price= 0.0; foreach($this->items as $item){            $price+=$item[' Num ']*$item[' Price ']; }        return $price; }    /** * Return all items in cart*/     Public functiongetcartlist () {if($this->getcount = = 0)return false; return $this-items; }}?>

PHP Shopping Cart Category

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.