Commit fef74953ab4 for woocommerce

commit fef74953ab492f04d2fe789fc56977fd212f86c9
Author: Akanshu Singh <61490175+singhakanshu00@users.noreply.github.com>
Date:   Tue Nov 17 19:06:14 2026 +0530

    fix: Fixes Product Attribute Filter attribute ID persistence (#71032)

    * fix: Fixes Product Attribute Filter attribute ID persistence

    * Test the attribute filter through its real registration

    The deprecation tests registered block.json directly, so they still
    passed with the store-default override restored in index.tsx.

    Replace them with two tests that load index.tsx: one fails if the
    default attribute ID is dropped on save, one fails if saved blocks
    without an ID are no longer set to the store default.

    ---------

    Co-authored-by: Tung Du <tung.du@automattic.com>

diff --git a/plugins/woocommerce/changelog/70984-attribute-filter-persist-attribute-id b/plugins/woocommerce/changelog/70984-attribute-filter-persist-attribute-id
new file mode 100644
index 00000000000..e00c25886af
--- /dev/null
+++ b/plugins/woocommerce/changelog/70984-attribute-filter-persist-attribute-id
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Keep the Attribute Filter block on its chosen attribute when the store's default filter attribute changes.
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/edit.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/edit.tsx
index 06684b3fdf8..a001c9ed8ca 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/edit.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/edit.tsx
@@ -31,6 +31,9 @@ import { type BlockAttributes } from './types';
 import { getColorsFromBlockSupports } from './utils/get-colors-from-block-supports';
 import { getOverlayMode, isOverlayMode } from './utils/overlay-mode';
 import { presetToCssVariable } from './utils/preset-to-css-variable';
+import { getDefaultAttributeId } from './inner-blocks/attribute-filter/utils';
+
+const DEFAULT_ATTRIBUTE_ID = getDefaultAttributeId();

 const TEMPLATE: InnerBlockTemplate[] = [
 	[
@@ -47,7 +50,11 @@ const TEMPLATE: InnerBlockTemplate[] = [
 	[ 'woocommerce/product-filter-active' ],
 	[ 'woocommerce/product-filter-price' ],
 	[ 'woocommerce/product-filter-rating' ],
-	[ 'woocommerce/product-filter-attribute' ],
+	[
+		'woocommerce/product-filter-attribute',
+		// Persist the ID: a block attribute equal to its default isn't saved.
+		DEFAULT_ATTRIBUTE_ID > 0 ? { attributeId: DEFAULT_ATTRIBUTE_ID } : {},
+	],
 	[ 'woocommerce/product-filter-taxonomy' ],
 	[ 'woocommerce/product-filter-status' ],
 ];
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/deprecated.ts b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/deprecated.ts
new file mode 100644
index 00000000000..b58a063d2fa
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/deprecated.ts
@@ -0,0 +1,28 @@
+/**
+ * Internal dependencies
+ */
+import metadata from './block.json';
+import Save from './save';
+import { getDefaultAttributeId } from './utils';
+
+/**
+ * Blocks used to be registered with the store's default attribute as the
+ * `attributeId` default, so Gutenberg omitted the ID from the saved markup
+ * whenever it matched the default. Pin those blocks to the current default.
+ */
+const deprecated = [
+	{
+		apiVersion: metadata.apiVersion,
+		attributes: metadata.attributes,
+		supports: metadata.supports,
+		save: Save,
+		isEligible( attributes: { attributeId?: number } ) {
+			return ! attributes.attributeId && getDefaultAttributeId() > 0;
+		},
+		migrate( attributes: Record< string, unknown > ) {
+			return { ...attributes, attributeId: getDefaultAttributeId() };
+		},
+	},
+];
+
+export default deprecated;
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/index.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/index.tsx
index 0e9ecac03bc..588755a916f 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/index.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/index.tsx
@@ -11,25 +11,17 @@ import { __, sprintf } from '@wordpress/i18n';
  * Internal dependencies
  */
 import metadata from './block.json';
+import deprecated from './deprecated';
 import Edit from './edit';
 import Save from './save';

 const ATTRIBUTES = getSetting< AttributeSetting[] >( 'attributes', [] );
-const defaultAttribute = getSetting< AttributeSetting >(
-	'defaultProductFilterAttribute'
-);

 registerBlockType( metadata, {
 	edit: Edit,
 	icon: productFilterAttribute,
-	attributes: {
-		...metadata.attributes,
-		attributeId: {
-			...metadata.attributes.attributeId,
-			default: parseInt( defaultAttribute.attribute_id, 10 ),
-		},
-	},
 	save: Save,
+	deprecated,
 	variations: ATTRIBUTES.map( ( attribute, index ) => {
 		return {
 			name: `product-filter-attribute-${ attribute.attribute_name }`,
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/test/index.ts b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/test/index.ts
new file mode 100644
index 00000000000..dc57a164b9b
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/test/index.ts
@@ -0,0 +1,40 @@
+/**
+ * External dependencies
+ */
+import { createBlock, parse, serialize } from '@wordpress/blocks';
+
+/**
+ * Internal dependencies
+ */
+import '../index';
+
+jest.mock( '@woocommerce/settings', () => ( {
+	...jest.requireActual( '@woocommerce/settings' ),
+	getSetting: jest.fn( ( key: string, defaultValue: unknown ) =>
+		key === 'defaultProductFilterAttribute'
+			? { attribute_id: '5' }
+			: defaultValue
+	),
+} ) );
+
+const BLOCK = 'woocommerce/product-filter-attribute';
+
+describe( 'Attribute filter block', () => {
+	it( 'saves the attribute ID when it is the store default', () => {
+		const block = createBlock( BLOCK, { attributeId: 5 } );
+
+		expect( serialize( [ block ] ) ).toContain( '"attributeId":5' );
+	} );
+
+	it( 'sets the store default on saved blocks that have no attribute ID', () => {
+		const [ block ] = parse(
+			`<!-- wp:${ BLOCK } {"showCounts":true} -->\n<div class="wp-block-woocommerce-product-filter-attribute"></div>\n<!-- /wp:${ BLOCK } -->`
+		);
+
+		expect( block.isValid ).toBe( true );
+		expect( block.attributes ).toMatchObject( {
+			attributeId: 5,
+			showCounts: true,
+		} );
+	} );
+} );
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/utils.ts b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/utils.ts
index 785a7dc7eef..4f3f62b1c09 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/utils.ts
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/utils.ts
@@ -57,3 +57,14 @@ export function getAttributeFromTaxonomy( taxonomy: string ) {
 		return attribute.taxonomy === taxonomy;
 	} );
 }
+
+/**
+ * Get the ID of the store's current default filter attribute, or 0 if none.
+ */
+export function getDefaultAttributeId(): number {
+	const defaultAttribute = getSetting< AttributeSetting | undefined >(
+		'defaultProductFilterAttribute',
+		undefined
+	);
+	return parseInt( defaultAttribute?.attribute_id ?? '0', 10 ) || 0;
+}