Commit 78e5f0df864 for woocommerce
commit 78e5f0df864911ad2201c9bc23bf56aaaa499325
Author: Job <8783673+jobthomas@users.noreply.github.com>
Date: Fri May 29 09:03:13 2026 +0200
Fix: skip EmailLogger order note for unloaded (preview) order objects (#65371)
diff --git a/plugins/woocommerce/src/Internal/Email/EmailLogger.php b/plugins/woocommerce/src/Internal/Email/EmailLogger.php
index 95b7363379d..a04801ad94e 100644
--- a/plugins/woocommerce/src/Internal/Email/EmailLogger.php
+++ b/plugins/woocommerce/src/Internal/Email/EmailLogger.php
@@ -150,7 +150,7 @@ class EmailLogger implements RegisterHooksInterface {
* @return void
*/
private function maybe_add_order_note( $wc_object, string $email_id, WC_Email $email, bool $success, ?string $error_reason ): void {
- if ( ! $wc_object instanceof WC_Order ) {
+ if ( ! $wc_object instanceof WC_Order || ! $wc_object->get_object_read() ) {
return;
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Email/EmailLoggerTest.php b/plugins/woocommerce/tests/php/src/Internal/Email/EmailLoggerTest.php
index 702a3ce10d7..ab6ec225229 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Email/EmailLoggerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Email/EmailLoggerTest.php
@@ -375,6 +375,7 @@ class EmailLoggerTest extends WC_Unit_Test_Case {
public function test_order_note_added_on_successful_send_for_order(): void {
$order = $this->createMock( \WC_Order::class );
$order->method( 'get_id' )->willReturn( 42 );
+ $order->method( 'get_object_read' )->willReturn( true );
$order->expects( $this->once() )
->method( 'add_order_note' )
->with(
@@ -394,6 +395,7 @@ class EmailLoggerTest extends WC_Unit_Test_Case {
public function test_order_note_added_on_failed_send_for_order(): void {
$order = $this->createMock( \WC_Order::class );
$order->method( 'get_id' )->willReturn( 42 );
+ $order->method( 'get_object_read' )->willReturn( true );
$order->expects( $this->once() )
->method( 'add_order_note' )
->with(
@@ -416,6 +418,7 @@ class EmailLoggerTest extends WC_Unit_Test_Case {
public function test_order_note_failure_includes_error_reason(): void {
$order = $this->createMock( \WC_Order::class );
$order->method( 'get_id' )->willReturn( 42 );
+ $order->method( 'get_object_read' )->willReturn( true );
$order->expects( $this->once() )
->method( 'add_order_note' )
->with(
@@ -438,6 +441,7 @@ class EmailLoggerTest extends WC_Unit_Test_Case {
public function test_order_note_failure_redacts_email_addresses_in_reason(): void {
$order = $this->createMock( \WC_Order::class );
$order->method( 'get_id' )->willReturn( 42 );
+ $order->method( 'get_object_read' )->willReturn( true );
$order->expects( $this->once() )
->method( 'add_order_note' )
->with(
@@ -472,6 +476,22 @@ class EmailLoggerTest extends WC_Unit_Test_Case {
$this->assertLogged( 'info', 'some_product_email' );
}
+ /**
+ * @testdox No order note is added when the order object has not been read from the datastore (e.g. a preview dummy).
+ */
+ public function test_no_order_note_for_unloaded_order_object(): void {
+ $order = $this->createMock( \WC_Order::class );
+ $order->method( 'get_id' )->willReturn( 12345 );
+ $order->method( 'get_object_read' )->willReturn( false );
+ $order->expects( $this->never() )->method( 'add_order_note' );
+
+ $email = $this->create_mock_email( 'customer_processing_order', 'customer@example.com', $order );
+ $this->sut->handle_woocommerce_email_sent( true, 'customer_processing_order', $email );
+
+ // Logger entry should still be written even though no note is added.
+ $this->assertLogged( 'info', 'customer_processing_order' );
+ }
+
/**
* @testdox No order note is added when logging is disabled by the filter.
*/
@@ -480,6 +500,7 @@ class EmailLoggerTest extends WC_Unit_Test_Case {
$order = $this->createMock( \WC_Order::class );
$order->method( 'get_id' )->willReturn( 42 );
+ $order->method( 'get_object_read' )->willReturn( true );
$order->expects( $this->never() )->method( 'add_order_note' );
$email = $this->create_mock_email( 'customer_processing_order', 'customer@example.com', $order );
@@ -494,6 +515,7 @@ class EmailLoggerTest extends WC_Unit_Test_Case {
$order = $this->createMock( \WC_Order::class );
$order->method( 'get_id' )->willReturn( 42 );
+ $order->method( 'get_object_read' )->willReturn( true );
$order->expects( $this->never() )->method( 'add_order_note' );
$email = $this->create_mock_email( 'customer_processing_order', 'customer@example.com', $order );