Php shopping cart implementation code example
// Shopping cart type
- /*
- Instructions for use:
- The constructor cart can use the following parameters:
- Cart ($ cartname = 'mycart', $ session_id = '', $ savetype = 'session', $ cookietime = 86400, $ cookiepath = '/', $ cookiedomain = '')
- $ Cartname is the identifier of the shopping cart. it can be specified to ensure no duplicate names and no conflicts.
- $ Session_id is session_id, which is transmitted using cookies by default. It can also be customized. it takes effect only when the storage type is session.
- $ Savetype storage type, with session and cookie methods
- ... Other parameters required by cookies
When the program uses session, we recommend that you change the php shopping cart class to cookie.
// Add a product
- // Reference class
- Require_once './cart. class. php ';
- // Create a class instance
- $ Cart = new cart ();
// The item has been modified.
- 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 the shopping cart data
- $ Cart-> save ();
Edit the number of items
- // Reference class
- Require_once './cart. class. php ';
- // Create a class instance
- $ Cart = new cart ();
// The item has been modified.
- If ($ cart-> data [$ id]) {
- $ Cart-> data [$ id] ['count'] = $ count;
- $ Cart-> data [$ id] ['Money'] = $ cart-> data [$ id] ['price'] * $ count;
// Save the shopping cart data
- $ Cart-> save ();
- }
Delete a product
- // Reference class
- Require_once './cart. class. php ';
- // Create a class instance
- $ Cart = new cart ();
// Delete the product
- Unset ($ cart-> data [$ id]);
// Save the shopping cart data
- $ Cart-> save ();
List shopping cart
- // Reference class
- Require_once './cart. class. php ';
- // Create a class instance
- $ Cart = new cart ();
Foreach ($ cart-> data AS $ k => $ v ){
- Echo 'item ID: '. $ k;
- Echo 'item name: '. $ v ['name'];
- Echo 'unit price: '. $ v ['price'];
- Echo 'item quantity: '. $ v ['count'];
- Echo 'total Price: '. $ v ['Money'];
- }
Total sum of a field --- for example, total price of all commodities
- // Reference class
- Require_once './cart. class. php ';
- // Create a class instance
- $ Cart = new cart ();
// Accumulative money field
- $ Cart-> sum ('Money ')
Clear shopping cart
- // Reference class
- Require_once './cart. class. php ';
- // Create a class instance
- $ Cart = new cart ();
// Clear data
- Unset ($ cart-> data );
// Save the shopping cart data
- $ Cart-> save ();
- */
// Shopping cart type
- // Edit bbs.it-home.org
- Class cart {
// Shopping cart ID
- Var $ cartname = '';
- // Storage type
- Var $ savetype = '';
- // Item 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 one day, $ cookiepath, $ cookiedomain)
- Function cart ($ cartname = 'mycart', $ session_id = '', $ savetype = 'session', $ cookietime = 86400, $ cookiepath = '/', $ cookiedomain = ''){
// Use 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 ();
- }
// 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 = 'cookies '){
- If ($ _ COOKIE [$ this-> cartname])
- $ This-> data = unserialize ($ _ COOKIE [$ this-> cartname]);
- Else
- $ This-> data = array ();
- }
- }
// Save the shopping cart data
- Function save (){
- If ($ this-> savetype = 'session '){
- $ _ SESSION [$ this-> cartname] = $ this-> data;
- } Elseif ($ this-> savetype = 'cookies '){
- If ($ this-> data)
- Setcookie ($ this-> cartname, serialize ($ this-> data), $ this-> cookietime + time (), $ this-> cookiepath, $ this-> cookiedomain );
- }
- }
// Returns a product field to accumulate
- Function sum ($ field ){
$ Sum = 0;
- If ($ this-> data)
- Foreach ($ this-> data AS $ v)
- If ($ v [$ field])
- $ Sum + = $ v [$ field] + 0;
Return $ sum;
- }
- }
- ?>
|