Commit 1b6d7670ede for woocommerce
commit 1b6d7670ede169b30d979b2e445e11077b4e4bbb
Author: ThelmaChido <41231764+Thelmachido@users.noreply.github.com>
Date: Tue Nov 24 16:02:18 2026 +0100
Fix Cart block showing only one notice when cart errors share a code (#71215)
Cart error notices used the error code as their ID, so errors that share a
code (every removed coupon, every wc_add_notice() error) replaced each other
in core/notices. Later errors with the same code but a different message now
get a numbered ID, and the same IDs are cleared when the errors go away.
diff --git a/plugins/woocommerce/changelog/70950-fix-cart-block-duplicate-error-notices b/plugins/woocommerce/changelog/70950-fix-cart-block-duplicate-error-notices
new file mode 100644
index 00000000000..70d09c93986
--- /dev/null
+++ b/plugins/woocommerce/changelog/70950-fix-cart-block-duplicate-error-notices
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Show every cart error in the Cart and Checkout blocks, not just one, when several errors share an error code.
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/notify-errors.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/notify-errors.ts
index 263a2efbcf2..7044603ed0d 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/notify-errors.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/notify-errors.ts
@@ -18,15 +18,54 @@ const filterValidErrors = ( errors: ApiErrorResponse[] ) => {
return errors.filter( isApiErrorResponse );
};
+type CartErrorNotice = {
+ message: string;
+ options: { id: string; context: string };
+};
+
+/**
+ * Gets a notice ID for each cart error. Errors often share a code (e.g. every wc_add_notice() error is
+ * `woocommerce_rest_cart_item_error`), and notices with the same ID replace each other. So the first
+ * error keeps the code as its ID, later ones with a different message get `code__2`, `code__3`, and
+ * exact repeats reuse the earlier ID.
+ */
+const getCartErrorNotices = (
+ errors: ApiErrorResponse[]
+): CartErrorNotice[] => {
+ const messagesByCode = new Map< string, string[] >();
+
+ return errors.map( ( error ) => {
+ const context = error?.data?.context || 'wc/cart';
+ const message = decodeEntities( error.message );
+ const key = `${ context }::${ error.code }`;
+ const messages = messagesByCode.get( key ) || [];
+
+ let index = messages.indexOf( message );
+ if ( index === -1 ) {
+ messages.push( message );
+ messagesByCode.set( key, messages );
+ index = messages.length - 1;
+ }
+
+ return {
+ message,
+ options: {
+ id:
+ index === 0
+ ? error.code
+ : `${ error.code }__${ index + 1 }`,
+ context,
+ },
+ };
+ } );
+};
+
/**
* This function is used to notify the user of errors/conflicts from an API error response object.
*/
const createNoticesFromErrors = ( errors: ApiErrorResponse[] ) => {
- errors.forEach( ( error ) => {
- createNotice( 'error', decodeEntities( error.message ), {
- id: error.code,
- context: error?.data?.context || 'wc/cart',
- } );
+ getCartErrorNotices( errors ).forEach( ( { message, options } ) => {
+ createNotice( 'error', message, options );
} );
};
@@ -53,6 +92,13 @@ export const updateCartErrorNotices = (
oldCartErrorContexts.forEach( ( e ) => {
dismissNoticeByOptions( e );
} );
+
+ // Also dismiss the numbered notices created for errors that share a code.
+ getCartErrorNotices( filterValidErrors( oldErrors ) ).forEach(
+ ( { options } ) => {
+ dismissNoticeByOptions( options );
+ }
+ );
}
if ( errors !== null ) {
createNoticesFromErrors( filterValidErrors( errors ) );
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/test/notify-errors.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/test/notify-errors.ts
new file mode 100644
index 00000000000..e72593d4e7a
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/test/notify-errors.ts
@@ -0,0 +1,110 @@
+/**
+ * External dependencies
+ */
+import { dispatch } from '@wordpress/data';
+import { createNotice } from '@woocommerce/base-utils';
+import { ApiErrorResponse } from '@woocommerce/types';
+
+/**
+ * Internal dependencies
+ */
+import { updateCartErrorNotices } from '../notify-errors';
+
+jest.mock( '@wordpress/data' );
+jest.mock( '@woocommerce/base-utils', () => ( {
+ ...jest.requireActual( '@woocommerce/base-utils' ),
+ createNotice: jest.fn(),
+} ) );
+
+const mockedRemoveNotice = jest.fn();
+( dispatch as jest.Mock ).mockImplementation( ( store ) => {
+ if ( store === 'core/notices' ) {
+ return { removeNotice: mockedRemoveNotice };
+ }
+} );
+
+const cartError = ( code: string, message: string ): ApiErrorResponse => ( {
+ code,
+ message,
+ data: { status: 400, params: {}, details: {} },
+} );
+
+describe( 'updateCartErrorNotices', () => {
+ afterEach( () => {
+ jest.clearAllMocks();
+ } );
+
+ it( 'creates a notice for each error that shares an error code', () => {
+ updateCartErrorNotices( [
+ cartError(
+ 'woocommerce_rest_cart_item_error',
+ 'First error message'
+ ),
+ cartError(
+ 'woocommerce_rest_cart_item_error',
+ 'Second error message'
+ ),
+ cartError( 'woocommerce_rest_cart_coupon_error', 'Coupon error' ),
+ ] );
+
+ expect( createNotice ).toHaveBeenCalledTimes( 3 );
+ expect( createNotice ).toHaveBeenNthCalledWith(
+ 1,
+ 'error',
+ 'First error message',
+ { id: 'woocommerce_rest_cart_item_error', context: 'wc/cart' }
+ );
+ expect( createNotice ).toHaveBeenNthCalledWith(
+ 2,
+ 'error',
+ 'Second error message',
+ { id: 'woocommerce_rest_cart_item_error__2', context: 'wc/cart' }
+ );
+ expect( createNotice ).toHaveBeenNthCalledWith(
+ 3,
+ 'error',
+ 'Coupon error',
+ { id: 'woocommerce_rest_cart_coupon_error', context: 'wc/cart' }
+ );
+ } );
+
+ it( 'reuses the notice ID for an exact repeat of an error', () => {
+ updateCartErrorNotices( [
+ cartError( 'woocommerce_rest_cart_item_error', 'Same message' ),
+ cartError( 'woocommerce_rest_cart_item_error', 'Same message' ),
+ ] );
+
+ const ids = ( createNotice as jest.Mock ).mock.calls.map(
+ ( call ) => call[ 2 ].id
+ );
+ expect( ids ).toEqual( [
+ 'woocommerce_rest_cart_item_error',
+ 'woocommerce_rest_cart_item_error',
+ ] );
+ } );
+
+ it( 'dismisses every notice created for the old errors', () => {
+ updateCartErrorNotices(
+ [],
+ [
+ cartError(
+ 'woocommerce_rest_cart_item_error',
+ 'First error message'
+ ),
+ cartError(
+ 'woocommerce_rest_cart_item_error',
+ 'Second error message'
+ ),
+ ]
+ );
+
+ expect( mockedRemoveNotice ).toHaveBeenCalledWith(
+ 'woocommerce_rest_cart_item_error',
+ 'wc/cart'
+ );
+ expect( mockedRemoveNotice ).toHaveBeenCalledWith(
+ 'woocommerce_rest_cart_item_error__2',
+ 'wc/cart'
+ );
+ } );
+} );