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

Fix: Enhanced Product Collection's Price Range filter by improving currency parsing logic to discard unwanted characters #47354

Conversation

imanish003
Copy link
Contributor

@imanish003 imanish003 commented May 10, 2024

Changes proposed in this Pull Request:

This PR addresses an issue in the PriceTextField component, highlighted by @dinhtungdu in this github comment, where the input field would clear itself if users omitted a space between the numeric value and the currency symbol when the currency is configured to appear on the right with a space (e.g., "20$" instead of "20 $"). The problem was rooted in the strict parsing logic that did not account for variations in user input related to currency formatting.

The fix involves an enhancement to the numeric parsing function, which now effectively handles various user inputs regardless of space or placement of the currency symbol. The revised parsing logic uses a regular expression to strip out any characters that are not numeric or the designated decimal separator, and then replaces the locale-specific decimal separator with a period for standard numeric conversion. This approach not only fixes the specific issue but also improves the robustness of the component against similar input variations.

Closes #47351

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Create a new post
  2. Add Product Collection block & choose "Product Catalog" collection
  3. Add "Price Range" filter in Inspector controls
  4. Test Min & Max inputs of Price Filter. Verify that there is no issue. Here are some example scenarios to test

Case 1:

  1. In Currency Options, set currency position to "Right with space" as shown in screenshot below:
    image
  2. Add any value to "Min" field of Price Range filter, for example 20 and click away. You will notice that it will have a space between value & currency symbol i.e. "20 $". This is as expected.
  3. Now remove the space between value & currency symbol i.e. "20$" and click away. Verify that it should become "20 $".
    • Previously, the input field was getting cleared in this case, which was an issue.
  4. Publish the post and verify the only products within selected Price Range are shown on the Frontend.

Case 2:
Try entering a MIN or MAX price with a decimal, for example, "10.123764". After clicking away, number of decimals should become equal to as set in "Currency Options" as you can see in screenshot above. In my case, It should become "10.12 $"

Case 3:
Try entering random values. Here are some inputs and expected outputs:

  • 10ABCD -> 10 $
  • 10000 -> 10,000 $ (Note, how it's using a Thousand separator from Currency Options setting)
  • 1234 KJASHDH -> 1,234 $

As you can see above, it will try to parse numeric values from the input and discard unwanted characters like alphabets, space, etc.

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Fix: Enhanced Product Collection's Price Range filter by improving currency parsing logic to discard unwanted characters

Comment

…sing

This commit addresses a usability issue in the PriceTextField component, highlighted by @dinhtungdu, where the input field would clear itself if users omitted a space between the numeric value and the currency symbol when the currency is configured to appear on the right with a space (e.g., "20$" instead of "20 $"). The problem was rooted in the strict parsing logic that did not account for variations in user input related to currency formatting.

The fix involves an enhancement to the numeric parsing function, which now effectively handles various user inputs regardless of space or placement of the currency symbol. The revised parsing logic uses a regular expression to strip out any characters that are not numeric or the designated decimal separator, and then replaces the locale-specific decimal separator with a period for standard numeric conversion. This approach not only fixes the specific issue but also improves the robustness of the component against similar input variations.
Copy link
Contributor

github-actions bot commented May 10, 2024

Test using WordPress Playground

The changes in this pull request can be previewed and tested using a WordPress Playground instance.
WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Test this pull request with WordPress Playground.

Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit.

@imanish003 imanish003 changed the title Fix input clearing issue in PriceTextField with improved currency par… Fix input clearing issue in PriceTextField with improved currency parsing May 13, 2024
1. Renaming `formatValueAsCurrency` to `formatNumberAsCurrency` to better
   reflect its functionality.
2. Moving the `convertCurrencyStringToNumber` function outside of the
   PriceTextField component, enhancing readability and reusability.
3. Changing the internal state handling from `newValue` to `inputValue`,
   directly managing the string input and parsing it as needed for further
   processing. This will force input field to re-render.
@imanish003 imanish003 added type: bug The issue is a confirmed bug. team: Kirigami & Origami focus: FSE Issues related to Full Site Editing block: product collection Issues related to the Product Collection block labels May 14, 2024
@imanish003 imanish003 marked this pull request as ready for review May 14, 2024 11:52
@imanish003 imanish003 requested a review from kmanijak May 14, 2024 11:52
@woocommercebot woocommercebot requested a review from a team May 14, 2024 11:57
Copy link
Contributor

github-actions bot commented May 14, 2024

Hi @kmanijak, @woocommerce/woo-fse

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

@imanish003 imanish003 closed this May 15, 2024
@imanish003 imanish003 reopened this May 15, 2024
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label May 15, 2024
Copy link
Contributor

@kmanijak kmanijak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and parsing works as expected!

There's one typo in testing steps:

Now remove the space between value & currency symbol i.e. "20$" and click away. Verify that it should become "10 $".

Should be:

Now remove the space between value & currency symbol i.e. "20$" and click away. Verify that it should become "20 $".


Also, I found one minor issue. The default input value is empty (placeholder says Auto). But when you interact with input and remove the value, then it defaults to 0 which is kind of misleading:
image

In this image, there's MIN set to 0,01 and MAX set to nothing but displays 0. But I double checked it can be also reproduced in trunk so just raising for clarity! Otherwise approving!

@imanish003
Copy link
Contributor Author

There's one typo in testing steps:

I fixed it, thanks for pointing it out 🙏🏻

Also, I found one minor issue. The default input value is empty (placeholder says Auto). But when you interact with input and remove the value, then it defaults to 0 which is kind of misleading:

Good catch! I am looking into it. I can probably fix it with this PR as well.

…to fix/47351-product-collection-improve-handling-of-currency-input-formatting-in-price-range-filter
This commit fixes an issue where an empty string in the price conversion function resulted in a value of 0. Now, empty strings are properly handled and do not convert to a numerical value.
@imanish003
Copy link
Contributor Author

@kmanijak JFYI Fixed the issue in d47dadd because of which an empty string was getting converted to 0.

@imanish003 imanish003 merged commit a7a1264 into trunk May 16, 2024
35 checks passed
@imanish003 imanish003 deleted the fix/47351-product-collection-improve-handling-of-currency-input-formatting-in-price-range-filter branch May 16, 2024 07:53
@github-actions github-actions bot added this to the 9.0.0 milestone May 16, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label May 16, 2024
@alvarothomas alvarothomas added needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels May 17, 2024
@imanish003 imanish003 changed the title Fix input clearing issue in PriceTextField with improved currency parsing Fix: Enhanced Product Collection's Price Range filter by improving currency parsing logic to discard unwanted characters May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
block: product collection Issues related to the Product Collection block focus: FSE Issues related to Full Site Editing needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris team: Kirigami & Origami type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Product Collection: Improve Handling of Currency Input Formatting in Price Range Filter
3 participants