From 08b695f1a84ce85be34d17c6777238a6d4ed769d Mon Sep 17 00:00:00 2001 From: idnovate Date: Fri, 3 May 2019 10:17:40 +0200 Subject: [PATCH 1/8] Display "Free shipping" cart rule correctly Fixes issue #13644 --- src/Adapter/Presenter/Cart/CartPresenter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index 486a941d09036..9938c0b992c62 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -546,7 +546,9 @@ private function getTemplateVarVouchers(Cart $cart) $cartVoucher['reduction_amount'] = $cartVoucher['value_real']; } - if (isset($cartVoucher['reduction_percent']) && $cartVoucher['reduction_amount'] == '0.00') { + if (isset($cartVoucher['free_shipping']) && $cartVoucher['free_shipping']) { + $cartVoucher['reduction_formatted'] = $this->priceFormatter->convertAndFormat($cart->getTotalShippingCost(null, $this->includeTaxes())); + } elseif (isset($cartVoucher['reduction_percent']) && $cartVoucher['reduction_amount'] == '0.00') { $cartVoucher['reduction_formatted'] = $cartVoucher['reduction_percent'] . '%'; } elseif (isset($cartVoucher['reduction_amount']) && $cartVoucher['reduction_amount'] > 0) { $value = $this->includeTaxes() ? $cartVoucher['reduction_amount'] : $cartVoucher['value_tax_exc']; From 692242099d21981b30a6de800a872c8dc1f01fe1 Mon Sep 17 00:00:00 2001 From: matthieu rolland Date: Thu, 31 Oct 2019 16:20:07 +0100 Subject: [PATCH 2/8] Apply correct cart voucher reductions values in cart summary presentation --- src/Adapter/Presenter/Cart/CartPresenter.php | 85 +++++++++++++++++--- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index 9938c0b992c62..5356f879cac57 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -526,7 +526,7 @@ private function getTemplateVarVouchers(Cart $cart) $vouchers = array(); $cartHasTax = null === $cart->id ? false : $cart::getTaxesAverageUsed($cart); - + $freeShippingAlreadySet = false; foreach ($cartVouchers as $cartVoucher) { $vouchers[$cartVoucher['id_cart_rule']]['id_cart_rule'] = $cartVoucher['id_cart_rule']; $vouchers[$cartVoucher['id_cart_rule']]['name'] = $cartVoucher['name']; @@ -546,30 +546,51 @@ private function getTemplateVarVouchers(Cart $cart) $cartVoucher['reduction_amount'] = $cartVoucher['value_real']; } - if (isset($cartVoucher['free_shipping']) && $cartVoucher['free_shipping']) { - $cartVoucher['reduction_formatted'] = $this->priceFormatter->convertAndFormat($cart->getTotalShippingCost(null, $this->includeTaxes())); - } elseif (isset($cartVoucher['reduction_percent']) && $cartVoucher['reduction_amount'] == '0.00') { - $cartVoucher['reduction_formatted'] = $cartVoucher['reduction_percent'] . '%'; - } elseif (isset($cartVoucher['reduction_amount']) && $cartVoucher['reduction_amount'] > 0) { - $value = $this->includeTaxes() ? $cartVoucher['reduction_amount'] : $cartVoucher['value_tax_exc']; + $shippingReduction = $amountReduction = $percentageReduction = 0; + $freeShippingOnly = false; + + if ($this->cartVoucherHasFreeShipping($cartVoucher)) { + if (!$freeShippingAlreadySet) { + $shippingReduction = $cart->getTotalShippingCost(null, $this->includeTaxes()); + $freeShippingAlreadySet = true; + } + $freeShippingOnly = true; + } + if ($this->cartVoucherHasPercentReduction($cartVoucher)) { + $productsTotalExcludingTax = $cart->getOrderTotal(true, Cart::ONLY_PRODUCTS); + $percentageReduction = ($productsTotalExcludingTax / 100) * $cartVoucher['reduction_percent']; + $freeShippingOnly = false; + } elseif ($this->cartVoucherHasAmountReduction($cartVoucher)) { + $amountReduction = $this->includeTaxes() ? $cartVoucher['reduction_amount'] : $cartVoucher['value_tax_exc']; $currencyFrom = new \Currency($cartVoucher['reduction_currency']); $currencyTo = new \Currency($cart->id_currency); if ($currencyFrom->conversion_rate == 0) { - $value = 0; + $amountReduction = 0; } else { // convert to default currency $defaultCurrencyId = (int) Configuration::get('PS_CURRENCY_DEFAULT'); - $value /= $currencyFrom->conversion_rate; + $amountReduction /= $currencyFrom->conversion_rate; if ($defaultCurrencyId == $currencyTo->id) { // convert to destination currency - $value *= $currencyTo->conversion_rate; + $amountReduction *= $currencyTo->conversion_rate; } } - // following will do currency conversion to current one - $cartVoucher['reduction_formatted'] = $this->priceFormatter->convertAndFormat($value); + $freeShippingOnly = false; + } + // when a voucher has only a shipping reduction, the value displayed must be "Free Shipping" + if ($freeShippingOnly) { + $cartVoucher['reduction_formatted'] = $this->translator->trans( + 'Free Shipping', + [], + 'Admin.Orderscustomers.Feature' + ); + } else { + // In all other cases, the value displayed should be the total of applied reductions for the current voucher + $totalCartVoucherReduction = $shippingReduction + $amountReduction + $percentageReduction; + $cartVoucher['reduction_formatted'] = '-' . $this->priceFormatter->convertAndFormat($totalCartVoucherReduction); } - $vouchers[$cartVoucher['id_cart_rule']]['reduction_formatted'] = '-' . $cartVoucher['reduction_formatted']; + $vouchers[$cartVoucher['id_cart_rule']]['reduction_formatted'] = $cartVoucher['reduction_formatted']; $vouchers[$cartVoucher['id_cart_rule']]['delete_url'] = $this->link->getPageLink( 'cart', true, @@ -587,6 +608,44 @@ private function getTemplateVarVouchers(Cart $cart) ); } + /** + * @param array $cartVoucher + * + * @return bool + */ + private function cartVoucherHasFreeShipping($cartVoucher) + { + return isset($cartVoucher['free_shipping']) && $cartVoucher['free_shipping']; + } + + /** + * @param array $cartVoucher + * + * @return bool + */ + private function cartVoucherHasPercentReduction($cartVoucher) + { + if ( + isset($cartVoucher['reduction_percent']) + && $cartVoucher['reduction_percent'] > 0 + && $cartVoucher['reduction_amount'] == '0.00' + ) { + return true; + } + + return false; + } + + /** + * @param array $cartVoucher + * + * @return bool + */ + private function cartVoucherHasAmountReduction($cartVoucher) + { + return isset($cartVoucher['reduction_amount']) && $cartVoucher['reduction_amount'] > 0; + } + /** * Receives a string containing a list of attributes affected to the product and returns them as an array. * From 3dc00491296849bc81f30647e99941a227a4d75d Mon Sep 17 00:00:00 2001 From: matthieu rolland Date: Thu, 31 Oct 2019 17:04:01 +0100 Subject: [PATCH 3/8] take taxes into account for percentage reduction amount --- src/Adapter/Presenter/Cart/CartPresenter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index 5356f879cac57..e32812af35dc1 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -557,7 +557,7 @@ private function getTemplateVarVouchers(Cart $cart) $freeShippingOnly = true; } if ($this->cartVoucherHasPercentReduction($cartVoucher)) { - $productsTotalExcludingTax = $cart->getOrderTotal(true, Cart::ONLY_PRODUCTS); + $productsTotalExcludingTax = $cart->getOrderTotal($this->includeTaxes(), Cart::ONLY_PRODUCTS); $percentageReduction = ($productsTotalExcludingTax / 100) * $cartVoucher['reduction_percent']; $freeShippingOnly = false; } elseif ($this->cartVoucherHasAmountReduction($cartVoucher)) { From 6faf288db50729822470300135962f779f68cff4 Mon Sep 17 00:00:00 2001 From: Matthieu Rolland Date: Mon, 4 Nov 2019 13:16:15 +0100 Subject: [PATCH 4/8] Update src/Adapter/Presenter/Cart/CartPresenter.php Co-Authored-By: LouiseBonnard <32565890+LouiseBonnard@users.noreply.github.com> --- src/Adapter/Presenter/Cart/CartPresenter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index e32812af35dc1..04f0a3ad0f57b 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -580,7 +580,7 @@ private function getTemplateVarVouchers(Cart $cart) // when a voucher has only a shipping reduction, the value displayed must be "Free Shipping" if ($freeShippingOnly) { $cartVoucher['reduction_formatted'] = $this->translator->trans( - 'Free Shipping', + 'Free shipping', [], 'Admin.Orderscustomers.Feature' ); From a9b62772f8b14e727acb96c43cc4faa367772709 Mon Sep 17 00:00:00 2001 From: Matthieu Rolland Date: Mon, 4 Nov 2019 13:16:22 +0100 Subject: [PATCH 5/8] Update src/Adapter/Presenter/Cart/CartPresenter.php Co-Authored-By: LouiseBonnard <32565890+LouiseBonnard@users.noreply.github.com> --- src/Adapter/Presenter/Cart/CartPresenter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index 04f0a3ad0f57b..eafaeff43700e 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -582,7 +582,7 @@ private function getTemplateVarVouchers(Cart $cart) $cartVoucher['reduction_formatted'] = $this->translator->trans( 'Free shipping', [], - 'Admin.Orderscustomers.Feature' + 'Admin.Shipping.Feature' ); } else { // In all other cases, the value displayed should be the total of applied reductions for the current voucher From 7a54da0e7d08e489f8b11adc20cd664759a3b907 Mon Sep 17 00:00:00 2001 From: Matthieu Rolland Date: Tue, 5 Nov 2019 10:51:11 +0100 Subject: [PATCH 6/8] Improve condition Co-Authored-By: GoT --- src/Adapter/Presenter/Cart/CartPresenter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index eafaeff43700e..eaf93e04cd4db 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -615,7 +615,7 @@ private function getTemplateVarVouchers(Cart $cart) */ private function cartVoucherHasFreeShipping($cartVoucher) { - return isset($cartVoucher['free_shipping']) && $cartVoucher['free_shipping']; + return !empty($cartVoucher['free_shipping']); } /** From 915627ae59e9d93cec7bb88683b415d6786a7da1 Mon Sep 17 00:00:00 2001 From: Matthieu Rolland Date: Tue, 5 Nov 2019 10:51:39 +0100 Subject: [PATCH 7/8] improve function return Co-Authored-By: GoT --- src/Adapter/Presenter/Cart/CartPresenter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index eaf93e04cd4db..fec5b49c5faf2 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -625,7 +625,9 @@ private function cartVoucherHasFreeShipping($cartVoucher) */ private function cartVoucherHasPercentReduction($cartVoucher) { - if ( + return isset($cartVoucher['reduction_percent']) + && $cartVoucher['reduction_percent'] > 0 + && $cartVoucher['reduction_amount'] == '0.00'; isset($cartVoucher['reduction_percent']) && $cartVoucher['reduction_percent'] > 0 && $cartVoucher['reduction_amount'] == '0.00' From 138b7f2b0158afc0d9160eb07314e308d8932678 Mon Sep 17 00:00:00 2001 From: matthieu rolland Date: Tue, 5 Nov 2019 11:16:44 +0100 Subject: [PATCH 8/8] fix condition --- src/Adapter/Presenter/Cart/CartPresenter.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Adapter/Presenter/Cart/CartPresenter.php b/src/Adapter/Presenter/Cart/CartPresenter.php index fec5b49c5faf2..2061c9aa21bcf 100644 --- a/src/Adapter/Presenter/Cart/CartPresenter.php +++ b/src/Adapter/Presenter/Cart/CartPresenter.php @@ -628,14 +628,6 @@ private function cartVoucherHasPercentReduction($cartVoucher) return isset($cartVoucher['reduction_percent']) && $cartVoucher['reduction_percent'] > 0 && $cartVoucher['reduction_amount'] == '0.00'; - isset($cartVoucher['reduction_percent']) - && $cartVoucher['reduction_percent'] > 0 - && $cartVoucher['reduction_amount'] == '0.00' - ) { - return true; - } - - return false; } /**