Commit 8b4407b0cf for woocommerce
commit 8b4407b0cf005abf205e3aa00f0597296f6610f2
Author: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com>
Date: Tue Feb 10 09:11:32 2026 +0100
Move `handleSubmit` inside `addToCart` in `add-to-cart-with-options` store (#63105)
* Move `handleSubmit` inside `addToCart`
* Wrapp `addToCart` with `withSyncEvent`
* Add changefile(s) from automation for the following project(s): woocommerce
* Fix unit tests
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/63105-wooplug-6240-unify-submit-actions-in-add-to-cart-with-options b/plugins/woocommerce/changelog/63105-wooplug-6240-unify-submit-actions-in-add-to-cart-with-options
new file mode 100644
index 0000000000..ad317187f9
--- /dev/null
+++ b/plugins/woocommerce/changelog/63105-wooplug-6240-unify-submit-actions-in-add-to-cart-with-options
@@ -0,0 +1,4 @@
+Significance: patch
+Type: tweak
+
+Move `handleSubmit` inside `addToCart` in `add-to-cart-with-options` store.
\ No newline at end of file
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/__tests__/frontend.test.ts b/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/__tests__/frontend.test.ts
index 15f6ba49af..15d4eef792 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/__tests__/frontend.test.ts
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/__tests__/frontend.test.ts
@@ -28,6 +28,7 @@ jest.mock(
}
return { state: {} };
} ),
+ withSyncEvent: jest.fn( ( fn ) => fn ),
} ),
{ virtual: true }
);
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/frontend.ts b/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/frontend.ts
index 06f7b2097f..be0567bc77 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/frontend.ts
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/frontend.ts
@@ -1,7 +1,12 @@
/**
* External dependencies
*/
-import { store, getContext, getConfig } from '@wordpress/interactivity';
+import {
+ store,
+ getContext,
+ getConfig,
+ withSyncEvent,
+} from '@wordpress/interactivity';
import type {
Store as WooCommerce,
SelectedAttributes,
@@ -167,8 +172,7 @@ export type AddToCartWithOptionsStore = {
setQuantity: ( productId: number, value: number ) => void;
addError: ( error: AddToCartError ) => string;
clearErrors: ( group?: string ) => void;
- addToCart: () => void;
- handleSubmit: ( event: SubmitEvent ) => void;
+ addToCart: ( event: SubmitEvent ) => void;
};
};
@@ -331,7 +335,46 @@ const { actions, state } = store<
validationErrors.length = 0;
}
},
- *addToCart() {
+ addToCart: withSyncEvent( function* ( event: SubmitEvent ) {
+ event.preventDefault();
+
+ const { isFormValid } = state;
+
+ if ( ! isFormValid ) {
+ // Dynamically import the store module first
+ yield import( '@woocommerce/stores/store-notices' );
+
+ const { actions: noticeActions } = store< StoreNotices >(
+ 'woocommerce/store-notices',
+ {},
+ {
+ lock: universalLock,
+ }
+ );
+
+ const { noticeIds, validationErrors } = state;
+
+ // Clear previous notices.
+ noticeIds.forEach( ( id ) => {
+ noticeActions.removeNotice( id );
+ } );
+ noticeIds.splice( 0, noticeIds.length );
+
+ // Add new notices and track their IDs.
+ const newNoticeIds = validationErrors.map( ( error ) =>
+ noticeActions.addNotice( {
+ notice: error.message,
+ type: 'error',
+ dismissible: true,
+ } )
+ );
+
+ // Store the new IDs in-place.
+ noticeIds.push( ...newNoticeIds );
+
+ return;
+ }
+
// Todo: Use the module exports instead of `store()` once the
// woocommerce store is public.
yield import( '@woocommerce/stores/woocommerce/cart' );
@@ -378,49 +421,7 @@ const { actions, state } = store<
showCartUpdatesNotices: false,
}
);
- },
- *handleSubmit( event: SubmitEvent ) {
- event.preventDefault();
-
- const { isFormValid } = state;
-
- if ( ! isFormValid ) {
- // Dynamically import the store module first
- yield import( '@woocommerce/stores/store-notices' );
-
- const { actions: noticeActions } = store< StoreNotices >(
- 'woocommerce/store-notices',
- {},
- {
- lock: universalLock,
- }
- );
-
- const { noticeIds, validationErrors } = state;
-
- // Clear previous notices.
- noticeIds.forEach( ( id ) => {
- noticeActions.removeNotice( id );
- } );
- noticeIds.splice( 0, noticeIds.length );
-
- // Add new notices and track their IDs.
- const newNoticeIds = validationErrors.map( ( error ) =>
- noticeActions.addNotice( {
- notice: error.message,
- type: 'error',
- dismissible: true,
- } )
- );
-
- // Store the new IDs in-place.
- noticeIds.push( ...newNoticeIds );
-
- return;
- }
-
- yield actions.addToCart();
- },
+ } ),
},
},
{ lock: universalLock }
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php
index e1eabf047f..a3217061ec 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php
@@ -539,7 +539,7 @@ class AddToCartWithOptions extends AbstractBlock {
} else {
// Otherwise, we use the Interactivity API.
$form_attributes = array(
- 'data-wp-on--submit' => 'actions.handleSubmit',
+ 'data-wp-on--submit' => 'actions.addToCart',
);
}