Commit 4ed122dbd46 for woocommerce
commit 4ed122dbd46d7c3fbe069cf1797391f7d4c7e049
Author: Wesley Rosa <wesleyjrosa@gmail.com>
Date: Thu Oct 8 18:13:45 2026 -0300
[Mise en Place] Fix Cart block showing the editor preview cart on the storefront (#68754)
* Stop treating a registered core/editor store as proof of the editor
isEditor() returned true whenever the core/editor store existed, but any
plugin that enqueues wp-editor on the storefront registers that store. The
Cart, Checkout and Mini-Cart blocks then served the editor preview cart
(Beanie/Cap) instead of the shopper's cart. Require a post to actually be
loaded for editing (getCurrentPostId) instead, which is true in real and
custom block editors but false on the storefront. Add tests for the editor,
custom-editor, and storefront-with-wp-editor cases.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XjyG1m53SdYXJk3gpQUr5M
* Add changelog entry for cart preview leak fix
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XjyG1m53SdYXJk3gpQUr5M
* Apply updated prettier formatting to isEditor files
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1LKZWU7nPJV7j5eaNRxuu
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/fix-wooplug-7680-cart-preview-leaks-on-frontend b/plugins/woocommerce/changelog/fix-wooplug-7680-cart-preview-leaks-on-frontend
new file mode 100644
index 00000000000..8481642d960
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-wooplug-7680-cart-preview-leaks-on-frontend
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix the Cart, Checkout and Mini-Cart blocks showing the editor preview cart on the storefront when another plugin loads the block editor scripts.
diff --git a/plugins/woocommerce/client/blocks/changelog/fix-wooplug-7680-cart-preview-leaks-on-frontend b/plugins/woocommerce/client/blocks/changelog/fix-wooplug-7680-cart-preview-leaks-on-frontend
new file mode 100644
index 00000000000..8481642d960
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/changelog/fix-wooplug-7680-cart-preview-leaks-on-frontend
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix the Cart, Checkout and Mini-Cart blocks showing the editor preview cart on the storefront when another plugin loads the block editor scripts.
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/utils/is-editor.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/utils/is-editor.ts
index 80993187243..1221d56fb79 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/block-data/utils/is-editor.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/utils/is-editor.ts
@@ -6,12 +6,21 @@ import { select } from '@wordpress/data';
/**
* Returns true if the current page is in the editor.
+ *
+ * The core/editor store can be registered on the storefront when another plugin
+ * enqueues wp-editor, so its mere presence is not proof we are in the editor. A
+ * real (including custom) block editor always has a post loaded for editing,
+ * which the storefront never does, so fall back to checking for a current post.
*/
export const isEditor = (): boolean => {
- return (
- getPath( window.location.href )?.includes( 'site-editor.php' ) ||
- getPath( window.location.href )?.includes( 'post.php' ) ||
- !! select( 'core/editor' ) ||
- false
- );
+ const path = getPath( window.location.href );
+
+ if ( path?.includes( 'site-editor.php' ) || path?.includes( 'post.php' ) ) {
+ return true;
+ }
+
+ const editorSelectors = select( 'core/editor' ) as unknown as
+ { getCurrentPostId?: () => number | null | undefined } | undefined;
+
+ return !! editorSelectors?.getCurrentPostId?.();
};
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/utils/test/is-editor.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/utils/test/is-editor.ts
new file mode 100644
index 00000000000..cd06a94d289
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/utils/test/is-editor.ts
@@ -0,0 +1,57 @@
+/**
+ * External dependencies
+ */
+import { select } from '@wordpress/data';
+import { getPath } from '@wordpress/url';
+
+jest.mock( '@wordpress/data', () => ( { select: jest.fn() } ) );
+jest.mock( '@wordpress/url', () => ( { getPath: jest.fn() } ) );
+
+// tests/js/config/global-mocks.js mocks isEditor to always return false, so use
+// the real implementation here.
+const { isEditor } =
+ jest.requireActual< typeof import( '../is-editor' ) >( '../is-editor' );
+
+const mockGetPath = getPath as jest.Mock;
+const mockSelect = select as jest.Mock;
+
+describe( 'isEditor', () => {
+ beforeEach( () => {
+ jest.clearAllMocks();
+ mockGetPath.mockReturnValue( '/' );
+ mockSelect.mockReturnValue( undefined );
+ } );
+
+ it( 'returns true on the Site Editor screen', () => {
+ mockGetPath.mockReturnValue( '/wp-admin/site-editor.php' );
+
+ expect( isEditor() ).toBe( true );
+ } );
+
+ it( 'returns true on the post editor screen', () => {
+ mockGetPath.mockReturnValue( '/wp-admin/post.php' );
+
+ expect( isEditor() ).toBe( true );
+ } );
+
+ it( 'returns true in a block editor that has a post loaded for editing', () => {
+ mockGetPath.mockReturnValue( '/wp-admin/post-new.php' );
+ mockSelect.mockReturnValue( { getCurrentPostId: () => 42 } );
+
+ expect( isEditor() ).toBe( true );
+ } );
+
+ it( 'returns false on the storefront when a plugin registered core/editor without editing a post', () => {
+ mockGetPath.mockReturnValue( '/cart/' );
+ mockSelect.mockReturnValue( { getCurrentPostId: () => null } );
+
+ expect( isEditor() ).toBe( false );
+ } );
+
+ it( 'returns false on the storefront when core/editor is not registered', () => {
+ mockGetPath.mockReturnValue( '/cart/' );
+ mockSelect.mockReturnValue( undefined );
+
+ expect( isEditor() ).toBe( false );
+ } );
+} );