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

reset state and postcode if it's not valid for the current country #47369

Merged
merged 5 commits into from May 13, 2024

Conversation

senadir
Copy link
Member

@senadir senadir commented May 10, 2024

When switching countries, Checkout would sometimes clear out state and zipcode, there are some cases in which those fields are not cleared correctly, and the server end up validating the current country against the old state and postcode. This PR resets them when changing country, and make sure to reset them as well if you're changing country, this is because state and postcode would always be validated client side first.

Initially I tried fixing this at server side, but it caused issues when placing an order, this feels like a good middleground.

Closes #46270

How to test the changes in this Pull Request:

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

  1. Select a country with states, like Algeria, and fill out a fill valid address: Oran, Oran, 31000.
  2. Switch to a country without states, like Afghanistan.
  3. Switch back to a country with states like Spain, notice lack of error.

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

Correctly clear out state and postcode when switching countries.

Comment

@senadir senadir added type: bug The issue is a confirmed bug. focus: checkout Issues related to checkout page. team: Rubik Store API checkout endpoints, Mini-Cart, Cart and Checkout related issues labels May 10, 2024
@senadir senadir self-assigned this May 10, 2024
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label May 10, 2024
@woocommercebot woocommercebot requested review from a team and nielslange and removed request for a team May 10, 2024 15:48
Copy link
Contributor

github-actions bot commented May 10, 2024

Hi @nielslange, @opr,

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

Comment on lines -249 to -257
/**
* There's a bug in WooCommerce core in which not having a state ("") would result in us validating against the store's state.
* This resets the state to an empty string if it doesn't match the country.
*
* @todo Removing this handling once we fix the issue with the state value always being the store one.
*/
if ( ! $validation_util->validate_state( $billing_state, $billing_country ) ) {
$billing_state = '';
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is no longer needed now that we reset at sanization level, I also tested this PR steps and they're still working

woocommerce/woocommerce-blocks#8460

Comment on lines 132 to 135
$carry[ $key ] = $validation_util->validate_state( $address['state'], $address['country'] ) ? $validation_util->format_state( sanitize_text_field( wp_unslash( $address[ $key ] ) ), $address['country'] ) : '';
break;
case 'postcode':
$carry[ $key ] = $address['postcode'] ? wc_format_postcode( sanitize_text_field( wp_unslash( $address['postcode'] ) ), $address['country'] ) : '';
$carry[ $key ] = \WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ? wc_format_postcode( sanitize_text_field( wp_unslash( $address['postcode'] ) ), $address['country'] ) : '';
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to reset both postcode and state

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.

@nielslange nielslange closed this May 13, 2024
@nielslange nielslange reopened this May 13, 2024
Copy link
Member

@nielslange nielslange left a comment

Choose a reason for hiding this comment

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

Thanks for working on this, @senadir. The fix works as expected. I just closed and re-opened the PR so that the CI checks can run. Let's ensure that all CI checks are passing before merging this PR.

@senadir
Copy link
Member Author

senadir commented May 13, 2024

Some tests are failing and they seem to be valid, will look into them.

opr
opr previously requested changes May 13, 2024
Copy link
Contributor

@opr opr left a comment

Choose a reason for hiding this comment

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

When testing this using Store API, I was able to place an order with a JP address with an invalid postcode and an invalid state.

I think this is because we're unsetting invalid values in the AbstractAddressSchema sanitize_callback and this also happens on checkout. If there's a way we can limit that logic to run only on the cart/update-customer route that would be better.

@senadir
Copy link
Member Author

senadir commented May 13, 2024

I will look into that, it's strange because I didn't remove any validation (not counting the one in get_address_x), I added more validation, I will look into this. I knew this issue would be tricky.

@senadir senadir force-pushed the fix/state-flickring-when-switching-countries branch from e2f18c0 to 3cdc448 Compare May 13, 2024 13:37
@senadir senadir requested review from opr and nielslange May 13, 2024 13:37
@senadir
Copy link
Member Author

senadir commented May 13, 2024

I changed the approach in the PR, would love a second review.

@senadir senadir dismissed opr’s stale review May 13, 2024 13:55

Changed the approch

Copy link
Contributor

@opr opr left a comment

Choose a reason for hiding this comment

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

This is working nicely for me now, thanks Nadir!

@senadir senadir merged commit b256336 into trunk May 13, 2024
36 checks passed
@senadir senadir deleted the fix/state-flickring-when-switching-countries branch May 13, 2024 14:46
@github-actions github-actions bot added this to the 9.0.0 milestone May 13, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label May 13, 2024
@rodelgc rodelgc added status: analysis complete Indicates if a PR has been analysed by Solaris needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: checkout Issues related to checkout page. 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: Rubik Store API checkout endpoints, Mini-Cart, Cart and Checkout related issues type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Changing between countries with states results in an error in the Checkout block
4 participants