-
-
- // Shopping cart class
- /*
- Instructions for use:
- Constructor cart can use parameters:
- cart($cartname = 'myCart', $session_id = '', $savetype = 'session', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '')
- $cartname is the identification of the shopping cart, which can be specified to ensure that there is no duplicate name and no related conflicts
- $ session_id is the session_id. The default is to use cookies to transmit. It can also be customized. It will only work if the storage type is session. $savetype storage type, there are session and cookie methods.
- ... Others are parameters required by cookies
When the program itself uses session, it is recommended to change this php shopping cart class to cookie implementation.
//Add a product
- // Reference class
- require_once './cart.class.php';
- // Create class instance
- $cart = new cart(); p>
// The product already has modified data
- if ($cart->data[$id]) {
- $cart->data[$id]['count'] += $count;
- $cart->data[$id]['money'] += $cart->data[$id]['price'] * $count;
- // Add product
- } 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();< /p>
Edit a product quantity
- // Reference class
- require_once './cart.class.php';
- // Create a class instance
- $cart = new cart();
-
// The product already has modified 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';
- // Create class instance
- $cart = new cart();
// Delete items
- unset($cart->data[$id]);
// Save shopping cart data
- $cart->save();
List shopping cart
- // Reference class
- require_once './cart.class.php';
- // Create class instance
- $cart = new cart();
foreach ($cart->data AS $k => $v) {
- echo 'Product ID: '.$k;
- echo 'Product name: '.$v['name'];
- echo 'Unit price of product: '.$v['price'];
- echo 'Quantity of product: '.$v['count'];
- echo 'Total price of product: ' .$v['money'];
- }
The total accumulation of a certain field---such as the total price of all products
- // Reference class
- require_once './cart.class.php';
- // Create a class instance
- $cart = new cart();
// Accumulate money field
- $cart->sum('money')
- < p>Clear shopping cart
- // Reference class
- require_once './cart.class.php';
- // Create class instance
- $cart = new cart();
// Clear Data
- unset($cart->data);
// Save shopping cart data
- $cart->save();
- */
- //edit bbs.it-home.org
- class cart {
// Shopping cart identifier
- var $cartname = '';
- // Storage type
- var $savetype = '';
- // Product data in the shopping cart
- var $data = array();
- // Cookie data
- var $cookietime = 0;
- var $cookiepath = '/';
- var $cookiedomain = '';
// Constructor (shopping cart ID, $session_id, storage type (session or cookie), default is time of day, $cookiepath, $cookiedomain)
- function cart($cartname = 'myCart', $session_id = '', $savetype = 'session', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '') {
// Adopt 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);
- }
- < ;p>$this->cartname = $cartname;
- $this->savetype = $savetype;
- $this->cookietime = $cookietime;
- $this->cookiepath = $cookiepath;
- $this- >cookiedomain = $cookiedomain;
- $this->readdata();
- }
// Read 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);
- }
- }
// 返回商品某字段累加
- function sum($field) {
$sum = 0;
- if ($this->data)
- foreach ($this->data AS $v)
- if ($v[$field])
- $sum += $v[$field] + 0;
return $sum;
- }
- }
- ?>
-
复制代码
|