Skip to content

Commit be3d255

Browse files
committed
added clearing cart conditions and other useful methods in dealing conditions
1 parent cb4cad1 commit be3d255

File tree

4 files changed

+206
-9
lines changed

4 files changed

+206
-9
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ Then Finally you can call **Cart::getSubTotal()** to get the Cart sub total with
356356
```php
357357
$cartSubTotal = Cart::getSubTotal(); // the subtotal will be calculated based on the conditions you have provided
358358
```
359-
360359
## Instances
361360

362361
You may also want multiple cart instances on the same page without conflicts.

src/Darryldecode/Cart/Cart.php

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php namespace Darryldecode\Cart;
22

3-
use Darryldecode\Cart\Exceptions\CartInstanceException;
43
use Darryldecode\Cart\Exceptions\InvalidConditionException;
54
use Darryldecode\Cart\Exceptions\InvalidItemException;
6-
use Darryldecode\Cart\Exceptions\InvalidItemFieldException;
75
use Darryldecode\Cart\Helpers\Helpers;
86
use Darryldecode\Cart\Validators\CartItemValidator;
97

8+
/**
9+
* Class Cart
10+
* @package Darryldecode\Cart
11+
*/
1012
class Cart {
1113

1214
/**
@@ -267,7 +269,7 @@ public function getConditions()
267269
}
268270

269271
/**
270-
* get condition by its name
272+
* get condition applied on the cart by its name
271273
*
272274
* @param $conditionName
273275
* @return CartCondition
@@ -277,6 +279,82 @@ public function getCondition($conditionName)
277279
return $this->getConditions()->get($conditionName);
278280
}
279281

282+
/**
283+
* removes a condition on a cart by condition name,
284+
* this can only remove conditions that are added on cart bases not conditions that are added on an item/product.
285+
* If you wish to remove a condition that has been added for a specific item/product, you may
286+
* use the removeItemCondition(itemId, conditionName) method instead.
287+
*
288+
* @param $conditionName
289+
* @return void
290+
*/
291+
public function removeCartCondition($conditionName)
292+
{
293+
$conditions = $this->getConditions();
294+
295+
$conditions->pull($conditionName);
296+
297+
$this->saveConditions($conditions);
298+
}
299+
300+
/**
301+
* remove a condition that has been applied on an item that is already on the cart
302+
*
303+
* @param $itemId
304+
* @param $conditionName
305+
* @return bool
306+
*/
307+
public function removeItemCondition($itemId, $conditionName)
308+
{
309+
if( ! $item = $this->getContent()->get($itemId) )
310+
{
311+
return false;
312+
}
313+
314+
if( $this->itemHasConditions($item) )
315+
{
316+
// NOTE:
317+
// we do it this way, we get first conditions and store
318+
// it in a temp variable $originalConditions, then we will modify the array there
319+
// and after modification we will store it again on $item['conditions']
320+
// This is because of ArrayAccess implementation
321+
// see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
322+
323+
$tempConditionsHolder = $item['conditions'];
324+
325+
foreach($tempConditionsHolder as $k => $condition)
326+
{
327+
if( $condition->getName() == $conditionName )
328+
{
329+
unset($tempConditionsHolder[$k]);
330+
}
331+
}
332+
333+
$item['conditions'] = $tempConditionsHolder;
334+
}
335+
336+
$this->update($itemId, array(
337+
'conditions' => $item['conditions']
338+
));
339+
340+
return true;
341+
}
342+
343+
/**
344+
* clears all conditions on a cart,
345+
* this does not remove conditions that has been added specifically to an item/product.
346+
* If you wish to remove a specific condition to a product, you may use the method: removeItemCondition($itemId, $conditionName)
347+
*
348+
* @return void
349+
*/
350+
public function clearCartConditions()
351+
{
352+
$this->session->put(
353+
$this->sessionKeyCartConditions,
354+
array()
355+
);
356+
}
357+
280358
/**
281359
* get cart sub total
282360
*

tests/CartConditionsTest.php

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,18 @@ 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_get_condition_by_condition_name()
330+
public function test_get_cart_condition_by_condition_name()
331331
{
332332
$itemCondition1 = new CartCondition(array(
333333
'name' => 'SALE 5%',
334334
'type' => 'sale',
335-
'target' => 'subtotal',
335+
'target' => 'total',
336336
'value' => '-5%',
337337
));
338338
$itemCondition2 = new CartCondition(array(
339339
'name' => 'Item Gift Pack 25.00',
340340
'type' => 'promo',
341-
'target' => 'subtotal',
341+
'target' => 'total',
342342
'value' => '-25',
343343
));
344344

@@ -358,11 +358,126 @@ public function test_get_condition_by_condition_name()
358358
$condition = $this->cart->getCondition($itemCondition1->getName());
359359

360360
$this->assertEquals($condition->getName(), 'SALE 5%');
361-
$this->assertEquals($condition->getTarget(), 'subtotal');
361+
$this->assertEquals($condition->getTarget(), 'total');
362362
$this->assertEquals($condition->getType(), 'sale');
363363
$this->assertEquals($condition->getValue(), '-5%');
364364
}
365365

366+
public function test_remove_cart_condition_by_condition_name()
367+
{
368+
$itemCondition1 = new CartCondition(array(
369+
'name' => 'SALE 5%',
370+
'type' => 'sale',
371+
'target' => 'total',
372+
'value' => '-5%',
373+
));
374+
$itemCondition2 = new CartCondition(array(
375+
'name' => 'Item Gift Pack 25.00',
376+
'type' => 'promo',
377+
'target' => 'total',
378+
'value' => '-25',
379+
));
380+
381+
$item = array(
382+
'id' => 456,
383+
'name' => 'Sample Item 1',
384+
'price' => 100,
385+
'quantity' => 1,
386+
'attributes' => array(),
387+
);
388+
389+
$this->cart->add($item);
390+
391+
$this->cart->condition([$itemCondition1, $itemCondition2]);
392+
393+
// let's prove first we have now two conditions in the cart
394+
$this->assertEquals(2, $this->cart->getConditions()->count(), 'Cart should have two conditions');
395+
396+
// now let's remove a specific condition by condition name
397+
$this->cart->removeCartCondition('SALE 5%');
398+
399+
// cart should have now only 1 condition
400+
$this->assertEquals(1, $this->cart->getConditions()->count(), 'Cart should have one condition');
401+
$this->assertEquals('Item Gift Pack 25.00', $this->cart->getConditions()->first()->getName());
402+
}
403+
404+
public function test_remove_item_condition_by_condition_name()
405+
{
406+
$itemCondition1 = new CartCondition(array(
407+
'name' => 'SALE 5%',
408+
'type' => 'sale',
409+
'target' => 'subtotal',
410+
'value' => '-5%',
411+
));
412+
$itemCondition2 = new CartCondition(array(
413+
'name' => 'Item Gift Pack 25.00',
414+
'type' => 'promo',
415+
'target' => 'subtotal',
416+
'value' => '-25',
417+
));
418+
419+
$item = array(
420+
'id' => 456,
421+
'name' => 'Sample Item 1',
422+
'price' => 100,
423+
'quantity' => 1,
424+
'attributes' => array(),
425+
'conditions' => [$itemCondition1, $itemCondition2]
426+
);
427+
428+
$this->cart->add($item);
429+
430+
// let's very first the item has 2 conditions in it
431+
$this->assertCount(2,$this->cart->get(456)['conditions'], 'Item should have two conditions');
432+
433+
// now let's remove a condition on that item using the condition name
434+
$this->cart->removeItemCondition(456, 'SALE 5%');
435+
436+
// now we should have only 1 condition left on that item
437+
$this->assertCount(1,$this->cart->get(456)['conditions'], 'Item should have one condition left');
438+
}
439+
440+
public function test_clear_cart_conditions()
441+
{
442+
// NOTE:
443+
// This only clears all conditions that has been added in a cart bases
444+
// this does not remove conditions on per item bases
445+
446+
$itemCondition1 = new CartCondition(array(
447+
'name' => 'SALE 5%',
448+
'type' => 'sale',
449+
'target' => 'total',
450+
'value' => '-5%',
451+
));
452+
$itemCondition2 = new CartCondition(array(
453+
'name' => 'Item Gift Pack 25.00',
454+
'type' => 'promo',
455+
'target' => 'total',
456+
'value' => '-25',
457+
));
458+
459+
$item = array(
460+
'id' => 456,
461+
'name' => 'Sample Item 1',
462+
'price' => 100,
463+
'quantity' => 1,
464+
'attributes' => array(),
465+
);
466+
467+
$this->cart->add($item);
468+
469+
$this->cart->condition([$itemCondition1, $itemCondition2]);
470+
471+
// let's prove first we have now two conditions in the cart
472+
$this->assertEquals(2, $this->cart->getConditions()->count(), 'Cart should have two conditions');
473+
474+
// now let's clear cart conditions
475+
$this->cart->clearCartConditions();
476+
477+
// cart should have now only 1 condition
478+
$this->assertEquals(0, $this->cart->getConditions()->count(), 'Cart should have no conditions now');
479+
}
480+
366481
protected function fillCart()
367482
{
368483
$items = array(

tests/CartTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ public function test_clearing_cart()
325325

326326
$this->cart->add($items);
327327

328-
$this->assertFalse($this->cart->isEmpty());
328+
$this->assertFalse($this->cart->isEmpty(),'prove first cart is not empty');
329+
330+
// now let's clear cart
331+
$this->cart->clear();
332+
333+
$this->assertTrue($this->cart->isEmpty(),'cart should now be empty');
329334
}
330335
}

0 commit comments

Comments
 (0)