use CartRule::autoRemoveFromCart function to remove extra cart rules
This commit is contained in:
@@ -1032,7 +1032,8 @@ class CartCore extends ObjectModel
|
||||
$this->updateQty(1, $cartRule->gift_product, $cartRule->gift_product_attribute, false, 'up', 0, null, false);
|
||||
}
|
||||
// after adding new cart rule, check if any of the appled cart rules are not being used
|
||||
CartRule::removeUnusedCartRules(Context::getContext());
|
||||
CartRule::autoRemoveFromCart(Context::getContext());
|
||||
CartRule::autoAddToCart(Context::getContext());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1235,8 +1236,6 @@ class CartCore extends ObjectModel
|
||||
if ($auto_add_cart_rule) {
|
||||
CartRule::autoAddToCart($context);
|
||||
}
|
||||
// after cart qty is updated, check if any of the appled cart rules are not being used
|
||||
CartRule::removeUnusedCartRules($context);
|
||||
|
||||
if ($product->customizable) {
|
||||
return $this->_updateCustomizationQuantity((int)$quantity, (int)$id_customization, (int)$id_product, (int)$id_product_attribute, (int)$id_address_delivery, $operator);
|
||||
@@ -1485,8 +1484,6 @@ class CartCore extends ObjectModel
|
||||
if ($auto_add_cart_rule) {
|
||||
CartRule::autoAddToCart();
|
||||
}
|
||||
// after cart qty is updated, check if any of the appled cart rules are not being used
|
||||
CartRule::removeUnusedCartRules();
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
@@ -1380,35 +1380,25 @@ class CartRuleCore extends ObjectModel
|
||||
return array();
|
||||
}
|
||||
|
||||
$order_total = $context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS_WITH_DEMANDS, null, null, false);
|
||||
static $errors = array();
|
||||
foreach ($context->cart->getCartRules() as $cart_rule) {
|
||||
if ($error = $cart_rule['obj']->checkValidity($context, true)) {
|
||||
$context->cart->removeCartRule($cart_rule['obj']->id);
|
||||
$context->cart->update();
|
||||
$errors[] = $error;
|
||||
} else {
|
||||
if ($order_total <= 0) {
|
||||
// remove cart rule if cart amount is already reached 0
|
||||
$context->cart->removeCartRule($cart_rule['obj']->id);
|
||||
} else {
|
||||
$order_total = $order_total - $cart_rule['value_real'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public static function removeUnusedCartRules($context = null)
|
||||
{
|
||||
if (!$context) {
|
||||
$context = Context::getContext();
|
||||
}
|
||||
$cartRules = $context->cart->getCartRules();
|
||||
$orderTotal = $context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS_WITH_DEMANDS, null, null, false);
|
||||
// ppp([$cartRules, $orderTotal]);
|
||||
foreach ($cartRules as $cartRule) {
|
||||
if ($orderTotal <= 0) {
|
||||
// remove cart rule if cart amount is already reached 0
|
||||
$context->cart->removeCartRule($cartRule['id_cart_rule']);
|
||||
} else {
|
||||
$orderTotal = $orderTotal - $cartRule['value_real'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Context|null $context
|
||||
* @return mixed
|
||||
|
||||
@@ -431,7 +431,8 @@ class HotelCartBookingData extends ObjectModel
|
||||
}
|
||||
}
|
||||
// after room is removed from cart, check if any of the appled cart rules are not being used
|
||||
CartRule::removeUnusedCartRules(Context::getContext());
|
||||
CartRule::autoRemoveFromCart(Context::getContext());
|
||||
CartRule::autoAddToCart(Context::getContext());
|
||||
|
||||
// return number of rooms deleted
|
||||
return true;
|
||||
@@ -526,8 +527,6 @@ class HotelCartBookingData extends ObjectModel
|
||||
}
|
||||
}
|
||||
if ($res && $objCart->updateQty((int)($roomsRequired * $num_days), $id_product)) {
|
||||
// after room is added in cart, check if any of the appled cart rules are not being used
|
||||
CartRule::removeUnusedCartRules(Context::getContext());
|
||||
if ($id_room) {
|
||||
return $obj_htl_cart_booking_data->id;
|
||||
} else {
|
||||
@@ -1913,7 +1912,8 @@ class HotelCartBookingData extends ObjectModel
|
||||
{
|
||||
$return = parent::save($null_values = false, $auto_date = true);
|
||||
// after updating cart data, check if any of the appled cart rules are not being used
|
||||
CartRule::removeUnusedCartRules(Context::getContext());
|
||||
CartRule::autoRemoveFromCart(Context::getContext());
|
||||
CartRule::autoAddToCart(Context::getContext());
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user