Skip to content

Commit

Permalink
Added way to customize session component
Browse files Browse the repository at this point in the history
  • Loading branch information
omnilight committed Dec 9, 2014
1 parent 1952689 commit e34d7f4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions ShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Yii;
use yii\base\Component;
use yii\base\Event;
use yii\di\Instance;
use yii\web\Session;


/**
Expand Down Expand Up @@ -37,6 +39,11 @@ class ShoppingCart extends Component
* @var bool
*/
public $storeInSession = true;
/**
* Session component
* @var string|Session
*/
public $session = 'session';
/**
* Shopping cart ID to support multiple carts
* @var string
Expand All @@ -58,16 +65,18 @@ public function init()
*/
public function loadFromSession()
{
if (isset(Yii::$app->session[$this->cartId]))
$this->setSerialized(Yii::$app->session[$this->cartId]);
$this->session = Instance::ensure($this->session, Session::className());
if (isset($this->session[$this->cartId]))
$this->setSerialized($this->session[$this->cartId]);
}

/**
* Saves cart to the session
*/
public function saveToSession()
{
Yii::$app->session[$this->cartId] = $this->getSerialized();
$this->session = Instance::ensure($this->session, Session::className());
$this->session[$this->cartId] = $this->getSerialized();
}

/**
Expand Down

0 comments on commit e34d7f4

Please sign in to comment.