Skip to content

Commit

Permalink
PATCH: better handling of exceptions in shopping cart
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnysideup committed Apr 9, 2018
1 parent e3aa41b commit 89474e1
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions code/control/ShoppingCart_Controller.php
Expand Up @@ -108,15 +108,7 @@ public function index()
return;
}
user_error(_t('Order.NOCARTINITIALISED', 'no cart initialised'), E_USER_NOTICE);
$errorPage404 = DataObject::get_one(
'ErrorPage',
array('ErrorCode' => '404')
);
if ($errorPage404) {
$this->redirect($errorPage404->Link());

return;
}
return $this->goToErrorPage();
user_error(_t('Order.NOCARTINITIALISED', 'no 404 page available'), E_USER_ERROR);
}

Expand Down Expand Up @@ -341,9 +333,14 @@ public function json(SS_HTTPRequest $request)
*/
public function additem(SS_HTTPRequest $request)
{
$this->cart->addBuyable($this->buyable(), $this->quantity(), $this->parameters());
$buyable = $this->buyable();
if($buyable) {
$this->cart->addBuyable($buyable, $this->quantity(), $this->parameters());
return $this->cart->setMessageAndReturn();
} else {
return $this->goToErrorPage();
}

return $this->cart->setMessageAndReturn();
}

/**
Expand All @@ -357,9 +354,14 @@ public function additem(SS_HTTPRequest $request)
*/
public function setquantityitem(SS_HTTPRequest $request)
{
$this->cart->setQuantity($this->buyable(), $this->quantity(), $this->parameters());
$buyable = $this->buyable();
if($buyable) {
$this->cart->setQuantity($buyable, $this->quantity(), $this->parameters());

return $this->cart->setMessageAndReturn();
return $this->cart->setMessageAndReturn();
} else {
return $this->goToErrorPage();
}
}

/**
Expand All @@ -372,9 +374,14 @@ public function setquantityitem(SS_HTTPRequest $request)
*/
public function removeitem(SS_HTTPRequest $request)
{
$this->cart->decrementBuyable($this->buyable(), $this->quantity(), $this->parameters());
$buyable = $this->buyable();
if($buyable) {
$this->cart->decrementBuyable($buyable, $this->quantity(), $this->parameters());

return $this->cart->setMessageAndReturn();
return $this->cart->setMessageAndReturn();
} else {
return $this->goToErrorPage()
}
}

/**
Expand All @@ -387,12 +394,17 @@ public function removeitem(SS_HTTPRequest $request)
*/
public function removeallitem(SS_HTTPRequest $request)
{
$this->cart->deleteBuyable($this->buyable(), $this->parameters());
//added this because cart was not updating correctly
$order = $this->cart->CurrentOrder();
$order->calculateOrderAttributes($force = true);
$buyable = $this->buyable();
if($buyable)) {
$this->cart->deleteBuyable($buyable, $this->parameters());
//added this because cart was not updating correctly
$order = $this->cart->CurrentOrder();
$order->calculateOrderAttributes($force = true);

return $this->cart->setMessageAndReturn();
return $this->cart->setMessageAndReturn();
} else {
return $this->goToErrorPage();
}
}

/**
Expand Down Expand Up @@ -849,6 +861,18 @@ protected function parameters($getpost = 'GET')
return ($getpost == 'GET') ? $this->getRequest()->getVars() : $_POST;
}

protected function goToErrorPage()
{
$errorPage404 = DataObject::get_one(
'ErrorPage',
array('ErrorCode' => '404')
);
if ($errorPage404) {
return $this->redirect($errorPage404->Link());
}
return $this->redirect('page-not-found');
}

/**
* Handy debugging action visit.
* Log in as an administrator and visit mysite/shoppingcart/debug.
Expand Down

0 comments on commit 89474e1

Please sign in to comment.