Guest User

Untitled

a guest
Jan 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. <?php
  2. /*
  3. * cart object
  4. *
  5. * @author dfare - fare development
  6. * @since 2011-03-26
  7. */
  8.  
  9.  
  10. class cart {
  11.  
  12. var $link;
  13. var $userId;
  14. var $itemArr;
  15.  
  16. /** contructors */
  17. public function Cart( $mysqlLink, $userId, $itemArr = array() ) {
  18. $this->__construct($mysqlLink, $userId, $itemArr);
  19. }
  20. public function __construct( $mysqlLink, $userId, $itemArr = array() ) {
  21. $this->link = $mysqlLink;
  22. $this->userId = $userId;
  23. $this->itemArr = $itemArr;
  24. }
  25.  
  26. /**
  27. * empty item array
  28. *
  29. * @return bool if items were cleared
  30. */
  31. public function clearItems() {
  32. $itemArr = array();
  33. if ( count($itemArr) == 0 ) {
  34. return true;
  35. }
  36. return false;
  37. }
  38.  
  39. /**
  40. * add item to cart
  41. * @param object product object (product.class.php)
  42. * @param int quantity of product
  43. * @attributesArr array of attributes (would contact size, color, etc)
  44. *
  45. * @return bool if item was added
  46. */
  47. public function addItem( $productObj, $quantity, $attributesArr = array() ) {
  48. if ( !is_object($productObj) || !is_numeric($quantity) || !is_array($attributesArr) ) {
  49. return false;
  50. }
  51.  
  52. $init = count($this->itemArr);
  53.  
  54. array_push($this->itemArr, array('product' => $productObj,
  55. 'quantity' => $quantity,
  56. 'attributes' => $attributesArr));
  57.  
  58. if ( count($this->itemArr) == $init + 1 ) {
  59. return true;
  60. }
  61. return false;
  62. }
  63.  
  64. /**
  65. * get items in cart
  66. *
  67. * @return array of items in cart
  68. * ex: $array[#]['key']
  69. * -- # being cart item id,
  70. * key being "product","quantity" etc
  71. */
  72. public function getItems() {
  73. return $this->itemArr;
  74. }
  75.  
  76. /**
  77. * removes item in cart (cart item id based on $this->getItems())
  78. *
  79. * @param int cart item id $this->getItems()
  80. *
  81. * @return bool was item deleted
  82. */
  83.  
  84. public function remItem( $itemId ) {
  85. $init = count($this->itemArr);
  86.  
  87. //make sure $this->itemArr[$itemId] exists
  88. if ( !is_numeric($itemId) || (count($this->itemArr) - 1 < $itemId || $itemId < 0) || !isset($this->itemArr[$itemId]) ) {
  89. return false;
  90. }
  91.  
  92. $newArr = array();
  93.  
  94. foreach ( $this->itemArr as $id => $array ) {
  95. if ( $itemId == $id ) {
  96. continue;
  97. }
  98. array_push($newArr, $this->itemArr[$id]);
  99. }
  100.  
  101. $this->itemArr = $newArr;
  102.  
  103. if ( count($this->itemArr) == $init - 1 ) {
  104. return true;
  105. }
  106. return false;
  107. }
  108.  
  109.  
  110.  
  111. }
  112. ?>
Add Comment
Please, Sign In to add comment