Skip to content

Commit 339bae9

Browse files
committed
added 2 new useful CART APIs
1 parent 024ede8 commit 339bae9

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/Darryldecode/Cart/Cart.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,45 @@ public function update($id, $data)
221221
$this->save($cart);
222222
}
223223

224+
/**
225+
* add condition on an existing item on the cart
226+
*
227+
* @param int|string $productId
228+
* @param CartCondition $itemCondition
229+
* @return $this
230+
*/
231+
public function addItemCondition($productId, $itemCondition)
232+
{
233+
if( $product = $this->get($productId) )
234+
{
235+
$conditionInstance = "\\Darryldecode\\Cart\\CartCondition";
236+
237+
if( $itemCondition instanceof $conditionInstance )
238+
{
239+
// we need to copy first to a temporary variable to hold the conditions
240+
// to avoid hitting this error "Indirect modification of overloaded element of Darryldecode\Cart\ItemCollection has no effect"
241+
// this is due to laravel Collection instance that implements Array Access
242+
// // see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
243+
$itemConditionTempHolder = $product['conditions'];
244+
245+
if( is_array($itemConditionTempHolder) )
246+
{
247+
array_push($itemConditionTempHolder, $itemCondition);
248+
}
249+
else
250+
{
251+
$itemConditionTempHolder = $itemCondition;
252+
}
253+
254+
$this->update($productId, array(
255+
'conditions' => $itemConditionTempHolder // the newly updated conditions
256+
));
257+
}
258+
}
259+
260+
return $this;
261+
}
262+
224263
/**
225264
* removes an item on cart by item ID
226265
*
@@ -523,6 +562,25 @@ public function getTotal()
523562
return $newTotal;
524563
}
525564

565+
/**
566+
* get total quantity of items in the cart
567+
*
568+
* @return int
569+
*/
570+
public function getTotalQuantity()
571+
{
572+
$items = $this->getContent();
573+
574+
if( $items->isEmpty() ) return 0;
575+
576+
$count = $items->sum(function($item)
577+
{
578+
return $item['quantity'];
579+
});
580+
581+
return $count;
582+
}
583+
526584
/**
527585
* get the cart
528586
*

tests/CartConditionsTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,41 @@ public function test_add_item_with_multiple_item_conditions_with_one_condition_w
327327
$this->assertEquals(85.00, $this->cart->getSubTotal(), 'Cart subtotal with 1 item should be 70');
328328
}
329329

330+
public function test_add_item_condition()
331+
{
332+
$itemCondition2 = new CartCondition(array(
333+
'name' => 'Item Gift Pack 25.00',
334+
'type' => 'promo',
335+
'target' => 'item',
336+
'value' => '-25',
337+
));
338+
$coupon101 = new CartCondition(array(
339+
'name' => 'COUPON 101',
340+
'type' => 'coupon',
341+
'target' => 'item',
342+
'value' => '-5%',
343+
));
344+
345+
$item = array(
346+
'id' => 456,
347+
'name' => 'Sample Item 1',
348+
'price' => 100,
349+
'quantity' => 1,
350+
'attributes' => array(),
351+
'conditions' => [$itemCondition2]
352+
);
353+
354+
$this->cart->add($item);
355+
356+
// let's prove first we have 1 condition on this item
357+
$this->assertCount(1, $this->cart->get($item['id'])['conditions'], "Item should have 1 condition");
358+
359+
// now let's insert a condition on an existing item on the cart
360+
$this->cart->addItemCondition($item['id'], $coupon101);
361+
362+
$this->assertCount(2, $this->cart->get($item['id'])['conditions'], "Item should have 2 conditions");
363+
}
364+
330365
public function test_get_cart_condition_by_condition_name()
331366
{
332367
$itemCondition1 = new CartCondition(array(

tests/CartTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,31 @@ public function test_clearing_cart()
400400
$this->assertTrue($this->cart->isEmpty(),'cart should now be empty');
401401
}
402402

403+
public function test_cart_get_total_quantity()
404+
{
405+
$items = array(
406+
array(
407+
'id' => 456,
408+
'name' => 'Sample Item 1',
409+
'price' => 67.99,
410+
'quantity' => 3,
411+
'attributes' => array()
412+
),
413+
array(
414+
'id' => 568,
415+
'name' => 'Sample Item 2',
416+
'price' => 69.25,
417+
'quantity' => 1,
418+
'attributes' => array()
419+
),
420+
);
421+
422+
$this->cart->add($items);
423+
424+
$this->assertFalse($this->cart->isEmpty(),'prove first cart is not empty');
425+
426+
// now let's count the cart's quantity
427+
$this->assertInternalType("int", $this->cart->getTotalQuantity(), 'Return type should be INT');
428+
$this->assertEquals(4, $this->cart->getTotalQuantity(),'Cart\'s quantity should be 4.');
429+
}
403430
}

0 commit comments

Comments
 (0)