Skip to content

Commit

Permalink
Added AddToCartAction::redirectToCart property
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed May 25, 2016
1 parent 29354ed commit 5857422
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/actions/AddToCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class AddToCartAction extends \yii\base\Action
*/
public $bulkLoad = false;

/**
* @var bool whether client should be redirected to the cart in case of success item adding
*/
public $redirectToCart = false;

/**
* @var bool whether any errors occurred during save
*/
protected $hasErrors = false;

/**
* Returns the cart module.
* @return CartModule
Expand Down Expand Up @@ -57,6 +67,7 @@ public function run()
foreach ($collection->models as $position) {
/** @var CartPositionInterface $position */
if (!$position->validate()) {
$this->hasErrors = true;
$error = $collection->getFirstError();
if (empty($error)) {
$error = Yii::t('cart', 'Failed to add item to the cart');
Expand All @@ -75,10 +86,25 @@ public function run()
}
}

}

protected function afterRun()
{
$this->ensureBehaviors();
if ($this->hasEventHandlers('afterAction')) {
return true;
}

$request = Yii::$app->request;

if ($request->isAjax) {
Yii::$app->end();
}

return $this->controller->redirect($request->referrer ?: $this->controller->goHome());
if ($this->redirectToCart && !$this->hasErrors) {
return $this->controller->redirect('@cart');
} else {
return $this->controller->redirect($request->referrer ?: $this->controller->goHome());
}
}
}

0 comments on commit 5857422

Please sign in to comment.