Skip to content

Commit

Permalink
Fix max amount for discount on ONE product
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Jun 19, 2020
1 parent 00e0818 commit b6a5fe0
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions classes/CartRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,11 +1133,11 @@ public function getContextualValue($use_tax, Context $context = null, $filter =
if (in_array($filter, [CartRule::FILTER_ACTION_ALL, CartRule::FILTER_ACTION_ALL_NOCAP, CartRule::FILTER_ACTION_REDUCTION])) {
$order_package_products_total = 0;
if ((float) $this->reduction_amount > 0
|| $this->reduction_percent && $this->reduction_product == 0) {
|| (float) $this->reduction_percent && $this->reduction_product == 0) {
$order_package_products_total = $context->cart->getOrderTotal($use_tax, Cart::ONLY_PRODUCTS, $package_products);
}
// Discount (%) on the whole order
if ($this->reduction_percent && $this->reduction_product == 0) {
if ((float) $this->reduction_percent && $this->reduction_product == 0) {
// Do not give a reduction on free products!
$order_total = $order_package_products_total;
foreach ($context->cart->getCartRules(CartRule::FILTER_ACTION_GIFT, false) as $cart_rule) {
Expand All @@ -1161,7 +1161,7 @@ public function getContextualValue($use_tax, Context $context = null, $filter =
}

// Discount (%) on a specific product
if ($this->reduction_percent && $this->reduction_product > 0) {
if ((float) $this->reduction_percent && $this->reduction_product > 0) {
foreach ($package_products as $product) {
if ($product['id_product'] == $this->reduction_product && (($this->reduction_exclude_special && !$product['reduction_applies']) || !$this->reduction_exclude_special)) {
$reduction_value += ($use_tax ? $product['total_wt'] : $product['total']) * $this->reduction_percent / 100;
Expand All @@ -1170,7 +1170,7 @@ public function getContextualValue($use_tax, Context $context = null, $filter =
}

// Discount (%) on the cheapest product
if ($this->reduction_percent && $this->reduction_product == -1) {
if ((float) $this->reduction_percent && $this->reduction_product == -1) {
$minPrice = false;
$cheapest_product = null;
foreach ($all_products as $product) {
Expand Down Expand Up @@ -1200,7 +1200,7 @@ public function getContextualValue($use_tax, Context $context = null, $filter =
}

// Discount (%) on the selection of products
if ($this->reduction_percent && $this->reduction_product == -2) {
if ((float) $this->reduction_percent && $this->reduction_product == -2) {
$selected_products_reduction = 0;
$selected_products = $this->checkProductRestrictionsFromCart($context->cart, true);
if (is_array($selected_products)) {
Expand Down Expand Up @@ -1232,7 +1232,22 @@ public function getContextualValue($use_tax, Context $context = null, $filter =
}
}

$reduction_amount = $this->reduction_amount;
$reduction_amount = (float) $this->reduction_amount;
// If the cart rule is restricted to one product it can't exceed this product price
if ($this->reduction_product > 0) {
foreach ($context->cart->getProducts() as $product) {
if ($product['id_product'] == $this->reduction_product) {
if ($this->reduction_tax) {
$max_reduction_amount = (float) $product['price_wt'];
} else {
$max_reduction_amount = (float) $product['price'];
}
$reduction_amount = min($reduction_amount, $max_reduction_amount);
break;
}
}
}

// If we need to convert the voucher value to the cart currency
if (isset($context->currency) && $this->reduction_currency != $context->currency->id) {
$voucherCurrency = new Currency($this->reduction_currency);
Expand Down Expand Up @@ -1296,7 +1311,7 @@ public function getContextualValue($use_tax, Context $context = null, $filter =
}
}
/*
* Reduction on the cheapest or on the selection is not really meaningful and has been disabled in the backend
* Reduction on the cheapest or on the selection is not really meaningful and has been disabled in the backend, it only applies with percent
* Please keep this code, so it won't be considered as a bug
* elseif ($this->reduction_product == -1)
* elseif ($this->reduction_product == -2)
Expand Down

0 comments on commit b6a5fe0

Please sign in to comment.