PHP Shopping Cart Class code
In the development of online shopping sites, shopping cart class is a necessary module for shopping sites. Summarize a PHP implementation shopping cart class. The products in the shopping cart are added, modified, deleted, listed, and related functions of various calculations are realized. Using PHP Single-class principle, safe and efficient, simple and easy to expand.
Class cart{
static protected $ins; Instance variable
Protected $item =array (); Put commodity containers
Prohibit external calls
Final protected function __construct () {}
Prohibit cloning
Final protected function __clone () {}
Inner instantiation of class
Static protected function Getins () {
if (! ( Self:: $ins-instanceof self) {self:: $ins =new-Self ();} Return self:: $ins;
}
In order to enable the product to be saved across pages, put the object into the session
Public Function Getcat () {
if (!isset ($_session[' cat ') | |! ($_session[' cat '] instanceof self)) {
$_session[' Cat ']=self::getins ();
}
Return $_session[' cat '];
}
into row test, whether it exists in the $item
Public Function Initem ($goods _id) {
if ($this->gettype () ==0) {
return false;
}
Check whether the goods are the same here by goods_id to detect, not through the product name name to detect, specific circumstances can be modified
if (! ( Array_key_exists ($goods _id, $this->item))) {
return false;
}else{
return $this->item[$goods _id][' num ']; Returns the number of items
}
}
Add a Product
Public Function Additem ($goods _id, $name, $num, $price) {
if ($this->initem ($goods _id)!=false) {
$this->item[$goods _id][' num ']+= $num;
Return
}
$this->item[$goods _id]=array (); A product is an array
$this->item[$goods _id][' num ']= $num; The number of purchases for this item
$this->item[$goods _id][' name ']= $name; Product Name
$this->item[$goods _id][' price ']= $price; Commodity price
}
Reduce one item
Public Function Reduceitem ($goods _id, $num) {
if ($this->initem ($goods _id) ==false) {
Return
}
if ($num > $this->getunm ($goods _id)) {
unset ($this->item[$goods _id]);
}else{
$this->item[$goods _id][' num ']-= $num;
}
}
Remove a product
Public Function Delitem ($goods _id) {
if ($this->initem ($goods _id)) {
unset ($this->item[$goods _id]);
}
}
Back to list of purchased items
Public Function Itemlist () {
return $this->item;
}
How many kinds of goods are there?
Public Function Gettype () {
Return count ($this->item);
}
Get the total number of a product
Public Function getunm ($goods _id) {
return $this->item[$goods _id][' num '];
}
Find out how many items are in your shopping cart
Public Function GetNumber () {
$num = 0;
if ($this->gettype () ==0) {
return 0;
}
foreach ($this->item as $k = + $v) {
$num + = $v [' num '];
}
return $num;
}
Calculate Total Price
Public Function GetPrice () {
$price = 0;
if ($this->gettype () ==0) {
return 0;
}
foreach ($this->item as $k = + $v) {
$price + = $v [' num ']* $v [' num '];
}
return $price;
}
Empty shopping Cart
Public Function Emptyitem () {
$this->item=array ();
}
}
An example of the above shopping cart class invocation is as follows:
Header ("Content-type:text/html;charset=utf-8");
Session_Start ();
$cart = Cart::getcat ();
$cart->additem (' 1 ', ' www.phpernote.com ', ' 1 ', ' 100 million ');
$cart->additem (' 2 ', ' PHP Shopping cart class ', ' 3 ', ' 10 ');
Print_r ($cart);