Commit 3d61d713f8b for woocommerce

commit 3d61d713f8b6bf1865164793b15f95ed39deae14
Author: Taha Paksu <3295+tpaksu@users.noreply.github.com>
Date:   Mon Nov 16 11:20:45 2026 +0300

    Keep a failed shipping zone save retryable without reapplying stale edits (#70538)

    * [WOOAIRR-673] Keep a failed shipping zone save retryable without reapplying stale edits

    * Add changefile(s) from automation for the following project(s): woocommerce

    * [WOOAIRR-673] Stop Save changes offering a retry over an empty change set

    * [WOOAIRR-673] Match the Free shipping method label exactly in the e2e test

    * [WOOAIRR-673] Carry unsaved method edits along when a method is added

    * Add changefile(s) from automation for the following project(s): woocommerce

    * [WOOAIRR-673] Clear the unload warning after a failed settings save

    ---------

    Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/70538-fix-WOOAIRR-673-method-only-save-retry b/plugins/woocommerce/changelog/70538-fix-WOOAIRR-673-method-only-save-retry
new file mode 100644
index 00000000000..f1fa171aa93
--- /dev/null
+++ b/plugins/woocommerce/changelog/70538-fix-WOOAIRR-673-method-only-save-retry
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Let merchants retry a failed shipping zone save, and keep an unsaved method toggle pending when a method is added.
diff --git a/plugins/woocommerce/client/legacy/js/admin/wc-shipping-zone-methods.js b/plugins/woocommerce/client/legacy/js/admin/wc-shipping-zone-methods.js
index 8c250ec8a7d..1675bcace9a 100644
--- a/plugins/woocommerce/client/legacy/js/admin/wc-shipping-zone-methods.js
+++ b/plugins/woocommerce/client/legacy/js/admin/wc-shipping-zone-methods.js
@@ -73,14 +73,20 @@
 					// Nothing was saved, so redraw from the model, which releases the table.
 					shippingMethod.trigger( 'rerender' );

-					// A row delete leaves a methods entry behind while marking the form clean, so
-					// ignore methods here exactly as the response does.
-					if ( _.isEmpty( _.omit( shippingMethod.changes, 'methods' ) ) ) {
-						// Saving an empty change set would just fail again, so clear the busy
-						// state without marking the form dirty.
-						$save_button.removeClass( 'is-busy' );
-					} else {
+					// A delete leaves a methods entry on a clean form and a toggle leaves the same
+					// entry on a dirty one, so take either signal, but never an empty change set.
+					var retryable = ! _.isEmpty( shippingMethod.changes ) && (
+						! _.isEmpty( _.omit( shippingMethod.changes, 'methods' ) ) ||
+						shippingMethodView.needsUnloadConfirm
+					);
+
+					if ( retryable ) {
 						shippingMethod.trigger( 'change:methods' );
+					} else {
+						// Saving an empty change set would just fail again, and nothing is left to
+						// lose, so clear the busy state and the unload warning.
+						$save_button.removeClass( 'is-busy' );
+						shippingMethodView.clearUnloadConfirmation();
 					}
 				},
 				onSaveResponse: function( response, textStatus, snapshot ) {
@@ -697,8 +703,24 @@
 							// Avoid triggering a rerender here because we don't want to show the method
 							// in the table in case merchant doesn't finish flow.

+							// The server list replaces the rows, so lay the unsaved toggles and order back over it;
+							// they stay pending and are saved with the next save.
+							_.each( _.omit( shippingMethodView.model.changes.methods, 'methods' ), function( row, id ) {
+								if ( response.data.methods[ id ] ) {
+									_.extend( response.data.methods[ id ], _.pick( row, 'enabled', 'method_order' ) );
+								}
+							} );
+
 							shippingMethodView.model.set( 'methods', response.data.methods );

+							// Setting 'methods' above marks the form dirty, which on the modal branch below
+							// still has to warn about navigating away from unfinished settings, so only
+							// the button is disabled here: an empty change set posts nothing and the
+							// endpoint rejects it. A completed save clears the flag via 'saved:methods'.
+							if ( _.isEmpty( shippingMethodView.model.changes ) ) {
+								$save_button.attr( 'disabled', 'disabled' );
+							}
+
 							// Close original modal
 							closeModal();

@@ -843,7 +865,6 @@
 							var view    = shippingMethodView,
 								model   = view.model,
 								methods   = _.indexBy( model.get( 'methods' ), 'instance_id' ),
-								changes = {},
 								instance_id = post_data.instance_id;

 							// Remove method to zone via ajax call
@@ -854,12 +875,22 @@
 									instance_id: instance_id,
 									zone_id: data.zone_id,
 								},
-								success: function( { data } ) {
+								success: function() {
 									delete methods[instance_id];
-									changes.methods = changes.methods || data.methods;
 									model.set('methods', methods);
-									model.logChanges( changes );
-									view.clearUnloadConfirmation();
+
+									// Keep the merchant's pending edits: writing the server's rows over them
+									// would revert a toggle the table still shows.
+									if ( model.changes.methods ) {
+										delete model.changes.methods[ instance_id ];
+										if ( _.isEmpty( _.omit( model.changes.methods, 'methods' ) ) ) {
+											delete model.changes.methods;
+										}
+									}
+
+									if ( _.isEmpty( model.changes ) ) {
+										view.clearUnloadConfirmation();
+									}
 									view.render();
 									shippingMethodView.unblock();
 								},
diff --git a/plugins/woocommerce/tests/e2e/tests/shipping/shipping-zones.spec.ts b/plugins/woocommerce/tests/e2e/tests/shipping/shipping-zones.spec.ts
index 20512d5f5e1..1042d9916f4 100644
--- a/plugins/woocommerce/tests/e2e/tests/shipping/shipping-zones.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/shipping/shipping-zones.spec.ts
@@ -442,6 +442,288 @@ test( 'surfaces a failed zone save and releases the methods table', async ( {
 	expect( dialogMessages ).toHaveLength( 1 );
 } );

+// Regression: a failed save left Save changes disabled when the only pending change was a
+// method toggle or reorder, which both log a methods-only change set, so no retry was possible.
+test( 'retries a failed save whose only pending change is a method toggle', async ( {
+	page,
+	restApi,
+	zone,
+} ) => {
+	await openZone( page, zone.name );
+
+	const dialogMessages: string[] = [];
+	page.on( 'dialog', async ( dialog ) => {
+		dialogMessages.push( dialog.message() );
+		await dialog.accept();
+	} );
+
+	await page.route( isSaveChanges, ( route ) =>
+		route.fulfill( {
+			status: 500,
+			contentType: 'text/plain',
+			body: 'Injected save failure',
+		} )
+	);
+
+	const toggle = page.locator( '.wc-shipping-zone-method-enabled a' ).first();
+
+	await expect( toggle ).toHaveText( 'Yes' );
+	await toggle.click();
+	await expect(
+		page.locator( '.wc-shipping-zone-method-enabled a' ).first()
+	).toHaveText( 'No' );
+	await expect( page.locator( '#submit' ) ).toBeEnabled();
+
+	await page.locator( '#submit' ).click();
+
+	await expect
+		.poll( () => dialogMessages )
+		.toContain( 'Your changes were not saved. Please retry.' );
+
+	// The toggle is still pending and the page still warns about it on unload, so the button
+	// has to stay clickable for the retry the alert asks for.
+	await expect( page.locator( '.blockUI' ) ).toHaveCount( 0 );
+	await expect( page.locator( '#submit' ) ).toBeEnabled();
+	await expect( page.locator( '#submit' ) ).not.toHaveClass( /is-busy/ );
+	await expect(
+		page.locator( '.wc-shipping-zone-method-enabled a' ).first()
+	).toHaveText( 'No' );
+
+	// A clickable button is only worth having if the retry it allows actually saves, so let
+	// the next request through and check the toggle reaches the database.
+	await page.unroute( isSaveChanges );
+	const retry = page.waitForResponse( ( response ) =>
+		isSaveChanges( new URL( response.url() ) )
+	);
+	await page.locator( '#submit' ).click();
+	expect( ( await ( await retry ).json() ).success ).toBe( true );
+
+	await expect( page.locator( '#submit' ) ).toBeDisabled();
+	expect( dialogMessages ).toHaveLength( 1 );
+
+	const methods = await restApi.get(
+		`${ WC_API_PATH }/shipping/zones/${ zone.id }/methods`
+	);
+	expect( methods.data[ 0 ].enabled ).toBe( false );
+} );
+
+// Adding a method replaces the table from the server, so the unsaved toggle has to be laid back
+// over that list and saved with the add, as pending zone edits are.
+test( 'saves an unsaved method toggle along with a method that is added', async ( {
+	page,
+	restApi,
+	zone,
+} ) => {
+	await openZone( page, zone.name );
+
+	const dialogMessages: string[] = [];
+	page.on( 'dialog', async ( dialog ) => {
+		dialogMessages.push( dialog.message() );
+		await dialog.accept();
+	} );
+
+	const toggle = page.locator( '.wc-shipping-zone-method-enabled a' ).first();
+
+	await expect( toggle ).toHaveText( 'Yes' );
+	await toggle.click();
+	await expect(
+		page.locator( '.wc-shipping-zone-method-enabled a' ).first()
+	).toHaveText( 'No' );
+
+	await openAddMethodModal( page );
+	await page.getByText( 'E2E Non-Modal Method' ).click();
+	await page.getByRole( 'button', { name: 'Continue' } ).click();
+
+	await expect(
+		page.locator( '.wc-shipping-zone-method-title', {
+			hasText: 'E2E Non-Modal Method',
+		} )
+	).toBeVisible();
+
+	const methods = await restApi.get(
+		`${ WC_API_PATH }/shipping/zones/${ zone.id }/methods`
+	);
+	const flatRate = methods.data.find(
+		( method: { method_id: string } ) => method.method_id === 'flat_rate'
+	);
+
+	expect( flatRate.enabled ).toBe( false );
+	await expect(
+		page.locator( '.wc-shipping-zone-method-enabled a' ).first()
+	).toHaveText( 'No' );
+	expect( dialogMessages ).toEqual( [] );
+} );
+
+// Cancelling the settings modal of a method that was just added removes it again. That cleanup
+// must leave the toggle the table still shows pending, not overwrite it with the server's rows.
+test( 'keeps an unsaved method toggle pending when the settings of an added method are cancelled', async ( {
+	page,
+	restApi,
+	zone,
+} ) => {
+	await openZone( page, zone.name );
+
+	await page.locator( '.wc-shipping-zone-method-enabled a' ).first().click();
+	await expect(
+		page.locator( '.wc-shipping-zone-method-enabled a' ).first()
+	).toHaveText( 'No' );
+
+	await openAddMethodModal( page );
+	await page.getByText( 'Free shipping', { exact: true } ).click();
+	await page.getByRole( 'button', { name: 'Continue' } ).click();
+	await expect( page.locator( '#btn-ok' ) ).toBeVisible();
+
+	const removeMethodResponse = page.waitForResponse( ( response ) =>
+		response
+			.url()
+			.includes( 'action=woocommerce_shipping_zone_remove_method' )
+	);
+	await page.keyboard.press( 'Escape' );
+	await removeMethodResponse;
+	await expect( page.locator( '.blockUI' ) ).toHaveCount( 0 );
+
+	await expect(
+		page.locator( '.wc-shipping-zone-method-enabled a' ).first()
+	).toHaveText( 'No' );
+	await expect( page.locator( '#submit' ) ).toBeEnabled();
+
+	await Promise.all( [
+		page.waitForResponse( ( response ) =>
+			isSaveChanges( new URL( response.url() ) )
+		),
+		page.locator( '#submit' ).click(),
+	] );
+
+	const methods = await restApi.get(
+		`${ WC_API_PATH }/shipping/zones/${ zone.id }/methods`
+	);
+	expect( methods.data ).toHaveLength( 1 );
+	expect( methods.data[ 0 ].enabled ).toBe( false );
+} );
+
+// Regression: adding a method with a settings modal marks the form dirty through the model's
+// method list, but the add already persisted the method, so the zone-level change set is empty.
+// A failed settings save left Save changes enabled over that empty set, and submitting it posts
+// no changes at all, which the endpoint rejects as missing_fields. The non-modal branch cannot
+// show this: it reaches onSaveResponse, whose 'saved:methods' already disables the button.
+test( 'leaves Save changes disabled when a settings save fails on a freshly added method', async ( {
+	page,
+	zone,
+} ) => {
+	await openZone( page, zone.name );
+
+	const dialogMessages: string[] = [];
+	page.on( 'dialog', async ( dialog ) => {
+		dialogMessages.push( dialog.message() );
+		await dialog.accept();
+	} );
+
+	await page.route(
+		( url ) => url.searchParams.get( 'action' ) === SAVE_SETTINGS_ACTION,
+		( route ) =>
+			route.fulfill( {
+				status: 500,
+				contentType: 'text/plain',
+				body: 'Injected settings save failure',
+			} )
+	);
+
+	// Free shipping has a settings modal and is not already on the fixture's zone.
+	await openAddMethodModal( page );
+	await page.getByText( 'Free shipping', { exact: true } ).click();
+	await page.getByRole( 'button', { name: 'Continue' } ).click();
+
+	await page.locator( '#btn-ok' ).click();
+
+	await expect
+		.poll( () => dialogMessages )
+		.toContain( 'Your changes were not saved. Please retry.' );
+
+	// The zone itself was never edited, so there is nothing to retry.
+	await expect( page.locator( '#submit' ) ).toBeDisabled();
+	await expect( page.locator( '#submit' ) ).not.toHaveClass( /is-busy/ );
+
+	// With nothing left to save, leaving the page must not raise the unsaved-changes prompt.
+	await page.goto( 'wp-admin/admin.php?page=wc-settings&tab=shipping' );
+	expect( dialogMessages ).toHaveLength( 1 );
+} );
+
+// Guards the other half of the branch, not the toggle fix: a delete marks the form clean while
+// a zone edit is still pending. Simplifying down to the unload flag alone would drop this case.
+test( 'keeps Save changes clickable when a failed save carries a zone edit made before a delete', async ( {
+	page,
+	restApi,
+	zone,
+} ) => {
+	// The delete only leaves that entry behind when another method remains.
+	await restApi.post(
+		`${ WC_API_PATH }/shipping/zones/${ zone.id }/methods`,
+		{
+			method_id: 'free_shipping',
+		}
+	);
+
+	await openZone( page, zone.name );
+
+	const dialogMessages: string[] = [];
+	page.on( 'dialog', async ( dialog ) => {
+		dialogMessages.push( dialog.message() );
+		await dialog.accept();
+	} );
+
+	// Editing the name re-renders the rows and rebinds their handlers, so hold on to a row
+	// and wait for it to leave the document before clicking a row action.
+	const staleRow = await page
+		.locator( '.wc-shipping-zone-method-rows tr' )
+		.first()
+		.elementHandle();
+
+	// The screen re-renders on both `input` and `change`, so set the value with a single
+	// event to keep the row this test clicks from being replaced underneath it.
+	await page.getByLabel( 'Zone name' ).evaluate( ( input, value ) => {
+		( input as HTMLInputElement ).value = value;
+		input.dispatchEvent( new Event( 'change', { bubbles: true } ) );
+	}, `Failed save ${ rand() }` );
+	await expect( page.locator( '#submit' ) ).toBeEnabled();
+	await page.waitForFunction( ( row ) => ! row.isConnected, staleRow );
+
+	await page
+		.locator(
+			'.wc-shipping-zone-method-rows .wc-shipping-zone-method-delete'
+		)
+		.first()
+		.click();
+	await expect
+		.poll( () => dialogMessages )
+		.toContain( 'Are you sure you want to delete this shipping method?' );
+	await expect(
+		page.locator(
+			'.wc-shipping-zone-method-rows .wc-shipping-zone-method-title'
+		)
+	).toHaveCount( 1 );
+
+	await page.route( isSaveChanges, ( route ) =>
+		route.fulfill( {
+			status: 500,
+			contentType: 'text/plain',
+			body: 'Injected save failure',
+		} )
+	);
+
+	// A successful settings save flushes the pending zone edit through save_changes without
+	// logging a change of its own, so the form is still marked clean when that save fails.
+	await page.locator( 'a.wc-shipping-zone-method-settings' ).first().click();
+	await page.locator( '#btn-ok' ).click();
+
+	await expect
+		.poll( () => dialogMessages )
+		.toContain( 'Your changes were not saved. Please retry.' );
+
+	await expect( page.locator( '.blockUI' ) ).toHaveCount( 0 );
+	await expect( page.locator( '#submit' ) ).toBeEnabled();
+	await expect( page.locator( '#submit' ) ).not.toHaveClass( /is-busy/ );
+} );
+
 // Regression: an edit typed while a save is in flight must stay unsaved instead of being
 // cleared when that response lands, so the next save still carries it.
 test( 'keeps a zone edit made while an earlier save is in flight', async ( {