Recently we became aware of an issue when trying to read the response
value in the client-side callback of extensionCartUpdate
.
The details
When writing a callback for extensionCartUpdate
it would typically look like this:
extensionCartUpdate( {
namespace: 'extension-unique-namespace',
data: {
key: 'value',
another_key: 100,
third_key: {
fourth_key: true,
},
},
} ).then( () => {
// Cart has been updated.
} ).catch( ( error ) => {
// Handle error.
processErrorResponse(error);
} );
However in some cases, developers may want to access the response
of the extensionCartUpdate
API call which would be done like this:
extensionCartUpdate( {
namespace: 'extension-unique-namespace',
data: {
key: 'value',
another_key: 100,
third_key: {
fourth_key: true,
},
},
} ).then( ( response ) => {
// You should be able to access `response` here.
} ).catch( ( error ) => {
// Handle error.
processErrorResponse(error);
} );
Typically developers shouldn’t need to worry about the value of response
but in some cases it could be useful.
How can I tell if this affects me?
If you’re a developer and you are using extensionCartUpdate
check your code to see if you’re using the response
value. You may notice this if you see errors in your console such as “Uncaught ReferenceError: response is not defined”
What action should I take?
Generally, using response
isn’t required so you may want to take this opportunity to refactor your extension’s logic to instead read from the wc/store/cart
data store to do work when that updates, rather than during the extensionCartUpdate
callback.
If you can’t do this, then passing overwriteDirtyCustomerData: true
as an option to extensionCartUpdate
like so will fix this.
extensionCartUpdate( {
namespace: 'extension-unique-namespace',
overwriteDirtyCustomerData: true,
data: {},
} ).then( ( response ) => {
// Response will be available.
} ).catch( ( error ) => {
// Handle error.
processErrorResponse(error);
} );
Adding this will not change the behaviour of extensionCartUpdate
.
Leave a Reply