Commit 95b2d6fca73 for woocommerce

commit 95b2d6fca7394979296704453354ba622f3b769c
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date:   Fri Nov 20 18:22:47 2026 -0300

    Refuse a repeat trash in the order and product delete abilities (#71257)

    * Refuse a repeat trash in the order and product delete abilities

    A trash of an object that is already in the trash wrote nothing, but
    the call reported success and the extension syncs got the object in
    `trashed`. The call now stops in load() with a 409 that tells the agent
    to pass force: true, so no validator, save or sync runs.

    * Unset unused sync callback parameters in the repeat trash test

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Drop the idempotent hint from the order and product delete abilities

    A retry after a timeout now gets a 409 for a repeat trash, so clients
    must not treat these abilities as safe to retry.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    ---------

    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/wooai-1742-refuse-repeat-trash b/plugins/woocommerce/changelog/wooai-1742-refuse-repeat-trash
new file mode 100644
index 00000000000..fade637aea9
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooai-1742-refuse-repeat-trash
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Refuse to trash a product or order that is already in the trash through the delete abilities, instead of reporting a new trash.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/OrderDelete.php b/plugins/woocommerce/src/Internal/Abilities/Domain/OrderDelete.php
index bf994bd478b..30488da4bf8 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/OrderDelete.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/OrderDelete.php
@@ -108,7 +108,7 @@ class OrderDelete extends ActionableAbility {
 				),
 				'annotations'  => array(
 					'readonly'    => false,
-					'idempotent'  => true,
+					'idempotent'  => false,
 					'destructive' => true,
 				),
 			),
@@ -171,6 +171,15 @@ class OrderDelete extends ActionableAbility {
 			);
 		}

+		if ( ! $this->force && OrderStatus::TRASH === $order->get_status( 'edit' ) ) {
+			return new \WP_Error(
+				'woocommerce_ability_order_already_trashed',
+				/* translators: %d: order ID. */
+				sprintf( __( 'Order %d is already in the trash. Pass force: true to delete it permanently.', 'woocommerce' ), $order->get_id() ),
+				array( 'status' => 409 )
+			);
+		}
+
 		$this->order_id = $order->get_id();

 		return $order;
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php
index 31d21312cb3..382ccef4843 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php
@@ -105,7 +105,7 @@ class ProductDelete extends ActionableAbility {
 				),
 				'annotations'  => array(
 					'readonly'    => false,
-					'idempotent'  => true,
+					'idempotent'  => false,
 					'destructive' => true,
 				),
 			),
@@ -168,6 +168,15 @@ class ProductDelete extends ActionableAbility {
 			);
 		}

+		if ( ! $this->force && ProductStatus::TRASH === $product->get_status( 'edit' ) ) {
+			return new \WP_Error(
+				'woocommerce_ability_product_already_trashed',
+				/* translators: %d: product ID. */
+				sprintf( __( 'Product %d is already in the trash. Pass force: true to delete it permanently.', 'woocommerce' ), $product->get_id() ),
+				array( 'status' => 409 )
+			);
+		}
+
 		$this->product_id = $product->get_id();

 		return $product;
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index 9bffe109760..7b2f338a437 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -317,7 +317,7 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 			),
 			'woocommerce/product-delete'              => array(
 				'readonly'    => false,
-				'idempotent'  => true,
+				'idempotent'  => false,
 				'destructive' => true,
 			),
 			'woocommerce/orders-query'                => array(
@@ -337,7 +337,7 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 			),
 			'woocommerce/order-delete'                => array(
 				'readonly'    => false,
-				'idempotent'  => true,
+				'idempotent'  => false,
 				'destructive' => true,
 			),
 			'woocommerce/order-update'                => array(
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderDeleteTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderDeleteTest.php
index 5be85e16719..3686705acf5 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderDeleteTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderDeleteTest.php
@@ -86,7 +86,7 @@ class OrderDeleteTest extends WC_Unit_Test_Case {
 	}

 	/**
-	 * @testdox Should move an order to the trash by default, and report it as trashed, also when it is already there.
+	 * @testdox Should move an order to the trash by default, refuse to trash it again, and still delete it with force.
 	 *
 	 * @dataProvider storage_modes
 	 * @param bool $hpos Whether to use the custom orders table.
@@ -95,15 +95,25 @@ class OrderDeleteTest extends WC_Unit_Test_Case {
 		$this->use_storage( $hpos );
 		$order = OrderHelper::create_order();

-		$first  = $this->execute( array( 'id' => $order->get_id() ) );
-		$second = $this->execute( array( 'id' => $order->get_id() ) );
+		$first   = $this->execute( array( 'id' => $order->get_id() ) );
+		$second  = wp_get_ability( 'woocommerce/order-delete' )->execute( array( 'id' => $order->get_id() ) );
+		$trashed = wc_get_order( $order->get_id() )->get_status();
+		$deleted = $this->execute(
+			array(
+				'id'    => $order->get_id(),
+				'force' => true,
+			)
+		);

 		$this->assertArrayHasKey( 'id', $first );
 		$this->assertSame( $order->get_id(), $first['id'] );
 		$this->assertArrayHasKey( 'status', $first );
 		$this->assertSame( 'trash', $first['status'] );
-		$this->assertSame( 'trash', $second['status'], 'Trashing a trashed order should succeed again.' );
-		$this->assertSame( OrderStatus::TRASH, wc_get_order( $order->get_id() )->get_status() );
+		$this->assertWPError( $second );
+		$this->assertSame( 'woocommerce_ability_order_already_trashed', $second->get_error_code() );
+		$this->assertSame( 409, $second->get_error_data()['status'] );
+		$this->assertSame( OrderStatus::TRASH, $trashed );
+		$this->assertSame( 'deleted', $deleted['status'] );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
index 3ca566928c9..817192d1898 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
@@ -4260,6 +4260,44 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 		$this->assertSame( 0, $fired );
 	}

+	/**
+	 * @testdox Should refuse to trash a product or an order that is already in the trash, and run no sync.
+	 */
+	public function test_a_repeat_trash_is_refused_and_runs_no_sync(): void {
+		$synced = array();
+		foreach ( array( 'product', 'order' ) as $resource_name ) {
+			$this->register_sync(
+				$resource_name,
+				'test-ext',
+				static function ( array $saved, ActionableAbility $ability, array $context ) use ( &$synced, $resource_name ) {
+					unset( $saved, $ability );
+					$synced[] = array( $resource_name, array_map( static fn( \WC_Data $item ) => $item->get_id(), $context['trashed'] ) );
+					return null;
+				}
+			);
+		}
+		$product = \WC_Helper_Product::create_simple_product();
+		$order   = OrderHelper::create_order();
+
+		$this->run_ability( 'woocommerce/product-delete', array( 'id' => $product->get_id() ) );
+		$this->run_ability( 'woocommerce/order-delete', array( 'id' => $order->get_id() ) );
+		$product_again = $this->run_ability_response( 'woocommerce/product-delete', array( 'id' => $product->get_id() ) );
+		$order_again   = $this->run_ability_response( 'woocommerce/order-delete', array( 'id' => $order->get_id() ) );
+
+		$this->assertSame( 409, $product_again->get_status() );
+		$this->assertSame( 'woocommerce_ability_product_already_trashed', $product_again->get_data()['code'] );
+		$this->assertSame( 409, $order_again->get_status() );
+		$this->assertSame( 'woocommerce_ability_order_already_trashed', $order_again->get_data()['code'] );
+		$this->assertSame(
+			array(
+				array( 'product', array( $product->get_id() ) ),
+				array( 'order', array( $order->get_id() ) ),
+			),
+			$synced,
+			'Only the first trash of each object should run the syncs.'
+		);
+	}
+
 	/**
 	 * @testdox Should refuse a sync on a resource that is not registered.
 	 */