Skip to content

Commit

Permalink
persist conditions to session as well
Browse files Browse the repository at this point in the history
  • Loading branch information
darryldecode committed Feb 25, 2015
1 parent e455bf0 commit be0b056
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,21 @@ Cart::condition($condition2);
Cart::condition([$condition1, $condition2]);

// To get all applied conditions on a cart, use below:
$carConditions = Cart::getConditions();
$cartConditions = Cart::getConditions();
foreach($carConditions as $condition)
{
$condition->getTarget(); // the target of which the condition was applied
$condition->getName(); // the name of the condition
$condition->getType(); // the type
$condition->getValue(); // the value of the condition
}

// You can also get a condition that has been applied on the cart by using its name, use below:
$condition = Cart::getCondition('VAT 12.5%');
$condition->getTarget(); // the target of which the condition was applied
$condition->getName(); // the name of the condition
$condition->getType(); // the type
$condition->getValue(); // the value of the condition
```

NOTE: All cart based conditions should be applied before calling **Cart::getTotal()**
Expand Down
49 changes: 40 additions & 9 deletions src/Darryldecode/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ class Cart {
protected $instanceName;

/**
* the session key use as storage
* the session key use to persist cart items
*
* @var
*/
protected $sessionKey;
protected $sessionKeyCartItems;

/**
* the session key use to persist cart conditions
*
* @var
*/
protected $sessionKeyCartConditions;

/**
* our object constructor
Expand All @@ -57,7 +64,8 @@ public function __construct($session, $events, $instanceName, $session_key)
$this->events = $events;
$this->session = $session;
$this->instanceName = $instanceName;
$this->sessionKey = $session_key;
$this->sessionKeyCartItems = $session_key.'_cart_items';
$this->sessionKeyCartConditions = $session_key.'_cart_conditions';
$this->conditions = new CartConditionCollection();
$this->events->fire($this->getInstanceName().'.created', array($this));
}
Expand Down Expand Up @@ -219,7 +227,7 @@ public function clear()
$this->events->fire($this->getInstanceName().'.clearing', array($this));

$this->session->put(
$this->sessionKey,
$this->sessionKeyCartItems,
array()
);

Expand Down Expand Up @@ -247,7 +255,9 @@ public function condition($condition)

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

$this->conditions->push($condition);
$this->conditions->put($condition->getName(), $condition);

$this->saveConditions($this->conditions);

return $this;
}
Expand All @@ -259,7 +269,18 @@ public function condition($condition)
*/
public function getConditions()
{
return $this->conditions;
return new CartConditionCollection($this->session->get($this->sessionKeyCartConditions));
}

/**
* get condition by its name
*
* @param $conditionName
* @return CartCondition
*/
public function getCondition($conditionName)
{
return $this->getConditions()->get($conditionName);
}

/**
Expand Down Expand Up @@ -354,7 +375,7 @@ public function getTotal()
*/
public function getContent()
{
return (new CartCollection($this->session->get($this->sessionKey)));
return (new CartCollection($this->session->get($this->sessionKeyCartItems)));
}

/**
Expand All @@ -364,7 +385,7 @@ public function getContent()
*/
public function isEmpty()
{
$cart = new CartCollection($this->session->get($this->sessionKey));
$cart = new CartCollection($this->session->get($this->sessionKeyCartItems));

return $cart->isEmpty();
}
Expand Down Expand Up @@ -417,7 +438,17 @@ protected function addRow($id, $item)
*/
protected function save($cart)
{
$this->session->put($this->sessionKey, $cart);
$this->session->put($this->sessionKeyCartItems, $cart);
}

/**
* save the cart conditions
*
* @param $conditions
*/
protected function saveConditions($conditions)
{
$this->session->put($this->sessionKeyCartConditions, $conditions);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/CartConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,42 @@ public function test_add_item_with_multiple_item_conditions_with_one_condition_w
$this->assertEquals(85.00, $this->cart->getSubTotal(), 'Cart subtotal with 1 item should be 70');
}

public function test_get_condition_by_condition_name()
{
$itemCondition1 = new CartCondition(array(
'name' => 'SALE 5%',
'type' => 'sale',
'target' => 'subtotal',
'value' => '-5%',
));
$itemCondition2 = new CartCondition(array(
'name' => 'Item Gift Pack 25.00',
'type' => 'promo',
'target' => 'subtotal',
'value' => '-25',
));

$item = array(
'id' => 456,
'name' => 'Sample Item 1',
'price' => 100,
'quantity' => 1,
'attributes' => array(),
);

$this->cart->add($item);

$this->cart->condition([$itemCondition1, $itemCondition2]);

// get a condition applied on cart by condition name
$condition = $this->cart->getCondition($itemCondition1->getName());

$this->assertEquals($condition->getName(), 'SALE 5%');
$this->assertEquals($condition->getTarget(), 'subtotal');
$this->assertEquals($condition->getType(), 'sale');
$this->assertEquals($condition->getValue(), '-5%');
}

protected function fillCart()
{
$items = array(
Expand Down

0 comments on commit be0b056

Please sign in to comment.