Shopping Cart Category
- /*
- Instructions for use:
- constructor cart can use parameters:
- Cart ($cartname = ' Mycart ', $session _id = ', $savetype = ' Session ', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '')
- $cartname is the logo of the shopping cart, can be specified, can guarantee no duplicate name, there will be no related conflict
- $session _id is session_id, the default is to use a cookie to transfer, or can be customized, if the storage type is session is not effective
- $savetype storage type, with session and cookie mode
- ... Others are the parameters required by the cookie
When the program itself is used to the session, it is recommended to change this PHP shopping cart class to a cookie implementation.
Add a Product
- Reference class
- Require_once './cart.class.php ';
- Creating class instances
- $cart = new cart ();
The item already exists modification data
- if ($cart->data[$id]) {
- $cart->data[$id [' count '] + = $count;
- $cart->data[$id [' money '] + = $cart->data[$id] [' Price '] * $count;
- Add Item
- } else {
- $cart->data[$id [' name '] = $name;
- $cart->data[$id [' price '] = $price;
- $cart->data[$id [' count '] = $count;
- $cart->data[$id [' money '] = $price * $COUNT;
- }
- Save Shopping Cart Data
- $cart->save ();
Edit an item quantity
- Reference class
- Require_once './cart.class.php ';
- Creating class instances
- $cart = new cart ();
The item already exists modification data
- if ($cart->data[$id]) {
- $cart->data[$id [' count '] = $count;
- $cart->data[$id [' money '] = $cart->data[$id] [' Price '] * $count;
Save Shopping Cart Data
- $cart->save ();
- }
Delete a product
- Reference class
- Require_once './cart.class.php ';
- Creating class instances
- $cart = new cart ();
Delete Item
- unset ($cart->data[$id]);
Save Shopping Cart Data
- $cart->save ();
List Shopping Cart
- Reference class
- Require_once './cart.class.php ';
- Creating class instances
- $cart = new cart ();
foreach ($cart->data as $k = + $v) {
- Echo ' Product ID: '. $k;
- Echo ' Product name: '. $v [' name '];
- Echo ' Unit Price: '. $v [' prices '];
- Echo ' Number of items: '. $v [' count '];
- Echo ' Total merchandise: '. $v [' money '];
- }
Total cumulative---of a field such as Total price of all goods
- Reference class
- Require_once './cart.class.php ';
- Creating class instances
- $cart = new cart ();
Accumulated Money Field
- $cart->sum (' money ')
Empty shopping Cart
- Reference class
- Require_once './cart.class.php ';
- Creating class instances
- $cart = new cart ();
Clear Data
- unset ($cart->data);
Save Shopping Cart Data
- $cart->save ();
- */
Shopping Cart Category
- Edit bbs.it-home.org
- Class Cart {
Shopping Cart Logo
- var $cartname = ';
- Storage type
- var $savetype = ';
- Product data in Shopping cart
- var $data = array ();
- Cookie data
- var $cookietime = 0;
- var $cookiepath = '/';
- var $cookiedomain = ';
Constructor (shopping cart identification, $session _id, storage type (session or cookie), default is day time, $cookiepath, $cookiedomain)
- function cart ($cartname = ' Mycart ', $session _id = ', $savetype = ' Session ', $cookietime = 86400, $cookiepath = '/', $cook Iedomain = ") {
Using Session Storage
- if ($savetype = = ' Session ') {
if (! $session _id && $_cookie[$cartname. ' _session_id ']) {
- session_id ($_cookie[$cartname. ' _session_id ']);
- } elseif ($session _id)
- session_id ($session _id);
Session_Start ();
if (! $session _id &&!$_cookie[$cartname. ' _session_id '])
- Setcookie ($cartname. ' _session_id ', session_id (), $cookietime + Time (), $cookiepath, $cookiedomain);
- }
$this->cartname = $cartname;
- $this->savetype = $savetype;
- $this->cookietime = $cookietime;
- $this->cookiepath = $cookiepath;
- $this->cookiedomain = $cookiedomain;
- $this->readdata ();
- }
Reading data
- function ReadData () {
- if ($this->savetype = = ' Session ') {
- if ($_session[$this->cartname] && is_array ($_session[$this->cartname]))
- $this->data = $_session[$this->cartname];
- Else
- $this->data = Array ();
- } elseif ($this->savetype = = ' Cookie ') {
- if ($_cookie[$this->cartname])
- $this->data = unserialize ($_cookie[$this->cartname]);
- Else
- $this->data = Array ();
- }
- }
Save Shopping Cart Data
- function Save () {
- if ($this->savetype = = ' Session ') {
- $_session[$this->cartname] = $this->data;
- } elseif ($this->savetype = = ' Cookie ') {
- if ($this->data)
- Setcookie ($this->cartname, serialize ($this->data), $this->cookietime + time (), $this->cookiepath, $this- >cookiedomain);
- }
- }
Returns a field increment of a product
- function sum ($field) {
$sum = 0;
- if ($this->data)
- foreach ($this->data as $v)
- if ($v [$field])
- $sum + = $v [$field] + 0;
return $sum;
- }
- }
- ?>
Copy Code |