Php shopping cart implementation code example

Source: Internet
Author: User
Php shopping cart implementation code example

  1. // Shopping cart type

  2. /*
  3. Instructions for use:
  4. The constructor cart can use the following parameters:
  5. Cart ($ cartname = 'mycart', $ session_id = '', $ savetype = 'session', $ cookietime = 86400, $ cookiepath = '/', $ cookiedomain = '')
  6. $ Cartname is the identifier of the shopping cart. it can be specified to ensure no duplicate names and no conflicts.
  7. $ 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.
  8. $ Savetype storage type, with session and cookie methods
  9. ... Other parameters required by cookies

  10. When the program uses session, we recommend that you change the php shopping cart class to cookie.

  11. // Add a product

  12. // Reference class
  13. Require_once './cart. class. php ';
  14. // Create a class instance
  15. $ Cart = new cart ();

  16. // The item has been modified.

  17. If ($ cart-> data [$ id]) {
  18. $ Cart-> data [$ id] ['count'] + = $ count;
  19. $ Cart-> data [$ id] ['Money'] + = $ cart-> data [$ id] ['price'] * $ count;
  20. // Add product
  21. } Else {
  22. $ Cart-> data [$ id] ['name'] = $ name;
  23. $ Cart-> data [$ id] ['price'] = $ price;
  24. $ Cart-> data [$ id] ['count'] = $ count;
  25. $ Cart-> data [$ id] ['Money'] = $ price * $ count;
  26. }
  27. // Save the shopping cart data
  28. $ Cart-> save ();

  29. Edit the number of items

  30. // Reference class
  31. Require_once './cart. class. php ';
  32. // Create a class instance
  33. $ Cart = new cart ();

  34. // The item has been modified.

  35. If ($ cart-> data [$ id]) {
  36. $ Cart-> data [$ id] ['count'] = $ count;
  37. $ Cart-> data [$ id] ['Money'] = $ cart-> data [$ id] ['price'] * $ count;

  38. // Save the shopping cart data

  39. $ Cart-> save ();
  40. }

  41. Delete a product

  42. // Reference class
  43. Require_once './cart. class. php ';
  44. // Create a class instance
  45. $ Cart = new cart ();

  46. // Delete the product

  47. Unset ($ cart-> data [$ id]);

  48. // Save the shopping cart data

  49. $ Cart-> save ();

  50. List shopping cart

  51. // Reference class
  52. Require_once './cart. class. php ';
  53. // Create a class instance
  54. $ Cart = new cart ();

  55. Foreach ($ cart-> data AS $ k => $ v ){

  56. Echo 'item ID: '. $ k;
  57. Echo 'item name: '. $ v ['name'];
  58. Echo 'unit price: '. $ v ['price'];
  59. Echo 'item quantity: '. $ v ['count'];
  60. Echo 'total Price: '. $ v ['Money'];
  61. }

  62. Total sum of a field --- for example, total price of all commodities

  63. // Reference class
  64. Require_once './cart. class. php ';
  65. // Create a class instance
  66. $ Cart = new cart ();

  67. // Accumulative money field

  68. $ Cart-> sum ('Money ')

  69. Clear shopping cart

  70. // Reference class
  71. Require_once './cart. class. php ';
  72. // Create a class instance
  73. $ Cart = new cart ();

  74. // Clear data

  75. Unset ($ cart-> data );

  76. // Save the shopping cart data

  77. $ Cart-> save ();
  78. */

  79. // Shopping cart type

  80. // Edit bbs.it-home.org
  81. Class cart {

  82. // Shopping cart ID

  83. Var $ cartname = '';
  84. // Storage type
  85. Var $ savetype = '';
  86. // Item data in the shopping cart
  87. Var $ data = array ();
  88. // Cookie data
  89. Var $ cookietime = 0;
  90. Var $ cookiepath = '/';
  91. Var $ cookiedomain = '';

  92. // Constructor (shopping cart ID, $ session_id, storage type (session or cookie), default is one day, $ cookiepath, $ cookiedomain)

  93. Function cart ($ cartname = 'mycart', $ session_id = '', $ savetype = 'session', $ cookietime = 86400, $ cookiepath = '/', $ cookiedomain = ''){

  94. // Use session storage

  95. If ($ savetype = 'session '){

  96. If (! $ Session_id & $ _ COOKIE [$ cartname. '_ session_id']) {

  97. Session_id ($ _ COOKIE [$ cartname. '_ session_id']);
  98. } Elseif ($ session_id)
  99. Session_id ($ session_id );

  100. Session_start ();

  101. If (! $ Session_id &&! $ _ COOKIE [$ cartname. '_ session_id'])

  102. Setcookie ($ cartname. '_ session_id', session_id (), $ cookietime + time (), $ cookiepath, $ cookiedomain );
  103. }

  104. $ This-> cartname = $ cartname;

  105. $ This-> savetype = $ savetype;
  106. $ This-> cookietime = $ cookietime;
  107. $ This-> cookiepath = $ cookiepath;
  108. $ This-> cookiedomain = $ cookiedomain;
  109. $ This-> readdata ();
  110. }

  111. // Read data

  112. Function readdata (){
  113. If ($ this-> savetype = 'session '){
  114. If ($ _ SESSION [$ this-> cartname] & is_array ($ _ SESSION [$ this-> cartname])
  115. $ This-> data =$ _ SESSION [$ this-> cartname];
  116. Else
  117. $ This-> data = array ();
  118. } Elseif ($ this-> savetype = 'cookies '){
  119. If ($ _ COOKIE [$ this-> cartname])
  120. $ This-> data = unserialize ($ _ COOKIE [$ this-> cartname]);
  121. Else
  122. $ This-> data = array ();
  123. }
  124. }

  125. // Save the shopping cart data

  126. Function save (){
  127. If ($ this-> savetype = 'session '){
  128. $ _ SESSION [$ this-> cartname] = $ this-> data;
  129. } Elseif ($ this-> savetype = 'cookies '){
  130. If ($ this-> data)
  131. Setcookie ($ this-> cartname, serialize ($ this-> data), $ this-> cookietime + time (), $ this-> cookiepath, $ this-> cookiedomain );
  132. }
  133. }

  134. // Returns a product field to accumulate

  135. Function sum ($ field ){

  136. $ Sum = 0;

  137. If ($ this-> data)
  138. Foreach ($ this-> data AS $ v)
  139. If ($ v [$ field])
  140. $ Sum + = $ v [$ field] + 0;

  141. Return $ sum;

  142. }
  143. }
  144. ?>

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.