Commit c410ea1d1a9 for woocommerce

commit c410ea1d1a9cc7dc66305cb39a4d26236ec1f572
Author: James Kemp <me@jckemp.com>
Date:   Tue Dec 1 17:09:38 2026 +0000

    Fix misleading "already fully refunded" message on zero-total orders (#71326)

    * Fix misleading "already fully refunded" message on zero-total orders

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

    ---------

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

diff --git a/plugins/woocommerce/changelog/71326-fix-71283-refund-of-an-order-with-a b/plugins/woocommerce/changelog/71326-fix-71283-refund-of-an-order-with-a
new file mode 100644
index 00000000000..ff4ff89a9ac
--- /dev/null
+++ b/plugins/woocommerce/changelog/71326-fix-71283-refund-of-an-order-with-a
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix the misleading "already fully refunded" message shown when refunding an order with a total of 0 that was never refunded.
diff --git a/plugins/woocommerce/src/Internal/RestApi/Refunds/DataUtils.php b/plugins/woocommerce/src/Internal/RestApi/Refunds/DataUtils.php
index cbc0feea335..28a746729e6 100644
--- a/plugins/woocommerce/src/Internal/RestApi/Refunds/DataUtils.php
+++ b/plugins/woocommerce/src/Internal/RestApi/Refunds/DataUtils.php
@@ -207,11 +207,7 @@ class DataUtils {
 		// by both endpoints rather than via the controller's later
 		// refund_exceeds_remaining guard.
 		if ( (float) $order->get_remaining_refund_amount() <= 0 ) {
-			return new WP_Error(
-				'order_not_refundable',
-				__( 'This order has already been fully refunded.', 'woocommerce' ),
-				array( 'status' => WP_Http::UNPROCESSABLE_ENTITY )
-			);
+			return $this->get_nothing_left_to_refund_error( $order );
 		}

 		// Precompute refunded quantities/totals once so the over-refund check
@@ -1092,11 +1088,7 @@ class DataUtils {
 		}

 		if ( (float) $order->get_remaining_refund_amount() <= 0 ) {
-			return new WP_Error(
-				'order_not_refundable',
-				__( 'This order has already been fully refunded.', 'woocommerce' ),
-				array( 'status' => WP_Http::UNPROCESSABLE_ENTITY )
-			);
+			return $this->get_nothing_left_to_refund_error( $order );
 		}

 		$refund_data = $this->compute_refunded_quantities_and_totals( $order );
@@ -1287,6 +1279,27 @@ class DataUtils {
 		return true;
 	}

+	/**
+	 * Build the error returned when an order has no amount left to refund.
+	 *
+	 * Only claims the order was fully refunded when it actually has refunds;
+	 * a zero-total order that was never refunded gets a neutral message.
+	 *
+	 * @param WC_Order $order The order being refunded.
+	 * @return WP_Error
+	 */
+	private function get_nothing_left_to_refund_error( $order ): WP_Error {
+		$message = $order->get_refunds()
+			? __( 'This order has already been fully refunded.', 'woocommerce' )
+			: __( 'This order has no amount left to refund.', 'woocommerce' );
+
+		return new WP_Error(
+			'order_not_refundable',
+			$message,
+			array( 'status' => WP_Http::UNPROCESSABLE_ENTITY )
+		);
+	}
+
 	/**
 	 * Run the full refund preview pipeline for one request.
 	 *
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Refunds/DataUtilsTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Refunds/DataUtilsTest.php
index a9ab861869f..1c82360ffbc 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Refunds/DataUtilsTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Refunds/DataUtilsTest.php
@@ -1358,6 +1358,35 @@ class DataUtilsTest extends WC_Unit_Test_Case {
 		$this->assertEquals( 'order_not_refundable', $result->get_error_code() );
 	}

+	/**
+	 * @testdox Should not claim a never-refunded zero-total order was already fully refunded.
+	 */
+	public function test_zero_total_order_without_refunds_gets_neutral_error(): void {
+		$order = $this->create_order_with_taxes( array(), 50.00 );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->set_total( 0 );
+		$order->save();
+
+		$items     = $order->get_items( 'line_item' );
+		$item      = reset( $items );
+		$line_data = array(
+			array(
+				'line_item_id' => $item->get_id(),
+				'quantity'     => 1,
+			),
+		);
+
+		$preview_result = $this->data_utils->validate_preview_line_items( $line_data, $order );
+		$create_result  = $this->data_utils->validate_line_items( $line_data, $order );
+
+		foreach ( array( $preview_result, $create_result ) as $result ) {
+			$this->assertInstanceOf( \WP_Error::class, $result );
+			$this->assertEquals( 'order_not_refundable', $result->get_error_code() );
+			$this->assertEquals( 'This order has no amount left to refund.', $result->get_error_message() );
+			$this->assertEquals( 422, $result->get_error_data()['status'] );
+		}
+	}
+
 	/**
 	 * @testdox Should return missing_line_item_id when line_item_id key is absent.
 	 */