Commit 39962c4b941 for woocommerce

commit 39962c4b9417c0d25f75004d5b080d208278b643
Author: Tung Du <dinhtungdu@gmail.com>
Date:   Wed Nov 25 00:21:39 2026 +0700

    Move All Products defaults into its edit so moved blocks keep their styles (#71358)

    * Preserve block attributes when moving multiple blocks

    * Add changelog entry for block attribute preservation

    * Refactor withDefaultAttributes to compute missing defaults once

    Extract a getMissingDefaults() helper and route both componentDidMount
    and getAttributesWithDefaults() through it, instead of building the full
    merged attribute object and diffing it back to recover the missing keys.

    This keeps the missing-key logic in one place and restores the
    guard against an undefined attributes prop: componentDidMount no longer
    calls Object.entries() or indexes attributes directly on a value that
    can be undefined for a woocommerce/ block without defaults.

    Behavior is unchanged: explicit falsy values are preserved, only
    genuinely-missing defaults are written to the owning block via its
    clientId, and the render-time merged view is the same. All 10 jest
    cases in filters/test/block-list-block.js pass.

    * Move All Products defaults into its edit and drop the editor filter

    The editor.BlockListBlock filter wrapped every block in the editor only to
    fill in defaults for All Products, the one block that registers them. Fill
    them in from All Products' own edit instead, written only to that block, and
    remove the filter.

    Amp-Thread-ID: https://ampcode.com/threads/T-52117521-3036-4ced-bfa8-b09db9835c00
    Co-authored-by: Amp <amp@ampcode.com>

    * Update changelog entry to cover all block settings and undo

    Amp-Thread-ID: https://ampcode.com/threads/T-52117521-3036-4ced-bfa8-b09db9835c00
    Co-authored-by: Amp <amp@ampcode.com>

    * Shorten changelog entry to block settings

    Amp-Thread-ID: https://ampcode.com/threads/T-52117521-3036-4ced-bfa8-b09db9835c00
    Co-authored-by: Amp <amp@ampcode.com>

    * Remove the get-block-attributes filter

    All Products now fills in its own missing defaults from its edit, so the
    blocks.getBlockAttributes filter has nothing left to do. It was also the
    only reader of the block's `defaults` setting, so stop registering it.

    Amp-Thread-ID: https://ampcode.com/threads/T-52117521-3036-4ced-bfa8-b09db9835c00
    Co-authored-by: Amp <amp@ampcode.com>

    ---------

    Co-authored-by: Bhavik Tank <bhavik.kalpesh@rtcamp.com>
    Co-authored-by: Amp <amp@ampcode.com>

diff --git a/plugins/woocommerce/changelog/fix-66996-preserve-product-block-attributes b/plugins/woocommerce/changelog/fix-66996-preserve-product-block-attributes
new file mode 100644
index 00000000000..17ac2a54c02
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-66996-preserve-product-block-attributes
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Keep each product block's own settings when moving several blocks at once in the editor, and undo the move in one step.
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/edit.js b/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/edit.js
index f97c5c15e3e..e8bb662ec3e 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/edit.js
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/edit.js
@@ -5,6 +5,7 @@ import {
 	BlockControls,
 	InnerBlocks,
 	InspectorControls,
+	store as blockEditorStore,
 	useBlockProps,
 } from '@wordpress/block-editor';
 import { createBlock } from '@wordpress/blocks';
@@ -18,8 +19,8 @@ import {
 	withSpokenMessages,
 } from '@wordpress/components';
 import { compose } from '@wordpress/compose';
-import { withDispatch, withSelect } from '@wordpress/data';
-import { useState } from '@wordpress/element';
+import { useDispatch, withDispatch, withSelect } from '@wordpress/data';
+import { useEffect, useState } from '@wordpress/element';
 import { __ } from '@wordpress/i18n';
 import { Icon, grid } from '@wordpress/icons';
 import { getBlockMap } from '@woocommerce/utils/get-block-map';
@@ -47,19 +48,50 @@ import {
 import { getBlockClassName } from '../utils';
 import Block from './block';
 import metadata from './block.json';
+import defaults from './defaults';
 import './editor.scss';

 const blockMap = getBlockMap( 'woocommerce/all-products' );

 const blockIcon = <Icon icon={ grid } />;

+/**
+ * Returns the default settings this block has not saved yet.
+ *
+ * The defaults depend on the theme, so they can't live in block.json.
+ * save() writes every attribute to the page, so they have to be saved.
+ *
+ * @param {Object} attributes Block attributes.
+ * @return {Object} Defaults for the attributes that are not set.
+ */
+const getMissingDefaults = ( attributes ) =>
+	Object.fromEntries(
+		Object.entries( defaults ).filter(
+			( [ key ] ) => typeof attributes[ key ] === 'undefined'
+		)
+	);
+
 const Edit = ( {
 	block,
-	attributes,
+	clientId,
+	attributes: savedAttributes,
 	setAttributes,
 	debouncedSpeak,
 	replaceInnerBlocks,
 } ) => {
+	const missingDefaults = getMissingDefaults( savedAttributes );
+	const attributes = { ...savedAttributes, ...missingDefaults };
+	const { updateBlockAttributes } = useDispatch( blockEditorStore );
+
+	useEffect( () => {
+		if ( Object.keys( missingDefaults ).length ) {
+			// Not setAttributes(): it writes to every selected block.
+			updateBlockAttributes( clientId, missingDefaults );
+		}
+		// Only on mount, like the editor filter this replaces.
+		// eslint-disable-next-line react-hooks/exhaustive-deps
+	}, [] );
+
 	const [ isEditing, setIsEditing ] = useState( false );
 	const [ innerBlocks, setInnerBlocks ] = useState( [] );

diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/index.js b/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/index.js
index 3d1e8296bfa..8ad42dd2ab5 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/index.js
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/products/all-products/index.js
@@ -12,7 +12,6 @@ import metadata from './block.json';
 import deprecated from './deprecated';
 import edit from './edit';
 import save from './save';
-import defaults from './defaults';

 const { name } = metadata;
 export { metadata, name };
@@ -30,7 +29,6 @@ const settings = {
 	// Save the props to post content.
 	save,
 	deprecated,
-	defaults,
 };

 registerBlockType( name, settings );
diff --git a/plugins/woocommerce/client/blocks/assets/js/filters/block-list-block.js b/plugins/woocommerce/client/blocks/assets/js/filters/block-list-block.js
deleted file mode 100644
index 2c125dc8eb2..00000000000
--- a/plugins/woocommerce/client/blocks/assets/js/filters/block-list-block.js
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * External dependencies
- */
-import { Component } from '@wordpress/element';
-import { createHigherOrderComponent } from '@wordpress/compose';
-import { getBlockType } from '@wordpress/blocks';
-import { addFilter } from '@wordpress/hooks';
-
-/**
- * withDefaultAttributes HOC for editor.BlockListBlock.
- *
- * @param object BlockListBlock The BlockListBlock element.
- */
-const withDefaultAttributes = createHigherOrderComponent(
-	( BlockListBlock ) => {
-		class WrappedComponent extends Component {
-			mounted = false;
-
-			componentDidMount() {
-				const { block, setAttributes } = this.props;
-
-				if ( block.name.startsWith( 'woocommerce/' ) ) {
-					setAttributes( this.getAttributesWithDefaults() );
-				}
-			}
-
-			componentDidUpdate() {
-				if (
-					this.props.block.name.startsWith( 'woocommerce/' ) &&
-					! this.mounted
-				) {
-					this.mounted = true;
-				}
-			}
-
-			getAttributesWithDefaults() {
-				const blockType = getBlockType( this.props.block.name );
-				let attributes = this.props.attributes;
-
-				if (
-					! this.mounted &&
-					this.props.block.name.startsWith( 'woocommerce/' ) &&
-					typeof blockType.attributes !== 'undefined' &&
-					typeof blockType.defaults !== 'undefined'
-				) {
-					attributes = Object.assign(
-						{},
-						this.props.attributes || {}
-					);
-					Object.keys( blockType.attributes ).map( ( key ) => {
-						if (
-							typeof attributes[ key ] === 'undefined' &&
-							typeof blockType.defaults[ key ] !== 'undefined'
-						) {
-							attributes[ key ] = blockType.defaults[ key ];
-						}
-						return key;
-					} );
-				}
-				return attributes;
-			}
-
-			render() {
-				return (
-					<BlockListBlock
-						{ ...this.props }
-						attributes={ this.getAttributesWithDefaults() }
-					/>
-				);
-			}
-		}
-		return WrappedComponent;
-	},
-	'withDefaultAttributes'
-);
-
-/**
- * Hook into `editor.BlockListBlock` to set default attributes (if blocks
- * define them separately) when a block is inserted.
- *
- * This is a workaround for Gutenberg which does not save "default" attributes
- * to the post, which means if defaults change, all existing blocks change too.
- *
- * See https://github.com/WordPress/gutenberg/issues/7342
- *
- * To use this, the block name needs a `woocommerce/` prefix, and as well
- * as defining `attributes` during block registration, you must also declare an
- * array called `defaults`. Defaults should be omitted from `attributes`.
- */
-addFilter(
-	'editor.BlockListBlock',
-	'woocommerce-blocks/block-list-block',
-	withDefaultAttributes
-);
diff --git a/plugins/woocommerce/client/blocks/assets/js/filters/get-block-attributes.js b/plugins/woocommerce/client/blocks/assets/js/filters/get-block-attributes.js
deleted file mode 100644
index 159296236c9..00000000000
--- a/plugins/woocommerce/client/blocks/assets/js/filters/get-block-attributes.js
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- * External dependencies
- */
-import { addFilter } from '@wordpress/hooks';
-
-/**
- * Adjust attributes on load to set defaults so default attributes get saved.
- *
- * @param {Object} blockAttributes Original block attributes.
- * @param {Object} blockType       Block type settings.
- *
- * @return {Object} Filtered block attributes.
- */
-const setBlockAttributeDefaults = ( blockAttributes, blockType ) => {
-	if ( blockType.name.startsWith( 'woocommerce/' ) ) {
-		Object.keys( blockType.attributes ).map( ( key ) => {
-			if (
-				typeof blockAttributes[ key ] === 'undefined' &&
-				typeof blockType.defaults !== 'undefined' &&
-				typeof blockType.defaults[ key ] !== 'undefined'
-			) {
-				blockAttributes[ key ] = blockType.defaults[ key ];
-			}
-			return key;
-		} );
-	}
-	return blockAttributes;
-};
-
-/**
- * Hook into `blocks.getBlockAttributes` to set default attributes (if blocks
- * define them separately) when a block is loaded.
- *
- * This is a workaround for Gutenberg which does not save "default" attributes
- * to the post, which means if defaults change, all existing blocks change too.
- *
- * See https://github.com/WordPress/gutenberg/issues/7342
- *
- * To use this, the block name needs a `woocommerce/` prefix, and as well
- * as defining `attributes` during block registration, you must also declare an
- * array called `defaults`. Defaults should be omitted from `attributes`.
- */
-addFilter(
-	'blocks.getBlockAttributes',
-	'woocommerce-blocks/get-block-attributes',
-	setBlockAttributeDefaults
-);
diff --git a/plugins/woocommerce/client/blocks/assets/js/index.js b/plugins/woocommerce/client/blocks/assets/js/index.js
index 9aeed7decf8..9954f40ae5e 100644
--- a/plugins/woocommerce/client/blocks/assets/js/index.js
+++ b/plugins/woocommerce/client/blocks/assets/js/index.js
@@ -3,8 +3,6 @@
  */
 import '../css/editor.scss';
 import '../css/style.scss';
-import './filters/block-list-block';
-import './filters/get-block-attributes';
 import './filters/hide-incompatible-template-parts';
 import './filters/unregister-block-types';
 import './plugins/shop-page-rendering-mode';