Cart\Cart::add PHP Method

add() public method

Add an item to the cart.
public add ( cart\CartItem $cartItem )
$cartItem cart\CartItem
    public function add(CartItem $cartItem)
    {
        $itemId = $cartItem->getId();
        // if item already exists in the cart, just update the quantity,
        // otherwise add it as a new item
        if ($this->has($itemId)) {
            $existingItem = $this->find($itemId);
            $existingItem->quantity += $cartItem->quantity;
        } else {
            $this->items[] = $cartItem;
        }
    }

Usage Example

Beispiel #1
0
 public function testClear()
 {
     $store = m::mock('Cart\\Storage\\Store');
     $store->shouldReceive('flush')->times(1);
     $cart = new Cart('foo', $store);
     $item1 = new CartItem(array('name' => 'foo'));
     $item2 = new CartItem(array('name' => 'bar'));
     $cart->add($item1);
     $cart->add($item2);
     $cart->clear();
     $cartItems = $cart->all();
     $this->assertTrue(count($cartItems) == 0);
 }
All Usage Examples Of Cart\Cart::add