PHP Shopping Cart class implementation code (Singleton mode)

Source: Internet
Author: User
  1. /**

  2. * PHP Shopping Cart Category
  3. * Single case mode
  4. * Edit bbs.it-home.org
  5. */
  6. Class cart{
  7. static protected $ins; Instance variable
  8. protected $item = Array (); Put commodity containers

  9. Prohibit external calls

  10. Final protected function __construct () {
  11. }

  12. Prohibit cloning

  13. Final protected function __clone () {
  14. }

  15. Inner instantiation of class

  16. Static protected function Getins () {
  17. if (! ( Self:: $ins instanceof Self)) {
  18. Self:: $ins = new self ();
  19. }

  20. Return self:: $ins;

  21. }

  22. In order to enable the product to be saved across pages, put the object into the session

  23. Public Function Getcat () {
  24. if (! ( $_session[' cat '] | | ! ($_session[' cat '] instanceof self)) {
  25. $_session[' cat '] = Self::getins ();
  26. }

  27. Return $_session[' cat '];

  28. }

  29. into row when the test, whether in the $item exist.

  30. Public Function Initem ($goods _id) {
  31. if ($this->gettype () = = 0) {
  32. return false;
  33. }
  34. if (! ( Array_key_exists ($goods _id, $this->item))) {
  35. return false;
  36. }else{
  37. return $this->item[$goods _id][' num ']; Returns the number of items
  38. }
  39. }

  40. Add a Product

  41. Public Function Additem ($goods _id, $name, $num, $price) {
  42. if ($this->initem ($goods _id)! = False) {
  43. $this->item[$goods _id][' num '] + = $num;
  44. Return
  45. }

  46. $this->item[$goods _id] = array (); A product is an array

  47. $this->item[$goods _id][' num '] = $num; The number of purchases for this item
  48. $this->item[$goods _id][' name '] = $name; Product Name
  49. $this->item[$goods _id][' price ' = $price; Commodity price
  50. }

  51. Reduce one item

  52. Public Function Reduceitem ($goods _id, $num) {
  53. if ($this->initem ($goods _id) = = False) {
  54. Return
  55. }
  56. if ($num > $this->getunm ($goods _id)) {
  57. unset ($this->item[$goods _id]);
  58. }else{
  59. $this->item[$goods _id][' num ']-= $num;
  60. }
  61. }

  62. Remove a product

  63. Public Function Delitem ($goods _id) {
  64. if ($this->initem ($goods _id)) {
  65. unset ($this->item[$goods _id]);
  66. }
  67. }

  68. Back to list of purchased items

  69. Public Function Itemlist () {
  70. return $this->item;
  71. }

  72. How many kinds of goods are there?

  73. Public Function Gettype () {
  74. Return count ($this->item);
  75. }

  76. Get the total number of a product

  77. Public Function getunm ($goods _id) {
  78. return $this->item[$goods _id][' num '];
  79. }

  80. Find out how many items are in your shopping cart

  81. Public Function GetNumber () {
  82. $num = 0;
  83. if ($this->gettype () = = 0) {
  84. return 0;
  85. }

  86. foreach ($this->item as $k = + $v) {

  87. $num + = $v [' num '];
  88. }
  89. return $num;
  90. }

  91. Calculate Total Price

  92. Public Function GetPrice () {
  93. $price = 0;
  94. if ($this->gettype () = = 0) {
  95. return 0;
  96. }

  97. foreach ($this->item as $k = + $v) {

  98. $price + = $v [' num ']* $v [' num '];
  99. }
  100. return $price;
  101. }

  102. Empty shopping Cart

  103. Public Function Emptyitem () {
  104. $this->item = Array ();
  105. }
  106. }
  107. ?>

Copy Code

Invocation Example:

    1. Include_once (' cart.php ');
    2. $cart = Cart::getcat ();
    3. $cart->additem (' 1 ', ' PHP Learning Tutorial (Programmer's Home version) ', ' 5 ', ' 9999 ');
    4. Print_r ($cart);
    5. ?>
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.