Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * cart object
- *
- * @author dfare - fare development
- * @since 2011-03-26
- */
- class cart {
- var $link;
- var $userId;
- var $itemArr;
- /** contructors */
- public function Cart( $mysqlLink, $userId, $itemArr = array() ) {
- $this->__construct($mysqlLink, $userId, $itemArr);
- }
- public function __construct( $mysqlLink, $userId, $itemArr = array() ) {
- $this->link = $mysqlLink;
- $this->userId = $userId;
- $this->itemArr = $itemArr;
- }
- /**
- * empty item array
- *
- * @return bool if items were cleared
- */
- public function clearItems() {
- $itemArr = array();
- if ( count($itemArr) == 0 ) {
- return true;
- }
- return false;
- }
- /**
- * add item to cart
- * @param object product object (product.class.php)
- * @param int quantity of product
- * @attributesArr array of attributes (would contact size, color, etc)
- *
- * @return bool if item was added
- */
- public function addItem( $productObj, $quantity, $attributesArr = array() ) {
- if ( !is_object($productObj) || !is_numeric($quantity) || !is_array($attributesArr) ) {
- return false;
- }
- $init = count($this->itemArr);
- array_push($this->itemArr, array('product' => $productObj,
- 'quantity' => $quantity,
- 'attributes' => $attributesArr));
- if ( count($this->itemArr) == $init + 1 ) {
- return true;
- }
- return false;
- }
- /**
- * get items in cart
- *
- * @return array of items in cart
- * ex: $array[#]['key']
- * -- # being cart item id,
- * key being "product","quantity" etc
- */
- public function getItems() {
- return $this->itemArr;
- }
- /**
- * removes item in cart (cart item id based on $this->getItems())
- *
- * @param int cart item id $this->getItems()
- *
- * @return bool was item deleted
- */
- public function remItem( $itemId ) {
- $init = count($this->itemArr);
- //make sure $this->itemArr[$itemId] exists
- if ( !is_numeric($itemId) || (count($this->itemArr) - 1 < $itemId || $itemId < 0) || !isset($this->itemArr[$itemId]) ) {
- return false;
- }
- $newArr = array();
- foreach ( $this->itemArr as $id => $array ) {
- if ( $itemId == $id ) {
- continue;
- }
- array_push($newArr, $this->itemArr[$id]);
- }
- $this->itemArr = $newArr;
- if ( count($this->itemArr) == $init - 1 ) {
- return true;
- }
- return false;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment