Skip to content

Commit

Permalink
AddToCartAction updated to be able to handle both errored and ok posi…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
SilverFire committed Mar 21, 2016
1 parent fe44f81 commit d9b14cf
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/actions/AddToCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,25 @@ public function run()
$collection->load();
}

if ($collection->validate()) {
foreach ($collection->models as $position) {
/** @var CartPositionInterface $position */
if (!$cart->hasPosition($position->getId())) {
$cart->put($position);
Yii::$app->session->addFlash('success', Yii::t('cart', 'Item has been added to cart'));
} else {
Yii::$app->session->addFlash('warning', Yii::t('cart', 'Item is already in the cart'));
foreach ($collection->models as $position) {
/** @var CartPositionInterface $position */
if (!$position->validate()) {
$error = $collection->getFirstError();
if (empty($error)) {
$error = Yii::t('cart', 'Failed to add item to the cart');
}
Yii::$app->session->addFlash('warning', $error);
Yii::warning('Failed to add item to cart', 'cart');

continue;
}

if (!$cart->hasPosition($position->getId())) {
$cart->put($position);
Yii::$app->session->addFlash('success', Yii::t('cart', 'Item has been added to cart'));
} else {
Yii::$app->session->addFlash('warning', Yii::t('cart', 'Item is already in the cart'));
}
} else {
Yii::$app->session->addFlash('warning', Yii::t('cart', 'Failed to add item to the cart'));
Yii::warning('Failed to add item to the cart', 'cart');
}

if ($request->isAjax) {
Expand Down

0 comments on commit d9b14cf

Please sign in to comment.