Prestashop: Exception error when multiple cart rules added

Created on 8 Aug 2019  Â·  16Comments  Â·  Source: PrestaShop/PrestaShop

Describe the bug
Warning: Division by zero in CartRuleCalculator.php line 240 exception when multiple cart rules added.

To Reproduce
Steps to reproduce the behavior:

  1. Create 3 cart rules, each one with a different gift product
    image
  2. Create a 4th cart rule, 1€ amount discount, tax included (with tax excluded the error doesn't appear).
    image
  3. Configure the price of the 3 products gifts configured in the previous cart rules to 1€
    image
    >[edited by Khouloud]

Products created must be with combinations & Tax =21% => Tax # 21% => OK

  1. Add a product to the cart
    image

Additional information
PrestaShop version: 1.7.6.0

1.7.5.2 1.7.6.0 Bug Cart rules Discounts FO Minor Shopping carts FO To Do

All 16 comments

Hi @idnovate,

I did not manage to reproduce the issue with PS1.7.6.0.
I attached a screen record
https://drive.google.com/file/d/1r8TOURpzqDCykUoydmQo8xpSxTakVT1g/view
Thanks to check & feedback.

You skipped step 3 :)

@idnovate, thanks for these clarifications.
Following your steps => OK > There is no exception
I attached a screen record
https://drive.google.com/file/d/1OTQyesAdmmrhNh1QFjcWvIh5igCRywZ3/view
Thanks to check & feedback.

Hi!

I managed to reproduce the issue with a fresh PS 1.7.6.1.

Please retry and choose "SPAIN" as store country, maybe it's applying different taxes than in FR.

@idnovate, I tried also with PS1.7.6.1 & it is Ok
I attached a screen record
https://drive.google.com/file/d/10bmI_wmqTle6JssPpdWX-6b6fbSB6Pt7/view

Thanks!

Please test it with a Spanish installation.

Look how my products prices look like:

image

@idnovate, no, I just added those products with no tax => same issue with Tax =20%
https://drive.google.com/file/d/1mg887RbRgvxbG9UPl1RD5d6KNFqhMrnO/view

Thanks!

Hi!

I attach you a video. It's quite long as I have recorded also installation.
https://drive.google.com/file/d/1q21un3FE_wh8NkUut4GFuMzlCZ6LoKk6/view

Developer console is not recorded (:disappointed_relieved:), but this is what appears after the product is added to the cart:
image

Hi @idnovate,

I manage to reproduce the issue with PS1.7.6.1. & PS1.7.6.0 & PS1.7.5.2 / Spain because in fact => we must have Tax = 21%

We need to have gifts with combinations (not standard products)
to reproduce the issue:

  1. Create 3 product with combinations => Configure the price of the 3 products to 1€ TTC
  2. Create 3 cart rules, each one with a different gift product (3 Products created previously)
  3. Create a 4th cart rule, 1€ amount discount, tax included
  4. Add a product to the cart

An error 500 is displayed in the FO
image

The FO doesn't work anymore.
PS: when we have products without combinations => OK & when Tax <> 21% => OK

I’ll add this to the debug roadmap so that it’s fixed. If you have already fixed it on your end or if you think you can do it, please do send us a pull request!
Thanks!

Hello ! This bug is fix on 1.7.6 ?

Hi @Hydrog3n,

Sorry, it is not fixed yet.

Thanks!

Hi @Hydrog3n,

Sorry, it is not fixed yet.

Thanks!

Ok thank for the info. Do you have a PR or a fix to fix this until you fix ?

@Hydrog3n,

Sorry no.
But PrestaShop is an open-source project, so it can be solved before if someone submits a pull request to solve it.

Thanks!

@Hydrog3n,

Sorry no.
But PrestaShop is an open-source project, so it can be solved before if someone submits a pull request to solve it.

Thanks!

Ok :) I know just if someone already do this I doesn't want to recode again. If I find I will PR ;)

Hi! @Hydrog3n

I have the same problem with Prestashop 1.7.6.4
The same error appears when:

  1. A gift product is added to cart
  2. Another voucher in discount price is added to cart

What I've checked is that this happens when:

  1. The gift product has a Tax
  2. The total price of the gift product (tax inc) is a integer

So for example it triggers with a gift that costs 3.0 but it doesn't trigger with a gift that costs 3.5

Then I tried to debug the problem (I'm not an expert programmer) and saw that when this case happened the variable $taxRate was equal to -1
Because of that the division /1-$taxRate gives an error in line 240.

I've parched adding
if($taxRate==-1){$taxRate = 0;}
before line 240

This should be solved properly however.

Hi @carlosul !

We are under 1.7.6.0 and I fix like this.
in the CartRuleCalculator.php Line 234

it's this block in master : https://github.com/PrestaShop/PrestaShop/blob/develop/src/Core/Cart/CartRuleCalculator.php#L243-L249

I don't remember why we don't PR this fix, but it's maybe it's not perfect for all cases.

                   // if cart rule amount is set tax included : calculate weight tax included
                    if ($totalTaxIncl != 0) {
                        $weightFactor = $concernedRow->getFinalTotalPrice()->getTaxIncluded() / $totalTaxIncl;
                    }
                    $discountAmountTaxIncl = $discountConverted * $weightFactor;

                    // Protect unkwown issue when $taxRate = -1
                    // https://github.com/PrestaShop/PrestaShop/issues/15041
                    if ($taxRate != -1) {
                        // recalculate tax included
                        $discountAmountTaxExcl = $discountAmountTaxIncl / (1 + $taxRate);
                    } else {
                        $discountAmountTaxExcl = $discountAmountTaxIncl;
                    }
Was this page helpful?
0 / 5 - 0 ratings