/** * Add an item to the cart. * * @param CartItem $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; } }
public function isApplicable(CartItem $item, array $products = []) { $period = $item->getTerm()->getPeriod(); if (!empty($products)) { if (!array_key_exists($item->getProductId(), $products)) { return false; } $couponProductPeriods = $products[$item->getProductId()]; if (!empty($couponProductPeriods) && !in_array($period, $couponProductPeriods)) { return false; } } return true; }
public function testRestore() { $item1 = new CartItem(array('name' => 'foo')); $item2 = new CartItem(array('name' => 'bar')); $storeGetReturn = array('id' => 'foo', 'items' => array($item1->toArray(), $item2->toArray())); $store = m::mock('Cart\\Storage\\Store'); $store->shouldReceive('get')->times(1)->andReturn(serialize($storeGetReturn)); $cart = new Cart('foo', $store); $cart->restore(); $this->assertSame('foo', $cart->getId()); $this->assertTrue($cart->totalUniqueItems() == 2); $this->assertTrue($cart->has($item1->id)); $this->assertTrue($cart->has($item2->id)); }
public function testGetSingleTax() { $item = new CartItem(); $item->quantity = 2; $item->tax = 5.0; $tax = $item->getSingleTax(); $this->assertEquals(5.0, $tax); $this->assertTrue(is_float($tax)); }