Skip to content

Commit be0b056

Browse files
committed
persist conditions to session as well
1 parent e455bf0 commit be0b056

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,21 @@ Cart::condition($condition2);
263263
Cart::condition([$condition1, $condition2]);
264264

265265
// To get all applied conditions on a cart, use below:
266-
$carConditions = Cart::getConditions();
266+
$cartConditions = Cart::getConditions();
267267
foreach($carConditions as $condition)
268268
{
269269
$condition->getTarget(); // the target of which the condition was applied
270270
$condition->getName(); // the name of the condition
271271
$condition->getType(); // the type
272272
$condition->getValue(); // the value of the condition
273273
}
274+
275+
// You can also get a condition that has been applied on the cart by using its name, use below:
276+
$condition = Cart::getCondition('VAT 12.5%');
277+
$condition->getTarget(); // the target of which the condition was applied
278+
$condition->getName(); // the name of the condition
279+
$condition->getType(); // the type
280+
$condition->getValue(); // the value of the condition
274281
```
275282

276283
NOTE: All cart based conditions should be applied before calling **Cart::getTotal()**

src/Darryldecode/Cart/Cart.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ class Cart {
3838
protected $instanceName;
3939

4040
/**
41-
* the session key use as storage
41+
* the session key use to persist cart items
4242
*
4343
* @var
4444
*/
45-
protected $sessionKey;
45+
protected $sessionKeyCartItems;
46+
47+
/**
48+
* the session key use to persist cart conditions
49+
*
50+
* @var
51+
*/
52+
protected $sessionKeyCartConditions;
4653

4754
/**
4855
* our object constructor
@@ -57,7 +64,8 @@ public function __construct($session, $events, $instanceName, $session_key)
5764
$this->events = $events;
5865
$this->session = $session;
5966
$this->instanceName = $instanceName;
60-
$this->sessionKey = $session_key;
67+
$this->sessionKeyCartItems = $session_key.'_cart_items';
68+
$this->sessionKeyCartConditions = $session_key.'_cart_conditions';
6169
$this->conditions = new CartConditionCollection();
6270
$this->events->fire($this->getInstanceName().'.created', array($this));
6371
}
@@ -219,7 +227,7 @@ public function clear()
219227
$this->events->fire($this->getInstanceName().'.clearing', array($this));
220228

221229
$this->session->put(
222-
$this->sessionKey,
230+
$this->sessionKeyCartItems,
223231
array()
224232
);
225233

@@ -247,7 +255,9 @@ public function condition($condition)
247255

248256
if( ! $condition instanceof CartCondition ) throw new InvalidConditionException('Argument 1 must be an instance of \'Darryldecode\Cart\CartCondition\'');
249257

250-
$this->conditions->push($condition);
258+
$this->conditions->put($condition->getName(), $condition);
259+
260+
$this->saveConditions($this->conditions);
251261

252262
return $this;
253263
}
@@ -259,7 +269,18 @@ public function condition($condition)
259269
*/
260270
public function getConditions()
261271
{
262-
return $this->conditions;
272+
return new CartConditionCollection($this->session->get($this->sessionKeyCartConditions));
273+
}
274+
275+
/**
276+
* get condition by its name
277+
*
278+
* @param $conditionName
279+
* @return CartCondition
280+
*/
281+
public function getCondition($conditionName)
282+
{
283+
return $this->getConditions()->get($conditionName);
263284
}
264285

265286
/**
@@ -354,7 +375,7 @@ public function getTotal()
354375
*/
355376
public function getContent()
356377
{
357-
return (new CartCollection($this->session->get($this->sessionKey)));
378+
return (new CartCollection($this->session->get($this->sessionKeyCartItems)));
358379
}
359380

360381
/**
@@ -364,7 +385,7 @@ public function getContent()
364385
*/
365386
public function isEmpty()
366387
{
367-
$cart = new CartCollection($this->session->get($this->sessionKey));
388+
$cart = new CartCollection($this->session->get($this->sessionKeyCartItems));
368389

369390
return $cart->isEmpty();
370391
}
@@ -417,7 +438,17 @@ protected function addRow($id, $item)
417438
*/
418439
protected function save($cart)
419440
{
420-
$this->session->put($this->sessionKey, $cart);
441+
$this->session->put($this->sessionKeyCartItems, $cart);
442+
}
443+
444+
/**
445+
* save the cart conditions
446+
*
447+
* @param $conditions
448+
*/
449+
protected function saveConditions($conditions)
450+
{
451+
$this->session->put($this->sessionKeyCartConditions, $conditions);
421452
}
422453

423454
/**

tests/CartConditionsTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,42 @@ 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()
331+
{
332+
$itemCondition1 = new CartCondition(array(
333+
'name' => 'SALE 5%',
334+
'type' => 'sale',
335+
'target' => 'subtotal',
336+
'value' => '-5%',
337+
));
338+
$itemCondition2 = new CartCondition(array(
339+
'name' => 'Item Gift Pack 25.00',
340+
'type' => 'promo',
341+
'target' => 'subtotal',
342+
'value' => '-25',
343+
));
344+
345+
$item = array(
346+
'id' => 456,
347+
'name' => 'Sample Item 1',
348+
'price' => 100,
349+
'quantity' => 1,
350+
'attributes' => array(),
351+
);
352+
353+
$this->cart->add($item);
354+
355+
$this->cart->condition([$itemCondition1, $itemCondition2]);
356+
357+
// get a condition applied on cart by condition name
358+
$condition = $this->cart->getCondition($itemCondition1->getName());
359+
360+
$this->assertEquals($condition->getName(), 'SALE 5%');
361+
$this->assertEquals($condition->getTarget(), 'subtotal');
362+
$this->assertEquals($condition->getType(), 'sale');
363+
$this->assertEquals($condition->getValue(), '-5%');
364+
}
365+
330366
protected function fillCart()
331367
{
332368
$items = array(

0 commit comments

Comments
 (0)