From 3568be9aa0a731f7d2bad0ea645110fe82871d30 Mon Sep 17 00:00:00 2001 From: dMetzger Date: Thu, 23 Feb 2012 18:24:23 +0000 Subject: [PATCH] [-] FO : automatic refresh of the cart when a cart rule is added --- classes/CartRule.php | 2 +- controllers/front/CartController.php | 20 ++++++++++++++++++-- themes/default/js/cart-summary.js | 10 ++++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index e2af66f8d..0e3665d7a 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -855,7 +855,7 @@ class CartRuleCore extends ObjectModel public static function autoRemoveFromCart($context = null) { if (!CartRule::isFeatureActive()) - return; + return array(); $errors = array(); if (!$context) diff --git a/controllers/front/CartController.php b/controllers/front/CartController.php index 251e19742..44b4ea2e6 100644 --- a/controllers/front/CartController.php +++ b/controllers/front/CartController.php @@ -34,6 +34,8 @@ class CartControllerCore extends FrontController protected $id_address_delivery; protected $customization_id; protected $qty; + + protected $ajax_refresh = false; /** * This is not a public page, so the canonical redirection is disabled @@ -111,7 +113,9 @@ class CartControllerCore extends FrontController $this->context->cart->update(); } } - CartRule::autoRemoveFromCart(); + $removed = CartRule::autoRemoveFromCart(); + if (count($removed)) + $this->ajax_refresh = true; } protected function processChangeProductAddressDelivery() @@ -215,6 +219,7 @@ class CartControllerCore extends FrontController if (!$this->errors) { + $cart_rules = $this->context->cart->getCartRules(); $updateQuantity = $this->context->cart->updateQty($this->qty, $this->id_product, $this->id_product_attribute, $this->customization_id, $this->id_address_delivery, Tools::getValue('op', 'up')); if ($updateQuantity < 0) { @@ -224,9 +229,18 @@ class CartControllerCore extends FrontController } else if (!$updateQuantity) $this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.', false); + else + { + $cart_rules2 = $this->context->cart->getCartRules(); + if (count($cart_rules2) != count($cart_rules)) + $this->ajax_refresh = true; + } } } - CartRule::autoRemoveFromCart(); + + $removed = CartRule::autoRemoveFromCart(); + if (count($removed)) + $this->ajax_refresh = true; } /** @@ -255,6 +269,8 @@ class CartControllerCore extends FrontController { if ($this->errors) die(Tools::jsonEncode(array('hasError' => true, 'errors' => $this->errors))); + if ($this->ajax_refresh) + die(Tools::jsonEncode(array('refresh' => true))); if (Tools::getIsset('summary')) { diff --git a/themes/default/js/cart-summary.js b/themes/default/js/cart-summary.js index 3c2feca3b..694bc97d8 100644 --- a/themes/default/js/cart-summary.js +++ b/themes/default/js/cart-summary.js @@ -258,11 +258,13 @@ function deleteProductFromSummary(id) var errors = ''; for(error in jsonData.errors) //IE6 bug fix - if(error != 'indexOf') + if (error != 'indexOf') errors += jsonData.errors[error] + "\n"; } else { + if (jsonData.refresh) + location.reload(); if (parseInt(jsonData.summary.products.length) == 0) { $('#center_column').children().each(function() { @@ -308,7 +310,7 @@ function deleteProductFromSummary(id) function upQuantity(id, qty) { - if(typeof(qty)=='undefined' || !qty) + if (typeof(qty) == 'undefined' || !qty) qty = 1; var customizationId = 0; var productId = 0; @@ -344,6 +346,8 @@ function upQuantity(id, qty) } else { + if (jsonData.refresh) + location.reload(); updateCustomizedDatas(jsonData.customizedDatas); updateCartSummary(jsonData.summary); updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART); @@ -403,6 +407,8 @@ function downQuantity(id, qty) } else { + if (jsonData.refresh) + location.reload(); updateCustomizedDatas(jsonData.customizedDatas); updateCartSummary(jsonData.summary); updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);