From f6d8f403dad45e72e63af537c87b6798dcf2b375 Mon Sep 17 00:00:00 2001 From: aFolletete Date: Wed, 21 Dec 2011 09:23:31 +0000 Subject: [PATCH] // fix issue on ajax querie on adding product // clean code --- classes/Product.php | 38 ++++++++++----------- classes/SpecificPrice.php | 3 ++ controllers/admin/AdminOrdersController.php | 7 ---- js/admin_order.js | 13 ++++++- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/classes/Product.php b/classes/Product.php index 10dda838f..15486c3dd 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -2191,13 +2191,12 @@ class ProductCore extends ObjectModel * @param variable_reference $specificPriceOutput. * If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object * @param boolean $with_ecotax insert ecotax in price output. - * @param bool $flush_cache set to true in order to flush getPriceStatic cache for these parameters * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1, $force_associated_tax = false, $id_customer = null, $id_cart = null, $id_address = null, &$specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, Context $context = null, - $use_customer_price = true, $flush_cache = false) + $use_customer_price = true) { if (!$context) $context = Context::getContext(); @@ -2235,10 +2234,6 @@ class ProductCore extends ObjectModel $condition = ' AND `id_product_attribute` = '.(int)$id_product_attribute; } - // Flush cache - if (isset(self::$_cart_quantity[$cache_name]) && $flush_cache) - unset(self::$_cart_quantity[$cache_name]); - if (!isset(self::$_cart_quantity[$cache_name]) || self::$_cart_quantity[$cache_name] != (int)$quantity) { self::$_cart_quantity[$cache_name] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' @@ -2310,8 +2305,7 @@ class ProductCore extends ObjectModel $specific_price_output, $use_group_reduction, $id_customer, - $use_customer_price, - $flush_cache + $use_customer_price ); } @@ -2333,12 +2327,11 @@ class ProductCore extends ObjectModel * @param boolean $with_ecotax insert ecotax in price output. * @param variable_reference $specific_price_output * If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object - * @param bool $flush_cache set to true in order to flush getPriceStatic cache for these parameters * @return float Product price **/ public static function priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $zipcode, $id_currency, $id_group, $quantity, $use_tax, $decimals, $only_reduc, $use_reduc, $with_ecotax, &$specific_price, $use_group_reduction, - $id_customer = 0, $use_customer_price = true, $flush_cache = false) + $id_customer = 0, $use_customer_price = true) { if (!$use_customer_price) $id_customer = 0; @@ -2364,17 +2357,11 @@ class ProductCore extends ObjectModel $id_customer ); - // Flush cache - if (isset(self::$_prices[$cache_id]) && $flush_cache) - unset(self::$_prices[$cache_id]); - if (isset(self::$_prices[$cache_id])) return self::$_prices[$cache_id]; // fetch price & attribute price $cache_id_2 = $id_product.'-'.$id_product_attribute; - if (isset(self::$_pricesLevel2[$cache_id_2]) && $flush_cache) - unset(self::$_pricesLevel2[$cache_id_2]); if (!isset(self::$_pricesLevel2[$cache_id_2])) { $sql = new DbQuery(); @@ -2407,10 +2394,10 @@ class ProductCore extends ObjectModel } $result = self::$_pricesLevel2[$cache_id_2]; - if (!$specific_price || $specific_price['price'] ==0) - $price = (float)$result['price']; - else - $price = (float)$specific_price['price']; + if (!$specific_price || $specific_price['price'] ==0) + $price = (float)$result['price']; + else + $price = (float)$specific_price['price']; // convert only if the specific price is in the default currency (id_currency = 0) if (!$specific_price || !($specific_price['price'] > 0 && $specific_price['id_currency'])) $price = Tools::convertPrice($price, $id_currency); @@ -4688,4 +4675,15 @@ class ProductCore extends ObjectModel return (bool)Db::getInstance()->getValue($query); } + + /** + * This method allows to flush price cache + * @static + * @since 1.5.0.2 + */ + public static function flushPriceCache() + { + self::$_prices = array(); + self::$_pricesLevel2 = array(); + } } diff --git a/classes/SpecificPrice.php b/classes/SpecificPrice.php index 39525e136..6c7696d74 100644 --- a/classes/SpecificPrice.php +++ b/classes/SpecificPrice.php @@ -77,6 +77,7 @@ class SpecificPriceCore extends ObjectModel { // Flush cache when we adding a new specific price self::$_specificPriceCache = array(); + Product::flushPriceCache(); // Set cache of feature detachable to true Configuration::updateGlobalValue('PS_SPECIFIC_PRICE_FEATURE_ACTIVE', '1'); return true; @@ -90,6 +91,7 @@ class SpecificPriceCore extends ObjectModel { // Flush cache when we updating a new specific price self::$_specificPriceCache = array(); + Product::flushPriceCache(); } } @@ -99,6 +101,7 @@ class SpecificPriceCore extends ObjectModel { // Flush cache when we deletind a new specific price self::$_specificPriceCache = array(); + Product::flushPriceCache(); // Refresh cache of feature detachable Configuration::updateGlobalValue('PS_SPECIFIC_PRICE_FEATURE_ACTIVE', self::isCurrentlyUsed($this->def['table'])); return true; diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 756aaaa65..d20de6966 100755 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -1248,13 +1248,6 @@ class AdminOrdersControllerCore extends AdminController $specific_price->add(); } - // Flush cache - // @TODO clean the following line used only in order to clean cache of getPriceStatic method - $specific_price_output = null; - Product::getPriceStatic($product->id, $use_taxes, isset($combination) ? $combination->id : null, 2, null, false, true, 1, - false, $order->id_customer, $cart->id, $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}, $specific_price_output, true, true, null, - true, true); - // Add product to cart $cart->updateQty($product_informations['product_quantity'], $product->id, isset($combination) ? $combination->id : null, false, 'up', new Shop($cart->id_shop)); diff --git a/js/admin_order.js b/js/admin_order.js index b70921de1..deaac952a 100644 --- a/js/admin_order.js +++ b/js/admin_order.js @@ -25,6 +25,7 @@ */ var current_product = null; +var ajaxQueries = new Array(); $(document).ready(function() { // Init all events @@ -33,6 +34,14 @@ $(document).ready(function() { $('img.js-disabled-action').css({"opacity":0.5}); }); +function stopAjaxQuery() { + if (typeof(ajaxQueries) == 'undefined') + ajaxQueries = new Array(); + for(i = 0; i < ajaxQueries.length; i++) + ajaxQueries[i].abort(); + ajaxQueries = new Array(); +} + function updateInvoice(invoices) { // Update select on product edition line @@ -372,6 +381,7 @@ function init() $('#submitAddProduct').click(function(e) { e.preventDefault(); + stopAjaxQuery(); var go = true; if ($('input#add_product_product_id').val() == 0) @@ -409,7 +419,7 @@ function init() if ($('select#add_product_product_invoice').val() == 0) query += '&'+$('tr#new_invoice select, tr#new_invoice input').serialize(); - $.ajax({ + var ajax_query = $.ajax({ type: 'POST', url: admin_order_tab_link, cache: false, @@ -437,6 +447,7 @@ function init() alert(data.error); } }); + ajaxQueries.push(ajax_query); } } });