woocommerce / woocommerce-blocks

(Deprecated) This plugin has been merged into woocommerce/woocommerce
https://wordpress.org/plugins/woo-gutenberg-products-block/
GNU General Public License v3.0
405 stars 218 forks source link

StoreApi `/wc/store/v1/cart/extensions` endpoint resets the state to store default #6423

Closed alexflorisca closed 2 years ago

alexflorisca commented 2 years ago

Describe the bug

When an extension registers a callback function with woocommerce_store_api_register_update_callback, the billingAddress.state value of the cart data is changed to match the default country set for the WooCommerce store, when the client side State/County is empty. For example, when selecting Portugal or Germany as the country on the Checkout page, the "State" input is hidden (and set as an empty string in the cart state).

When a call is made to /wc/store/v1/cart/extensions from the client (usually via the extensionCartUpdate() function) and the "State" input is empty, the response will change the billingAddress.state to a country code (which I think is the default country code the store is set up with in Woocommerce settings.

Ok that's a mouthful, here's a video:

https://user-images.githubusercontent.com/3966773/168869098-402dfd72-bea4-42fe-b5cb-220d2a575c6b.mov

I've tested this with two separate plugins newsletter-test and woocommerce-eu-vat-number and empty callback functions to make sure it's nothing to do with the extension.

This is causing a bug in woocommerce-eu-vat-number where we are generating two batch requests per update. The first comes back with a different state, so the client updates it's state and persists to the server with another update-customer request. The buck stops here with this plugin, but I can see this getting into an infinite loop pretty easily so we should probably fix this sooner rather than later

To reproduce

Steps to reproduce the behavior:

  1. Clone the Newsletter Test plugin locally
  2. Checkout the try/replicate-store-api-bug branch
  3. composer install && nvm use 12 && npm i && npm start
  4. Enable the plugin on your local WP instance
  5. Go to WooCommerce > Settings and set the Country/State as "Germany - Berlin"
  6. Visit the Checkout page
  7. Open the Network tab
  8. Select United Kingdom as the country
  9. Make sure the "County" input is empty
  10. Click on the checkbox that is displayed underneath the email address field as part of the newsletter test plugin
  11. Expect a batch network request. The request response body should contain billing_address.state: "DE-BE"
  12. The "County" input on the checkout form should now be "DE-BE" instead of empty

Expected behaviour

I would expect the billing_address.state not to change on the server

Environment

WordPress (please complete the following information):

alexflorisca commented 2 years ago

This turned out to be a different bug which was detailed and fixed here