Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bossanova808 committed Apr 27, 2016
1 parent 5a9d080 commit 7fd8b16
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ This plugin has been tested with Craft 2.5 and Craft Commerce 1.0.1187.

## Changelog

0.1.0
* [Improved] Improved error handling.

0.0.9
* [Added] releases.json

0.0.8
* [Fixed] Handle submissions with all zero quantities more gracefully

Expand Down
2 changes: 1 addition & 1 deletion multiadd/MultiAddPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getName()

public function getVersion()
{
return '0.0.9';
return '0.1.0';
}

public function getSchemaVersion()
Expand Down
70 changes: 34 additions & 36 deletions multiadd/controllers/MultiAddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,53 +46,51 @@ public function actionMultiAdd()
echo '</pre>';
}


if (!isset($items)) {
$errors[] = "No items?";
craft()->urlManager->setRouteVariables(['error' => 'No items?']);
$errors[] = "No items to add.";
}

//prevent submission of all 0 qtys
$itemsToProcess = false;
foreach ($items as $key => $item) {
$qty = isset($item['qty']) ? (int)$item['qty'] : 0;
if ($qty >0){
$itemsToProcess = true;
break;
else{
$itemsToProcess = false;
//prevent submission of all 0 qtys
foreach ($items as $key => $item) {
$qty = isset($item['qty']) ? (int)$item['qty'] : 0;
if ($qty >0){
$itemsToProcess = true;
break;
}
}
if(!$itemsToProcess){
$errors[] = "All items have 0 quantity.";
}
}

if(!$itemsToProcess){
$errors[] = "All items have 0 quantity?";
craft()->urlManager->setRouteVariables(['error' => 'No items?']);
}
else {


// Do some cart-adding using our new, faster, rollback-able service
if (!$errors) {
$error = "";
if (!craft()->multiAdd_cart->multiAddToCart($cart, $items, $error)) {
$errors[] = $error;
}
}
// Do some cart-adding using our new, faster, rollback-able service
if (!$errors) {
$error = "";
if (!craft()->multiAdd_cart->multiAddToCart($cart, $items, $error)) {
$errors[] = $error;
}
}

//trouble?
if ($errors) {
foreach ($errors as $error) {
$this->logError($error);
}
craft()->urlManager->setRouteVariables(['error' => $errors]);
//trouble?
if ($errors) {
foreach ($errors as $error) {
$this->logError($error);
}
//everything went fine!
else {
craft()->userSession->setFlash('notice', 'Products have been multiadd-ed');
//only redirect if we're not debugging and we haven't submitted by ajax
if (!$debug and !$ajax){
$this->redirectToPostedUrl();
}
craft()->urlManager->setRouteVariables(['error' => $errors]);
}
//everything went fine!
else {
craft()->userSession->setFlash('notice', 'Products have been multiadd-ed');
//only redirect if we're not debugging and we haven't submitted by ajax
if (!$debug and !$ajax){
$this->redirectToPostedUrl();
}
}


// Appropriate Ajax responses...
if($ajax){
if($errors){
Expand Down
8 changes: 8 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{
"version": "0.1.0",
"downloadUrl": "https://github.com/engram-design/MultiAdd/archive/0.1.0.zip",
"date": "2016-04-27T11:17:28+10:00",
"notes": [
"[Improved] Error Handling",
]
},
{
"version": "0.0.9",
"downloadUrl": "https://github.com/engram-design/MultiAdd/archive/0.0.9.zip",
Expand Down

0 comments on commit 7fd8b16

Please sign in to comment.