Commit aff1b3752a1 for woocommerce
commit aff1b3752a15dd59632328213aea3152aeeb16ef
Author: Anuj Singh <80690679+Anuj-Rathore24@users.noreply.github.com>
Date: Wed Nov 25 01:50:34 2026 +0530
Return empty array from wc_get_order_notes() when order_id or post_id is zero or empty (#71367)
* Return empty array from wc_get_order_notes() when order_id or post_id is zero or empty
* Address CodeRabbit review feedback on wc_get_order_notes() guard
diff --git a/plugins/woocommerce/changelog/fix-71100-wc-get-order-notes-empty-order-id b/plugins/woocommerce/changelog/fix-71100-wc-get-order-notes-empty-order-id
new file mode 100644
index 00000000000..b7a423ff4e5
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-71100-wc-get-order-notes-empty-order-id
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Prevent wc_get_order_notes() from returning notes for all orders when order_id or post_id is empty or zero, such as during email preview rendering.
diff --git a/plugins/woocommerce/includes/wc-order-functions.php b/plugins/woocommerce/includes/wc-order-functions.php
index 9b864c11fd8..3b3efa52c1e 100644
--- a/plugins/woocommerce/includes/wc-order-functions.php
+++ b/plugins/woocommerce/includes/wc-order-functions.php
@@ -1288,6 +1288,7 @@ function wc_get_order_note( $data ) {
* Get order notes.
*
* @since 3.2.0
+ * @since 11.5.0 Return an empty array when order_id or post_id is explicitly provided as 0 or empty without order__in/post__in.
* @param array $args Query arguments {
* Array of query parameters.
*
@@ -1314,6 +1315,8 @@ function wc_get_order_note( $data ) {
* @return stdClass[] Array of stdClass objects with order notes details.
*/
function wc_get_order_notes( $args ) {
+ $has_null_order_id = is_array( $args ) && array_key_exists( 'order_id', $args ) && null === $args['order_id'];
+
$key_mapping = array(
'limit' => 'number',
'page' => 'paged',
@@ -1329,6 +1332,17 @@ function wc_get_order_notes( $args ) {
}
}
+ // WP_Comment_Query treats post_id = 0/empty as "no filter" and returns notes across all orders.
+ if ( is_array( $args ) && empty( $args['post__in'] ) ) {
+ if ( array_key_exists( 'post_id', $args ) ) {
+ if ( ! is_numeric( $args['post_id'] ) || (int) $args['post_id'] <= 0 ) {
+ return array();
+ }
+ } elseif ( $has_null_order_id ) {
+ return array();
+ }
+ }
+
// Define orderby.
$orderby_mapping = array(
'date_created' => 'comment_date',
diff --git a/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php
index de9b8ca18fe..917ffcd6183 100644
--- a/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php
@@ -944,6 +944,74 @@ class WC_Order_Functions_Test extends \WC_Unit_Test_Case {
);
}
+ /**
+ * @testdox Should return no order notes when order_id or post_id is empty or non-positive unless order__in or post__in is provided.
+ *
+ * @see https://github.com/woocommerce/woocommerce/issues/71100
+ */
+ public function test_wc_get_order_notes_returns_empty_for_empty_or_invalid_order_id(): void {
+ $order1 = WC_Helper_Order::create_order();
+ $order1->add_order_note( 'Order 1 internal note' );
+ $order1->add_order_note( 'Order 1 customer note', 1 );
+
+ $order2 = WC_Helper_Order::create_order();
+ $order2->add_order_note( 'Order 2 internal note' );
+
+ $invalid_ids = array( 0, '0', '', null, false, -1, 'invalid' );
+ foreach ( $invalid_ids as $invalid_id ) {
+ $this->assertSame(
+ array(),
+ wc_get_order_notes( array( 'order_id' => $invalid_id ) ),
+ sprintf( 'Expected empty array for order_id=%s', var_export( $invalid_id, true ) ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
+ );
+ $this->assertSame(
+ array(),
+ wc_get_order_notes( array( 'post_id' => $invalid_id ) ),
+ sprintf( 'Expected empty array for post_id=%s', var_export( $invalid_id, true ) ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
+ );
+ }
+
+ // Calling without order_id or post_id should continue returning notes across orders.
+ $all_customer_notes = wc_get_order_notes( array( 'type' => 'customer' ) );
+ $this->assertContains( 'Order 1 customer note', wp_list_pluck( $all_customer_notes, 'content' ) );
+
+ // Using order__in / post__in should continue working even without order_id or with default order_id => 0.
+ $order1_notes = wc_get_order_notes( array( 'order__in' => array( $order1->get_id() ) ) );
+ $this->assertContains( 'Order 1 internal note', wp_list_pluck( $order1_notes, 'content' ) );
+ $this->assertNotContains( 'Order 2 internal note', wp_list_pluck( $order1_notes, 'content' ) );
+
+ $order2_notes = wc_get_order_notes(
+ array(
+ 'order_id' => 0,
+ 'order__in' => array( $order2->get_id() ),
+ )
+ );
+ $this->assertContains( 'Order 2 internal note', wp_list_pluck( $order2_notes, 'content' ) );
+ $this->assertNotContains( 'Order 1 internal note', wp_list_pluck( $order2_notes, 'content' ) );
+
+ // An empty order__in overrides post__in after mapping, so zero order_id must still return empty.
+ $this->assertSame(
+ array(),
+ wc_get_order_notes(
+ array(
+ 'order_id' => 0,
+ 'post__in' => array( $order1->get_id() ),
+ 'order__in' => array(),
+ )
+ )
+ );
+
+ // A null order_id falls back to a valid post_id to match isset()-based mapping precedence.
+ $post_id_fallback_notes = wc_get_order_notes(
+ array(
+ 'order_id' => null,
+ 'post_id' => $order1->get_id(),
+ )
+ );
+ $this->assertContains( 'Order 1 internal note', wp_list_pluck( $post_id_fallback_notes, 'content' ) );
+ $this->assertNotContains( 'Order 2 internal note', wp_list_pluck( $post_id_fallback_notes, 'content' ) );
+ }
+
/**
* @testdox Should show an order note's time in the current site timezone after the timezone changes.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewTest.php
index 9e95ebf371a..5e33eaafbec 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewTest.php
@@ -503,4 +503,37 @@ class EmailPreviewTest extends WC_Unit_Test_Case {
$this->assertSame( array(), $order->get_refunds(), 'get_refunds() must not read refunds from the database.' );
$this->assertSame( array(), $order->get_customer_order_notes(), 'get_customer_order_notes() must not read notes from the database.' );
}
+
+ /**
+ * @testdox Email preview must not leak real order notes when a template or hook calls wc_get_order_notes().
+ *
+ * @see https://github.com/woocommerce/woocommerce/issues/71100
+ */
+ public function test_preview_does_not_leak_order_notes_via_wc_get_order_notes(): void {
+ $real_order = wc_create_order();
+ $real_order->add_order_note( 'Secret customer note from real order', 1 );
+ $real_order->save();
+
+ $captured_notes = null;
+ add_action(
+ 'woocommerce_email_order_details',
+ function ( $order ) use ( &$captured_notes ) {
+ $captured_notes = wc_get_order_notes(
+ array(
+ 'order_id' => $order->get_id(),
+ 'type' => 'customer',
+ )
+ );
+ foreach ( $captured_notes as $note ) {
+ echo wp_kses_post( wpautop( $note->content ) );
+ }
+ }
+ );
+
+ $this->sut->set_email_type( 'WC_Email_Customer_Completed_Order' );
+ $content = $this->sut->render();
+
+ $this->assertSame( array(), $captured_notes, 'wc_get_order_notes() must return no notes for the preview order.' );
+ $this->assertStringNotContainsString( 'Secret customer note from real order', $content, 'Real order notes must not appear in the rendered email preview.' );
+ }
}