PHP Shopping Cart Class implementation code example

Source: Internet
Author: User
Tags setcookie
  1. Shopping Cart Category

  2. /*
  3. Instructions for use:
  4. constructor cart can use parameters:
  5. Cart ($cartname = ' Mycart ', $session _id = ', $savetype = ' Session ', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '')
  6. $cartname is the logo of the shopping cart, can be specified, can guarantee no duplicate name, there will be no related conflict
  7. $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
  8. $savetype storage type, with session and cookie mode
  9. ... Others are the parameters required by the cookie

  10. When the program itself is used to the session, it is recommended to change this PHP shopping cart class to a cookie implementation.

  11. Add a Product

  12. Reference class
  13. Require_once './cart.class.php ';
  14. Creating class instances
  15. $cart = new cart ();

  16. The item already exists modification data

  17. if ($cart->data[$id]) {
  18. $cart->data[$id [' count '] + = $count;
  19. $cart->data[$id [' money '] + = $cart->data[$id] [' Price '] * $count;
  20. Add Item
  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 Shopping Cart Data
  28. $cart->save ();

  29. Edit an item quantity

  30. Reference class
  31. Require_once './cart.class.php ';
  32. Creating class instances
  33. $cart = new cart ();

  34. The item already exists modification data

  35. if ($cart->data[$id]) {
  36. $cart->data[$id [' count '] = $count;
  37. $cart->data[$id [' money '] = $cart->data[$id] [' Price '] * $count;

  38. Save Shopping Cart Data

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

  41. Delete a product

  42. Reference class
  43. Require_once './cart.class.php ';
  44. Creating class instances
  45. $cart = new cart ();

  46. Delete Item

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

  48. Save Shopping Cart Data

  49. $cart->save ();

  50. List Shopping Cart

  51. Reference class
  52. Require_once './cart.class.php ';
  53. Creating class instances
  54. $cart = new cart ();

  55. foreach ($cart->data as $k = + $v) {

  56. Echo ' Product ID: '. $k;
  57. Echo ' Product name: '. $v [' name '];
  58. Echo ' Unit Price: '. $v [' prices '];
  59. Echo ' Number of items: '. $v [' count '];
  60. Echo ' Total merchandise: '. $v [' money '];
  61. }

  62. Total cumulative---of a field such as Total price of all goods

  63. Reference class
  64. Require_once './cart.class.php ';
  65. Creating class instances
  66. $cart = new cart ();

  67. Accumulated Money Field

  68. $cart->sum (' money ')

  69. Empty shopping Cart

  70. Reference class
  71. Require_once './cart.class.php ';
  72. Creating class instances
  73. $cart = new cart ();

  74. Clear Data

  75. unset ($cart->data);

  76. Save Shopping Cart Data

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

  79. Shopping Cart Category

  80. Edit bbs.it-home.org
  81. Class Cart {

  82. Shopping Cart Logo

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

  92. Constructor (shopping cart identification, $session _id, storage type (session or cookie), default is day time, $cookiepath, $cookiedomain)

  93. function cart ($cartname = ' Mycart ', $session _id = ', $savetype = ' Session ', $cookietime = 86400, $cookiepath = '/', $cook Iedomain = ") {

  94. Using 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. Reading 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 = = ' Cookie ') {
  119. if ($_cookie[$this->cartname])
  120. $this->data = unserialize ($_cookie[$this->cartname]);
  121. Else
  122. $this->data = Array ();
  123. }
  124. }

  125. Save Shopping Cart Data

  126. function Save () {
  127. if ($this->savetype = = ' Session ') {
  128. $_session[$this->cartname] = $this->data;
  129. } elseif ($this->savetype = = ' Cookie ') {
  130. if ($this->data)
  131. Setcookie ($this->cartname, serialize ($this->data), $this->cookietime + time (), $this->cookiepath, $this- >cookiedomain);
  132. }
  133. }

  134. Returns a field increment of a product

  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. ?>

Copy Code
  • 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.