[-] FO : fixed infinite loop in free shipping cart rule #PSTEST-1138

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@14561 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
dMetzger
2012-04-10 16:57:50 +00:00
parent 13f16490a7
commit 46ed8664a4
2 changed files with 39 additions and 26 deletions
+38 -24
View File
@@ -280,12 +280,13 @@ class CartCore extends ObjectModel
return $this->getCartRules(); return $this->getCartRules();
} }
public function getCartRules() public function getCartRules($filter = CartRule::FILTER_ACTION_ALL)
{ {
// If the cart has not been saved, then there can't be any cart rule applied // If the cart has not been saved, then there can't be any cart rule applied
if (!CartRule::isFeatureActive() || !$this->id) if (!CartRule::isFeatureActive() || !$this->id)
return array(); return array();
$cache_key = 'Cart::getCartRules'.$this->id;
if (!Cache::isStored('Cart::getCartRules'.$this->id)) if (!Cache::isStored('Cart::getCartRules'.$this->id))
{ {
$result = Db::getInstance()->executeS(' $result = Db::getInstance()->executeS('
@@ -298,25 +299,26 @@ class CartCore extends ObjectModel
) )
WHERE `id_cart` = '.(int)$this->id WHERE `id_cart` = '.(int)$this->id
); );
Cache::store($cache_key, $result);
// Define virtual context to prevent case where the cart is not the in the global context
$virtual_context = Context::getContext()->cloneContext();
$virtual_context->cart = $this;
foreach ($result as &$row)
{
$row['obj'] = new CartRule($row['id_cart_rule'], (int)$this->id_lang);
$row['value_real'] = $row['obj']->getContextualValue(true, $virtual_context);
$row['value_tax_exc'] = $row['obj']->getContextualValue(false, $virtual_context);
// Retro compatibility < 1.5.0.2
$row['id_discount'] = $row['id_cart_rule'];
$row['description'] = $row['name'];
}
Cache::store('Cart::getCartRules'.$this->id, $result);
} }
return Cache::retrieve('Cart::getCartRules'.$this->id); $result = Cache::retrieve($cache_key);
// Define virtual context to prevent case where the cart is not the in the global context
$virtual_context = Context::getContext()->cloneContext();
$virtual_context->cart = $this;
foreach ($result as &$row)
{
$row['obj'] = new CartRule($row['id_cart_rule'], (int)$this->id_lang);
$row['value_real'] = $row['obj']->getContextualValue(true, $virtual_context, $filter);
$row['value_tax_exc'] = $row['obj']->getContextualValue(false, $virtual_context, $filter);
// Retro compatibility < 1.5.0.2
$row['id_discount'] = $row['id_cart_rule'];
$row['description'] = $row['name'];
}
return $result;
} }
public function getDiscountsCustomer($id_cart_rule) public function getDiscountsCustomer($id_cart_rule)
@@ -1247,6 +1249,8 @@ class CartCore extends ObjectModel
if (!in_array($type, $array_type)) if (!in_array($type, $array_type))
die(Tools::displayError()); die(Tools::displayError());
$with_shipping = in_array($type, array(Cart::BOTH, Cart::ONLY_SHIPPING));
// if cart rules are not used // if cart rules are not used
if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive()) if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive())
return 0; return 0;
@@ -1259,7 +1263,7 @@ class CartCore extends ObjectModel
if ($virtual && $type == Cart::BOTH) if ($virtual && $type == Cart::BOTH)
$type = Cart::BOTH_WITHOUT_SHIPPING; $type = Cart::BOTH_WITHOUT_SHIPPING;
if (!in_array($type, array(Cart::BOTH_WITHOUT_SHIPPING, Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING, Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING))) if ($with_shipping)
{ {
if (is_null($products) && is_null($id_carrier)) if (is_null($products) && is_null($id_carrier))
$shipping_fees = $this->getTotalShippingCost(null, (boolean)$with_taxes); $shipping_fees = $this->getTotalShippingCost(null, (boolean)$with_taxes);
@@ -1375,10 +1379,20 @@ class CartCore extends ObjectModel
$order_total_discount = 0; $order_total_discount = 0;
if ($type != Cart::ONLY_PRODUCTS && CartRule::isFeatureActive()) if ($type != Cart::ONLY_PRODUCTS && CartRule::isFeatureActive())
{ {
$result = $this->getCartRules(); if ($with_shipping)
foreach ($result as $row) $cart_rules = $this->getCartRules(CartRule::FILTER_ACTION_ALL);
$order_total_discount += Tools::ps_round($row['obj']->getContextualValue($with_taxes, $virtual_context), 2); else
$cart_rules = array_merge($this->getCartRules(CartRule::FILTER_ACTION_REDUCTION), $this->getCartRules(CartRule::FILTER_ACTION_GIFT));
foreach ($cart_rules as $cart_rule)
if ($with_shipping)
$order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_ALL), 2);
else
{
$order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION), 2);
$order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_GIFT), 2);
}
$order_total_discount = min(Tools::ps_round($order_total_discount, 2), $wrapping_fees + $order_total_products + $shipping_fees); $order_total_discount = min(Tools::ps_round($order_total_discount, 2), $wrapping_fees + $order_total_products + $shipping_fees);
$order_total -= $order_total_discount; $order_total -= $order_total_discount;
+1 -2
View File
@@ -682,7 +682,7 @@ class CartRuleCore extends ObjectModel
if ($this->free_shipping && ($filter == CartRule::FILTER_ACTION_ALL || $filter == CartRule::FILTER_ACTION_SHIPPING)) if ($this->free_shipping && ($filter == CartRule::FILTER_ACTION_ALL || $filter == CartRule::FILTER_ACTION_SHIPPING))
{ {
if (!$this->carrier_restriction) if (!$this->carrier_restriction)
$reduction_value += $context->cart->getTotalShippingCost(null, $useTax = true, $context->country); $reduction_value += $context->cart->getTotalShippingCost(null, $useTax, $context->country);
else else
{ {
$data = Db::getInstance()->executeS(' $data = Db::getInstance()->executeS('
@@ -699,7 +699,6 @@ class CartRuleCore extends ObjectModel
if ($filter == CartRule::FILTER_ACTION_ALL || $filter == CartRule::FILTER_ACTION_REDUCTION) if ($filter == CartRule::FILTER_ACTION_ALL || $filter == CartRule::FILTER_ACTION_REDUCTION)
{ {
// Discount (%) on the whole order // Discount (%) on the whole order
if ($this->reduction_percent && $this->reduction_product == 0) if ($this->reduction_percent && $this->reduction_product == 0)
$reduction_value += $context->cart->getOrderTotal($useTax, Cart::ONLY_PRODUCTS) * $this->reduction_percent / 100; $reduction_value += $context->cart->getOrderTotal($useTax, Cart::ONLY_PRODUCTS) * $this->reduction_percent / 100;