-
Notifications
You must be signed in to change notification settings - Fork 219
Fix 'Country is required' error on the Cart block when updating shipping address #5129
Conversation
…to update the billing address in the internal state of the useCustomerData hook
Size Change: +355 B (0%) Total Size: 1.11 MB
ℹ️ View Unchanged
|
…hipping addresses only when needed in API calls
billingDataRef.current = initialBillingAddress; | ||
shippingAddressRef.current = initialShippingAddress; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are doing this here if we have hasCustomerDataSynced
and the condition below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because initialBillingAddress
and initialShippingAddress
will update once the cart store has been hydrated. So we are storing a ref to it to reflect the change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the initial value passed to useRef
serve as that? I know that this line will execute each time the hook runs so are you updating the ref each time?
billingDataRef.current = initialBillingAddress;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing well! I'd be honest that the implementation is going a bit over my head as I was struggling to follow all the shouldUpdates
but as long as the issue is fixed I'm okay with it right now.
The shouldUpdateBillingAddress and shouldUpdateShippingAddress are just comparing when the 2 addresses have changed separately, so that we know which ones to send to the API request. It's a bunch of logic to work out weather to send either one of them or both based on what's actually changed. |
…ing address (#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]>
* Empty commit for release pull request * Fix state validation to compare state codes, and only validate if a country is given (#5132) * Only get valid states from wc if there is a country * Shared validation logic which uses keys * Fix 'Country is required' error on the Cart block when updating shipping address (#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]> * Make order note block removable (#5139) * unforce order note * show block in inserter * Mini Cart block fatal error caused by append_script_and_deps_src (#5142) * Remove unused use statements * Fix types for $script * `get_script_from_handle` might return null so handle this case to prevent errors * Missing escaping * Replace do_action_deprecated with the WC equivalent (#5151) * Remove feature gate condition for Legacy Template block (#5158) * Update readme.txt * Update testing instructions * Bumping version strings to new version. Co-authored-by: github-actions <[email protected]> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Niels Lange <[email protected]>
* Empty commit for release pull request * Update readme.txt with release changelog * Add testing instructions for 6.3.0 release * Update testing instructions as MD tables didn't work * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update docs/testing/releases/630.md Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update readme.txt * Update release zip in testing note * Update testing instructions * Remove feature gate condition for Legacy Template block (#5158) * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt * Update testing instructions * Update testing instructions * Update release zip in testing note * Update testing instructions for SE templates * Update testing instructions for SE templates II * Update testing instructions * Bumping version strings to new version. * Release: 6.3.1 (#5169) * Empty commit for release pull request * Fix state validation to compare state codes, and only validate if a country is given (#5132) * Only get valid states from wc if there is a country * Shared validation logic which uses keys * Fix 'Country is required' error on the Cart block when updating shipping address (#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]> * Make order note block removable (#5139) * unforce order note * show block in inserter * Mini Cart block fatal error caused by append_script_and_deps_src (#5142) * Remove unused use statements * Fix types for $script * `get_script_from_handle` might return null so handle this case to prevent errors * Missing escaping * Replace do_action_deprecated with the WC equivalent (#5151) * Remove feature gate condition for Legacy Template block (#5158) * Update readme.txt * Update testing instructions * Bumping version strings to new version. Co-authored-by: github-actions <[email protected]> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Michael P. Pfeiffer <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]>
* Empty commit for release pull request * Update readme.txt with release changelog * Add testing instructions for 6.3.0 release * Update testing instructions as MD tables didn't work * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update docs/testing/releases/630.md Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update readme.txt * Update release zip in testing note * Update testing instructions * Remove feature gate condition for Legacy Template block (#5158) * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt * Update testing instructions * Update testing instructions * Update release zip in testing note * Update testing instructions for SE templates * Update testing instructions for SE templates II * Update testing instructions * Bumping version strings to new version. * Empty commit for release pull request * Fix state validation to compare state codes, and only validate if a country is given (#5132) * Only get valid states from wc if there is a country * Shared validation logic which uses keys * Fix 'Country is required' error on the Cart block when updating shipping address (#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]> * Make order note block removable (#5139) * unforce order note * show block in inserter * Mini Cart block fatal error caused by append_script_and_deps_src (#5142) * Remove unused use statements * Fix types for $script * `get_script_from_handle` might return null so handle this case to prevent errors * Missing escaping * Replace do_action_deprecated with the WC equivalent (#5151) * Remove feature gate condition for Legacy Template block (#5158) * Update readme.txt * Update testing instructions * Bumping version strings to new version. * Empty commit for release pull request * Update readme.txt * Add testing instructions * Add testing notes to README.md * Hide WooCommmerce block templates from Edit Post template dropdown (#5167) * Hide WooCommmerce block templates from Edit Post template dropdown * Add empty post_types value to template and update comments. * Legacy Template Block: Remove the deletion lock (#5166) Previously, our legacy template block was locked to prevent the users from removing it. The block could however be removed by erasing the parent, so this change allows the user to directly remove the block itself. Also: Closes #5163 Closes #5109 * Update release zip * Update release zip * Update testing instructions * Bumping version strings to new version. Co-authored-by: github-actions <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Michael P. Pfeiffer <[email protected]> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Tom Cafferkey <[email protected]> Co-authored-by: Lucio Giannotta <[email protected]>
…ing address (woocommerce#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]>
* Empty commit for release pull request * Update readme.txt with release changelog * Add testing instructions for 6.3.0 release * Update testing instructions as MD tables didn't work * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update docs/testing/releases/630.md Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update readme.txt * Update release zip in testing note * Update testing instructions * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt * Update testing instructions * Update testing instructions * Update release zip in testing note * Update testing instructions for SE templates * Update testing instructions for SE templates II * Update testing instructions * Bumping version strings to new version. * Release: 6.3.1 (woocommerce#5169) * Empty commit for release pull request * Fix state validation to compare state codes, and only validate if a country is given (woocommerce#5132) * Only get valid states from wc if there is a country * Shared validation logic which uses keys * Fix 'Country is required' error on the Cart block when updating shipping address (woocommerce#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]> * Make order note block removable (woocommerce#5139) * unforce order note * show block in inserter * Mini Cart block fatal error caused by append_script_and_deps_src (woocommerce#5142) * Remove unused use statements * Fix types for $script * `get_script_from_handle` might return null so handle this case to prevent errors * Missing escaping * Replace do_action_deprecated with the WC equivalent (woocommerce#5151) * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update readme.txt * Update testing instructions * Bumping version strings to new version. Co-authored-by: github-actions <[email protected]> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Michael P. Pfeiffer <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]>
* Empty commit for release pull request * Update readme.txt with release changelog * Add testing instructions for 6.3.0 release * Update testing instructions as MD tables didn't work * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update docs/testing/releases/630.md Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update readme.txt * Update release zip in testing note * Update testing instructions * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt * Update testing instructions * Update testing instructions * Update release zip in testing note * Update testing instructions for SE templates * Update testing instructions for SE templates II * Update testing instructions * Bumping version strings to new version. * Empty commit for release pull request * Fix state validation to compare state codes, and only validate if a country is given (woocommerce#5132) * Only get valid states from wc if there is a country * Shared validation logic which uses keys * Fix 'Country is required' error on the Cart block when updating shipping address (woocommerce#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]> * Make order note block removable (woocommerce#5139) * unforce order note * show block in inserter * Mini Cart block fatal error caused by append_script_and_deps_src (woocommerce#5142) * Remove unused use statements * Fix types for $script * `get_script_from_handle` might return null so handle this case to prevent errors * Missing escaping * Replace do_action_deprecated with the WC equivalent (woocommerce#5151) * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update readme.txt * Update testing instructions * Bumping version strings to new version. * Empty commit for release pull request * Update readme.txt * Add testing instructions * Add testing notes to README.md * Hide WooCommmerce block templates from Edit Post template dropdown (woocommerce#5167) * Hide WooCommmerce block templates from Edit Post template dropdown * Add empty post_types value to template and update comments. * Legacy Template Block: Remove the deletion lock (woocommerce#5166) Previously, our legacy template block was locked to prevent the users from removing it. The block could however be removed by erasing the parent, so this change allows the user to directly remove the block itself. Also: Closes woocommerce#5163 Closes woocommerce#5109 * Update release zip * Update release zip * Update testing instructions * Bumping version strings to new version. Co-authored-by: github-actions <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Michael P. Pfeiffer <[email protected]> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Tom Cafferkey <[email protected]> Co-authored-by: Lucio Giannotta <[email protected]>
…ing address (woocommerce#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]>
* Empty commit for release pull request * Update readme.txt with release changelog * Add testing instructions for 6.3.0 release * Update testing instructions as MD tables didn't work * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update docs/testing/releases/630.md Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update readme.txt * Update release zip in testing note * Update testing instructions * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt * Update testing instructions * Update testing instructions * Update release zip in testing note * Update testing instructions for SE templates * Update testing instructions for SE templates II * Update testing instructions * Bumping version strings to new version. * Release: 6.3.1 (woocommerce#5169) * Empty commit for release pull request * Fix state validation to compare state codes, and only validate if a country is given (woocommerce#5132) * Only get valid states from wc if there is a country * Shared validation logic which uses keys * Fix 'Country is required' error on the Cart block when updating shipping address (woocommerce#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]> * Make order note block removable (woocommerce#5139) * unforce order note * show block in inserter * Mini Cart block fatal error caused by append_script_and_deps_src (woocommerce#5142) * Remove unused use statements * Fix types for $script * `get_script_from_handle` might return null so handle this case to prevent errors * Missing escaping * Replace do_action_deprecated with the WC equivalent (woocommerce#5151) * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update readme.txt * Update testing instructions * Bumping version strings to new version. Co-authored-by: github-actions <[email protected]> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Michael P. Pfeiffer <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]>
* Empty commit for release pull request * Update readme.txt with release changelog * Add testing instructions for 6.3.0 release * Update testing instructions as MD tables didn't work * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update docs/testing/releases/630.md Co-authored-by: Albert Juhé Lluveras <[email protected]> * Update readme.txt * Update release zip in testing note * Update testing instructions * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update testing instructions * Update testing instructions * Update testing instructions * Update readme.txt * Update testing instructions * Update testing instructions * Update release zip in testing note * Update testing instructions for SE templates * Update testing instructions for SE templates II * Update testing instructions * Bumping version strings to new version. * Empty commit for release pull request * Fix state validation to compare state codes, and only validate if a country is given (woocommerce#5132) * Only get valid states from wc if there is a country * Shared validation logic which uses keys * Fix 'Country is required' error on the Cart block when updating shipping address (woocommerce#5129) * Fix error on the Cart block * Created a cartIsHydrated variable in useStoreCart hook and used this to update the billing address in the internal state of the useCustomerData hook * Fix the country is required error on the Cart page using refs * Separate api calls to update shipping and billingUpdate billing and shipping addresses only when needed in API calls * Remove redundant check for customerDataToUpdate * remove use of refs in initial values Co-authored-by: Nadir Seghir <[email protected]> * Make order note block removable (woocommerce#5139) * unforce order note * show block in inserter * Mini Cart block fatal error caused by append_script_and_deps_src (woocommerce#5142) * Remove unused use statements * Fix types for $script * `get_script_from_handle` might return null so handle this case to prevent errors * Missing escaping * Replace do_action_deprecated with the WC equivalent (woocommerce#5151) * Remove feature gate condition for Legacy Template block (woocommerce#5158) * Update readme.txt * Update testing instructions * Bumping version strings to new version. * Empty commit for release pull request * Update readme.txt * Add testing instructions * Add testing notes to README.md * Hide WooCommmerce block templates from Edit Post template dropdown (woocommerce#5167) * Hide WooCommmerce block templates from Edit Post template dropdown * Add empty post_types value to template and update comments. * Legacy Template Block: Remove the deletion lock (woocommerce#5166) Previously, our legacy template block was locked to prevent the users from removing it. The block could however be removed by erasing the parent, so this change allows the user to directly remove the block itself. Also: Closes woocommerce#5163 Closes woocommerce#5109 * Update release zip * Update release zip * Update testing instructions * Bumping version strings to new version. Co-authored-by: github-actions <[email protected]> Co-authored-by: Niels Lange <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]> Co-authored-by: Michael P. Pfeiffer <[email protected]> Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Alex Florisca <[email protected]> Co-authored-by: Nadir Seghir <[email protected]> Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Tom Cafferkey <[email protected]> Co-authored-by: Lucio Giannotta <[email protected]>
This fixes an error on the cart page that was causing a 'Country is required' error to show up once a customer updates their shipping address.
The main issue is that the default state of wc/store/cart has an empty billing address. This then gets hydrated by the client with JSON data that is injected onto the page by the API. This contains the country code set in WooCommerce settings. So initially, the billing address is empty, then gets populated with a country code. Between these two steps, the
use-customer-data hook takes the value from wc/store/cart and assigns it to a local state variable. This means this local state variable
customerData
is out of sync with the wc/store/cart value. When a user updates their shipping address, an empty billing address is sent through the API call, and the server returns a "Country is required" error.To solve this, we've used a couple of
Refs
to store the shipping and billing addresses, which will update their values when the cart store has hydrated. We then set the local state variablecustomerData
to the shipping and billing addresses from wc/store/cart whenever they change. This only needs to run once, as any subsequent time will be the result of this effect or client side changes.Fixes #5070
Other Checks
Screenshots
Testing
Automated Tests
Manual Testing
How to test the changes in this Pull Request:
User Facing Testing
These are steps for user testing (where "user" is someone interacting with this change that is not editing any code).
Changelog