@@ -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 /**
0 commit comments