From 889259d0311eda618374cd0d60d2f302a7aeb458 Mon Sep 17 00:00:00 2001 From: Marion Muszynski Date: Wed, 16 Aug 2017 11:04:23 +0200 Subject: [PATCH] fix async bug for saving gift message of cart --- override/classes/OrderHistory.php | 2 +- override/controllers/CartController.php | 21 +++++---------- themes/site/js/cart-summary.js | 34 +++++++++++++++---------- themes/site_mobile/js/cart-summary.js | 18 ++++++++----- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/override/classes/OrderHistory.php b/override/classes/OrderHistory.php index 572059c0..cfc57e7f 100755 --- a/override/classes/OrderHistory.php +++ b/override/classes/OrderHistory.php @@ -37,7 +37,7 @@ class OrderHistory extends OrderHistoryCore { if (isset($result['template']) AND Validate::isEmail($result['email'])) { - if($lastOrderState->id == 17 && $this->id_order_state == 4) { + if($lastOrderState && $lastOrderState->id == 17 && $this->id_order_state == 4) { $result['template'] .= '_complete'; } diff --git a/override/controllers/CartController.php b/override/controllers/CartController.php index 3347df60..497b5cb8 100644 --- a/override/controllers/CartController.php +++ b/override/controllers/CartController.php @@ -42,7 +42,7 @@ class CartController extends CartControllerCore $add = Tools::getIsset('add') ? 1 : 0; $delete = Tools::getIsset('delete') ? 1 : 0; $removeAll = Tools::getIsset('removeall') ? 1 : 0; - $giftMessage = Tools::getValue('giftmessage'); + $giftMessage = Tools::getIsset('giftmessage') ? 1 : 0; if (Configuration::get('PS_TOKEN_ENABLE') == 1 && strcasecmp(Tools::getToken(false), strval(Tools::getValue('token'))) @@ -61,12 +61,6 @@ class CartController extends CartControllerCore if ($add){ $current_sale = array(); foreach(self::$cart->getProducts() as $product) { - /*$current_sale = Db::getInstance()->getRow(' - SELECT ps.`delivery_delay`, ps.`id_sale` - FROM `'._DB_PREFIX_.'product_ps_cache` psc - LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_sale` = psc.`id_sale`) - WHERE psc.`id_product` = '.(int) $product['id_product'] - );*/ $sale = Sale::getSaleFromCategory((int)$product['id_category_default'], false); $current_sale = array( @@ -77,12 +71,6 @@ class CartController extends CartControllerCore // Only one delivery id per cart, so we can break now break; } - /*$adding_sale = Db::getInstance()->getRow(' - SELECT ps.`delivery_delay`, ps.`id_sale` - FROM `'._DB_PREFIX_.'product_ps_cache` psc - LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_sale` = psc.`id_sale`) - WHERE psc.`id_product` = '.(int) $idProduct - );*/ $current_product = new Product((int) $idProduct); $sale2 = Sale::getSaleFromCategory($current_product->id_category_default, false); $adding_sale = array( @@ -91,8 +79,6 @@ class CartController extends CartControllerCore 'shipping' => Sale::getShippingSale((int)$sale2->id) ); - //die('{"hasError" : true,"popup_error_cart": true, "errors" : [" '.implode(' , ',$current_sale).' - '.implode(' , ',$adding_sale).'"]}'); - if (!empty($current_sale)) { global $smarty; // Only classic/Noel delivery sales can be added together or product of the same sale or philea sales with same delivery delay @@ -301,12 +287,15 @@ class CartController extends CartControllerCore } } } + // @End adding Antadis + // @Adding Antadis - gift message if($giftMessage){ if(Tools::getValue('action') == 'remove_all'){ self::$cart->gift = 0; self::$cart->gift_message = ''; } elseif(Tools::getValue('action') == 'remove_message'){ + self::$cart->gift = 1; self::$cart->gift_message = ''; } elseif(Tools::getValue('action') == 'add_message'){ self::$cart->gift = 1; @@ -319,6 +308,8 @@ class CartController extends CartControllerCore if(self::$cart->update()){ die('{"hasError" : false, "action" : "'.Tools::getValue('action').'"}'); + } else { + die('{"hasError" : true, "errors" : ["'.Tools::displayError('Invalid gift message.', false).'"]}'); } } // @End adding Antadis diff --git a/themes/site/js/cart-summary.js b/themes/site/js/cart-summary.js index b90c95a2..b040018a 100755 --- a/themes/site/js/cart-summary.js +++ b/themes/site/js/cart-summary.js @@ -34,8 +34,8 @@ $(document).ready(function() $('.cart_quantity_delete' ).unbind('click').click(function(){ deletProductFromSummary($(this).attr('id')); return false; }); $('.cart_quantity_input').typeWatch({ highlight: true, wait: 600, captureLength: 0, callback: updateQty }); - $('#go_to_next_step').unbind('click').click(function(){giftMessage(true,true,false);}); - $('.remove_message_gift').unbind('click').click(function(){giftMessage(false,false,true); return false; }); + $('.cart_navigation a#go_to_next_step').click(function(){ return giftMessage(true,true,false); }); + $('.remove_message_gift').unbind('click').click(function(){ giftMessage(false,false,true); return false; }); } }); @@ -477,8 +477,10 @@ function updateCartSummary(json) } function giftMessage(add, next, all){ + var async = true; + var got_next = false; var action = "add_message"; - if(add == false ){ + if(add == false){ if(all == true || !$('input#gift').is(':checked')) { action = "remove_all"; } else { @@ -490,30 +492,33 @@ function giftMessage(add, next, all){ action = "remove_all"; } else { action = "add_message"; + async = false; // jquery 1.4.4, we have to wait for the response before going to next step } } + $.ajax({ type: 'POST', - url: baseDir + 'cart.php', - async: true, + url: baseDir + 'cart.php?giftmessage=1', + async: async, cache: false, dataType: 'json', data: { 'ajax':true, - 'giftmessage' : 1, 'action': action, 'message': $('#gift_message').val(), 'token': static_token }, - success: function(jsonData) - { + success: function(jsonData) + { if (jsonData.hasError) { var errors = ''; - for(error in jsonData.errors) + for(error in jsonData.errors){ //IE6 bug fix - if(error != 'indexOf') + if(error != 'indexOf'){ errors += jsonData.errors[error] + "\n"; + } + } alert(errors); } else @@ -537,12 +542,15 @@ function giftMessage(add, next, all){ $('#gift_message').val(''); } if(next==true){ - return true; + got_next = true; } } }, - error: function(XMLHttpRequest, textStatus, errorThrown) {} - }); + error: function(XMLHttpRequest, textStatus, errorThrown) { + console.log("ERROR\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus + "\n" + 'errorThrown: ' + errorThrown); + } + }); + return got_next; } function updateCustomizedDatas(json) diff --git a/themes/site_mobile/js/cart-summary.js b/themes/site_mobile/js/cart-summary.js index 9899dc28..2155a333 100755 --- a/themes/site_mobile/js/cart-summary.js +++ b/themes/site_mobile/js/cart-summary.js @@ -34,7 +34,7 @@ $(document).ready(function() $('.cart_quantity_delete' ).unbind('click').click(function(){ deletProductFromSummary($(this).attr('id')); return false; }); $('.cart_quantity_input').typeWatch({ highlight: true, wait: 600, captureLength: 0, callback: updateQty }); - $('#go_to_next_step').unbind('click').click(function(){giftMessage(true,true,false); }); + $('.cart_navigation a#go_to_next_step').click(function(){ return giftMessage(true,true,false); }); } }); @@ -465,8 +465,10 @@ function updateCartSummary(json) } function giftMessage(add, next, all){ + var async = true; + var got_next = false; var action = "add_message"; - if(add == false ){ + if(add == false){ if(all == true || !$('input#gift').is(':checked')) { action = "remove_all"; } else { @@ -478,17 +480,18 @@ function giftMessage(add, next, all){ action = "remove_all"; } else { action = "add_message"; + async = false; // jquery 1.4.4, we have to wait for the response before going to next step } } + $.ajax({ type: 'POST', - url: baseDir + 'cart.php', - async: true, + url: baseDir + 'cart.php?giftmessage=1', + async: async, cache: false, dataType: 'json', data: { 'ajax':true, - 'giftmessage' : 1, 'action': action, 'message': $('#gift_message').val(), 'token': static_token @@ -514,14 +517,15 @@ function giftMessage(add, next, all){ $('#gift').removeAttr('checked'); } if(next==true){ - return true; + got_next = true; } } }, error: function(XMLHttpRequest, textStatus, errorThrown) { //alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus); } - }); + }); + return got_next; } function updateCustomizedDatas(json)