Home > Backend Development > PHP Tutorial > Implementation code of php shopping cart class (single case mode)

Implementation code of php shopping cart class (single case mode)

WBOY
Release: 2016-07-25 08:59:33
Original
882 people have browsed it
  1. /**

  2. * php shopping cart class
  3. * Singleton mode
  4. * Edit bbs.it-home.org
  5. */
  6. class Cart{
  7. static protected $ins; //Instance variable
  8. protected $item = array(); //Place items Container

  9. //External calls are prohibited

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

  12. //Cloning is prohibited

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

  15. //Internal instantiation of the 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 save the product 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. //Check when enqueueing, whether it exists in $item.

  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']; //Return the number of this product
  38. }
  39. }

  40. //Add one Goods

  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; //Purchase quantity of this product
  48. $this->item[$goods_id]['name'] = $name; //Product name
  49. $this ->item[$goods_id]['price'] = $price; //unit price of the item
  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 one Goods

  63. public function Delitem($goods_id){
  64. if($this->Initem($goods_id)){
  65. unset($this->item[$goods_id]);
  66. }
  67. }

  68. //Return to the list of purchased items

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

  72. //How many types of items are there in total?

  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. // Check how many items are in the 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 the 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. //Clear the shopping cart

  103. public function Emptyitem(){
  104. $this->item = array();
  105. }
  106. }
  107. ?>

Copy code

Call example:

  1. include_once('Cart.php');
  2. $cart = Cart::Getcat();
  3. $cart->Additem('1','php learning tutorial (programmer Home Edition)','5','9999');
  4. print_r($cart);
  5. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template