Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order amount changed when saving (due to unexpected scientific notation numbers) #28613

Open
5 tasks done
marcochiesi opened this issue Dec 17, 2020 · 11 comments
Open
5 tasks done
Labels
focus: calculation Issues related to calculations (for example, rounding issues). plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: normal The issue/PR is of normal priority—not many people are affected or there’s a workaround, etc. team: Proton WC Core development and maintenance type: bug The issue is a confirmed bug.

Comments

@marcochiesi
Copy link
Contributor

marcochiesi commented Dec 17, 2020

Prerequisites (mark completed items with an [x]):

  • I have carried out troubleshooting steps and I believe I have found a bug.
  • I have searched for similar bugs in both open and closed issues and cannot find a duplicate.

Describe the bug
In some circumstances, when simply saving the order from the WooCommerce backend, the order amount values get changed.

Expected behavior
When saving an order, amounts shouldn't be changed arbitrarily.

Actual behavior
The order total gets changed when simply saving the order (when some specific values are present).
This occurred on a production site but it can be reproduced on a vanilla WP with WooCommerce being the only plugin installed and with a default theme.
At first, I couldn't believe my eyes, but after further investigation, I found that the described bug depends on the presence of some very small float number in the line_tax fields that can be present after applying taxes and discounts. These values are rendered in the order edit form with the scientific notation (i.e. -8,0E-5). They appear as 0,00€ visually in the order table, but the hidden input gets the value in the scientific notation. When the form is submitted, those values are interpreted as integers, so i.e.
-8,0E-5€ becomes -8€, and it changes also the total of the order.
This is caused by the wc_format_localized_price function, which internally uses the strval function, which can return strings with numbers in scientific notation (i.e. 0.00008 becomes -8.0E-5).

This is the current implementation of that function:

function wc_format_localized_price( $value ) {
	return apply_filters( 'woocommerce_format_localized_price', str_replace( '.', wc_get_price_decimal_separator(), strval( $value ) ), $value );
}

At the moment I have used the following trick to detect and avoid the conversion to scientific notation:

add_filter( 'woocommerce_format_localized_price', 'bs_fix_prices_with_scientific_notation', 1, 2 );
function bs_fix_prices_with_scientific_notation( $string_value, $value ) {
	// Only modify the value when scientific notation is detected
	return preg_match( '/^[+-]?(?:\d+|\d*\.\d+)[eE][+-]?\d+$/', strval( $value ) ) ?
		str_replace( '.', wc_get_price_decimal_separator(), sprintf( "%F", $value ) ) :
		$string_value;
}

Only replacing strval with sprintf would lead to a lot of undesired side effects in other parts of the interface, so I preferred to explicitly detect the case of scientific notation and only act in that case.

Since this is a critical bug, I hope it can be fixed in WooCommerce as soon as possible. I would be glad to submit a pull request if the above approach would be considered valid (maybe there are better ways to fix the issue, i.e. let the scientific number notation be accepted when saving).

Steps to reproduce the bug (We need to be able to reproduce the bug in order to fix it.)
Steps to reproduce the bug:

  1. Install WooCommerce and skip the Wizard
  2. In WooCommerce General setting check "Enable tax rates and calculations" and save
  3. In WooCommerce General settings set Currency to Euro, Thousand separator to . and decimal separator to , and save
  4. In WooCommerce Tax settings Enable option "Round tax at subtotal level, instead of rounding per line" and save
  5. In WooCommerce Tax settings ensure that the option "Prices entered with tax" is set to "No, I will enter prices exclusive of tax" and set both "Display prices in the shop" and "Display prices during cart and checkout" to "Including tax" and save
  6. In WooCommerce Tax settings - Reduced rate rates, add a row with a 4% rate valid for all countries, states, etc
  7. In WooCommerce Payment settings enable a payment method (i.e. Direct Bank transfer)
  8. Create a Product A, with a price of 20,6731€ and with tax class "Reduced rate"
  9. Create a Product B, with a price of 1,058€ and with tax class "Reduced rate"
  10. Ensure Coupons are enabled and create a coupon with an amount of 14,423077€ of type "Fixed cart discount"
  11. On the frontend add 6x Product A and 1x Product B to the cart
  12. Apply the previously created coupon and ensure the discount is applied and submit the order. Order total should be 115,09€
  13. On the backend edit the order and just hit Update. The order total gets changed to 107,09€

Screenshots
woocommerce-bug

See a full video recording (4 minutes) here: https://drive.google.com/file/d/1TJDqD4xnmSXXv8yS5v740S9XOrTUX-xl/view?usp=sharing

Isolating the problem (mark completed items with an [x]):

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.

WordPress Environment

``` ### WordPress Environment ###

WordPress address (URL): http://woo-test-2.local
Site address (URL): http://woo-test-2.local
WC Version: 4.8.0
REST API Version: ✔ 4.8.0
WC Blocks Version: ✔ 3.8.1
Action Scheduler Version: ✔ 3.1.6
WC Admin Version: ✔ 1.7.3
Log Directory Writable: ✔
WP Version: 5.6
WP Multisite: –
WP Memory Limit: 256 MB
WP Debug Mode: –
WP Cron: ✔
Language: en_US
External object cache: –

Server Environment

Server Info: nginx/1.16.0
PHP Version: 7.3.5
PHP Post Max Size: 1,000 MB
PHP Time Limit: 1200
PHP Max Input Vars: 4000
cURL Version: 7.64.0
OpenSSL/1.1.1b

SUHOSIN Installed: –
MySQL Version: 8.0.16
Max Upload Size: 300 MB
Default Timezone is UTC: ✔
fsockopen/cURL: ✔
SoapClient: ✔
DOMDocument: ✔
GZip: ✔
Multibyte String: ✔
Remote Post: ✔
Remote Get: ✔

Database

WC Database Version: 4.8.0
WC Database Prefix: wp_
Total Database Size: 3.70MB
Database Data Size: 2.32MB
Database Index Size: 1.38MB
wp_woocommerce_sessions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_woocommerce_order_items: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_order_itemmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_log: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_actionscheduler_actions: Data: 0.02MB + Index: 0.11MB + Engine InnoDB
wp_actionscheduler_claims: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_actionscheduler_groups: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_actionscheduler_logs: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_commentmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_comments: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
wp_links: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_options: Data: 1.48MB + Index: 0.03MB + Engine InnoDB
wp_postmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_posts: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_term_relationships: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_term_taxonomy: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_termmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_terms: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_usermeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_users: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_wc_admin_note_actions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_wc_admin_notes: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wc_category_lookup: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wc_customer_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_download_log: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_order_coupon_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_order_product_lookup: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_wc_order_stats: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_wc_order_tax_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_product_meta_lookup: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
wp_wc_reserved_stock: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wc_tax_rate_classes: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_wc_webhooks: Data: 0.02MB + Index: 0.02MB + Engine InnoDB

Post Type Counts

attachment: 1
page: 6
post: 2
product: 2
shop_coupon: 1
shop_order: 4

Security

Secure connection (HTTPS): ❌
Your store is not using HTTPS. Learn more about HTTPS and SSL Certificates.
Hide errors from visitors: ✔

Active Plugins (1)

WooCommerce: by Automattic – 4.8.0

Inactive Plugins (0)

Settings

API Enabled: –
Force SSL: –
Currency: EUR (€)
Currency Position: left
Thousand Separator: .
Decimal Separator: ,
Number of Decimals: 2
Taxonomies: Product Types: external (external)
grouped (grouped)
simple (simple)
variable (variable)

Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog)
exclude-from-search (exclude-from-search)
featured (featured)
outofstock (outofstock)
rated-1 (rated-1)
rated-2 (rated-2)
rated-3 (rated-3)
rated-4 (rated-4)
rated-5 (rated-5)

Connected to WooCommerce.com: –

WC Pages

Shop base: #6 - /shop/
Cart: #7 - /cart/
Checkout: #8 - /checkout/
My account: #9 - /my-account/
Terms and conditions: ❌ Page not set

Theme

Name: Twenty Twenty-One
Version: 1.0
Author URL: https://wordpress.org/
Child Theme: ❌ – If you are modifying WooCommerce on a parent theme that you did not build personally we recommend using a child theme. See: How to create a child theme
WooCommerce Support: ✔

Templates

Overrides: –

Action Scheduler

Complete: 12
Oldest: 2020-12-17 13:31:29 +0000
Newest: 2020-12-17 13:53:07 +0000

</details>
@tammullen tammullen added the needs: developer feedback Issues that need feedback from one of the WooCommerce Core developers. label Dec 20, 2020
@rrennick
Copy link
Contributor

@marcochiesi Thanks for reporting the issue. I tested this with WooCommerce 5.8.0 and the discount amount on the product was rounded correctly:

  • Product with 4 decimal places in price

Screen Shot 2021-10-22 at 12 58 25 PM

  • Coupon with 6 decimal places

Screen Shot 2021-10-22 at 12 58 06 PM

  • Product discount rounded to 2 decimal places

Screen Shot 2021-10-22 at 12 57 28 PM

Are you still experiencing this issue?

@rrennick rrennick added needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. and removed needs: developer feedback Issues that need feedback from one of the WooCommerce Core developers. labels Oct 22, 2021
@marcochiesi
Copy link
Contributor Author

Hello @rrennick
Thank you for your reply. I have just re-tested on a vanilla WordPress 5.8.1 with WooCommerce 5.8.0 (being the only plugin installed) and unfortunately, the bug is still present.
Please follow very accurately the steps to reproduce the bug that I have specified in my first message, especially the amounts and quantities. As far as I can see you have used different amounts, and I believe that the bug occurs only with specific amounts, due to rounding on numbers that are next to zero. Please let me know if you need any additional information.
Looking forward to seeing this issue fixed.

@github-actions github-actions bot added needs: triage feedback Issues for which we requested feedback from the author and received it. and removed needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. labels Oct 22, 2021
@rrennick
Copy link
Contributor

@marcochiesi Thanks for the follow up.

@rrennick rrennick added the status: reproduction Bug reports that need to be reproduced and confirmed. label Oct 26, 2021
@swatipawarGS
Copy link

swatipawarGS commented Nov 18, 2021

Hi @marcochiesi ,

Thank you for the updated information on this issue. However, we couldn't reproduce it using the steps provided you. Everything works as expected on our end using WordPress 5.8.2 and WooCommerce 5.9.0 version.

Below is the screencast documenting my experience :

28613.mp4

Please provide us with more details and any additional steps required to repro this issue which may help us to evaluate it further.

@tammullen tammullen added type: bug The issue is a confirmed bug. focus: order Issues related to orders. and removed needs: triage feedback Issues for which we requested feedback from the author and received it. status: reproduction Bug reports that need to be reproduced and confirmed. type: bug The issue is a confirmed bug. focus: order Issues related to orders. labels Nov 19, 2021
@tammullen
Copy link
Contributor

Thanks @swatipawarGS for your screencast but it appears to show the price remains the same after pressing update. Can you try again but make sure to include in your screencast all the steps in the original report?

@tammullen tammullen added needs: triage feedback Issues for which we requested feedback from the author and received it. status: reproduction Bug reports that need to be reproduced and confirmed. labels Nov 19, 2021
@swatipawarGS
Copy link

Hi @marcochiesi ,

Thank you for submitting the issue. However, I can’t reproduce it using the steps you provided. Everything is working as expected on my end using WooCommerce 5.9.0 version.

Below is the screencast documenting my experience :

28613.2.1.mp4

@marcochiesi
Copy link
Contributor Author

Hello @tammullen and @swatipawarGS
being the original reporter of the issue, I get email notifications related to this thread.
A few days (Nov 17) a strange thing occurred: I got a notification of a message from @swatipawarGS with a video actually showing the issue, but that message is not present here on GitHub. The day after @swatipawarGS posted the same message (visible above) but with a different (similar) video, where the issue was not appearing.
I am not sure why the original message was lost, but the attached video from @swatipawarGS (actually showing the issue) is still available at this URL:
https://user-images.githubusercontent.com/94531721/142232803-465b729f-9447-4a01-86c4-c941f73a92cd.mp4 (in particular check starting at minute 1:40 and following).

@marcochiesi
Copy link
Contributor Author

P.s. For the sake of completeness, I confirm the bug is still present on a vanilla WordPress 5.8.2 with WooCommerce 5.9.0 (being the only plugin installed), and it can be reproduced by following accurately the step-by-step instructions listed in the very first post of this thread.

@swatipawarGS
Copy link

swatipawarGS commented Nov 22, 2021

Hi @marcochiesi ,

Thank you for the updated information on this issue. However, we couldn't reproduce it using the steps provided you. Everything works as expected on our end using WordPress 5.8.2 and WooCommerce 5.9.0 version.

Below is the screencast documenting my experience :

28613.mp4

Please provide us with more details and any additional steps required to repro this issue which may help us to evaluate it further.

@marcochiesi
Copy link
Contributor Author

@swatipawarGS In your latest video, you do not show all the steps provided in the initial instructions, so I suspect you didn't enter the exact same amounts for the products and the coupon that were provided in the initial report. Please follow accurately all the instructions and you should be able to reproduce the issue. See also the video attached to the initial report; even if it was based on a previous version of WooCommerce, the situation is the same also with the latest one. Please let me know if you want me to record a new video with the latest version.

@swatipawarGS
Copy link

Hi @marcochiesi ,

Thank you for the updated information. We confirm that the issue is reproducible on our end as well using WooCommerce 5.9.0 version and WordPress 5.8.2 version.

Below is the screencast documenting my experience :

28613.2.mp4

Priority of this issue will be assigned shortly after we’d go through a round of prioritization. Please note it may take a couple of days.

@tammullen tammullen added type: bug The issue is a confirmed bug. and removed needs: triage feedback Issues for which we requested feedback from the author and received it. status: reproduction Bug reports that need to be reproduced and confirmed. labels Dec 1, 2021
@ObliviousHarmony ObliviousHarmony added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Feb 21, 2022
@peterfabian peterfabian added the priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. label May 19, 2022
@peterfabian peterfabian added the focus: calculation Issues related to calculations (for example, rounding issues). label May 19, 2022
@rrennick rrennick added the team: Proton WC Core development and maintenance label Sep 22, 2023
@barryhughes barryhughes added priority: normal The issue/PR is of normal priority—not many people are affected or there’s a workaround, etc. and removed priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. labels May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: calculation Issues related to calculations (for example, rounding issues). plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: normal The issue/PR is of normal priority—not many people are affected or there’s a workaround, etc. team: Proton WC Core development and maintenance type: bug The issue is a confirmed bug.
Projects
Status: No status
Development

No branches or pull requests

7 participants