Commit 278bc9f9757 for woocommerce
commit 278bc9f9757bba1ec4389997c22af058961fd7cd
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Fri Nov 13 11:54:16 2026 +0200
Stop emails after the first in a request from adding the store's country to addresses (#70940)
fix: Stop the email store address from forcing countries on later addresses
WC_Emails::get_store_address() adds a
woocommerce_formatted_address_force_country_display callback at
priority 5 so the store address in the email footer shows its
country, then calls remove_filter() without a priority. That defaults
to priority 10, so the callback is never removed and stays for the
rest of the request. Every address formatted afterwards keeps its
country even when it is the store's.
A request that sends more than one email shows this: the first email
leaves the country out of a customer's address in the store's
country, and every later email in the same request adds it, for
example the customer email sent after the admin new order email.
Introduced in #53224, which added the {store_address} placeholder.
Remove the callback at priority 5. A new WC_Emails_Tests case checks
that the store address shows its country and that an address
formatted afterwards does not; it fails without the fix.
test_wc_get_account_formatted_address expected "United States (US)"
in a US customer's address on a US store. It passed only when its
class ran first in the process, because creating the customer sends
the new account email, whose footer leaked this filter for the rest
of that first test. It now expects the country to be left out, and
sets the store country to say why.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/fix-email-store-address-country-leak b/plugins/woocommerce/changelog/fix-email-store-address-country-leak
new file mode 100644
index 00000000000..cbdeee1817f
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-email-store-address-country-leak
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Show the customer's address the same way in every email sent together, instead of adding the store's country after the first.
diff --git a/plugins/woocommerce/includes/class-wc-emails.php b/plugins/woocommerce/includes/class-wc-emails.php
index 5ec9f6a74c5..8e47080cb86 100644
--- a/plugins/woocommerce/includes/class-wc-emails.php
+++ b/plugins/woocommerce/includes/class-wc-emails.php
@@ -967,7 +967,8 @@ class WC_Emails {
$result = preg_replace( '/<br\/?>/i', ', ', $result );
remove_filter(
'woocommerce_formatted_address_force_country_display',
- array( $this, 'get_store_address_force_country_display' )
+ array( $this, 'get_store_address_force_country_display' ),
+ 5
);
return $result;
}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php
index 207cc260eb8..9359c2b17b5 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php
@@ -272,9 +272,11 @@ class WC_Tests_Account_Functions extends WC_Unit_Test_Case {
* @since 3.3.0
*/
public function test_wc_get_account_formatted_address() {
+ // The customer's country is the store's, so the formatted address leaves it out.
+ update_option( 'woocommerce_default_country', 'US:CA' );
$customer = WC_Helper_Customer::create_customer();
- $this->assertEquals( '123 South Street<br/>Apt 1<br/>San Francisco, CA 94110<br/>United States (US)', wc_get_account_formatted_address( 'billing', $customer->get_id() ) );
+ $this->assertEquals( '123 South Street<br/>Apt 1<br/>San Francisco, CA 94110', wc_get_account_formatted_address( 'billing', $customer->get_id() ) );
$customer->delete( true );
}
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-emails-tests.php b/plugins/woocommerce/tests/php/includes/class-wc-emails-tests.php
index 24e83345e18..fa205784fb0 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-emails-tests.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-emails-tests.php
@@ -178,6 +178,33 @@ class WC_Emails_Tests extends \WC_Unit_Test_Case {
);
}
+ /**
+ * @testdox get_store_address() shows the store's country, and leaves later addresses in that country without it.
+ */
+ public function test_get_store_address_forces_the_country_only_for_the_store_address(): void {
+ update_option( 'woocommerce_default_country', 'US:CA' );
+ update_option( 'woocommerce_store_address', '123 Main Street' );
+ update_option( 'woocommerce_store_city', 'San Francisco' );
+ update_option( 'woocommerce_store_postcode', '94110' );
+
+ $store_address = WC()->mailer()->get_store_address();
+
+ $this->assertStringContainsString( 'United States (US)', $store_address, 'The store address should show the country.' );
+ $this->assertStringNotContainsString(
+ 'United States (US)',
+ WC()->countries->get_formatted_address(
+ array(
+ 'address_1' => '1 Other Street',
+ 'city' => 'Los Angeles',
+ 'state' => 'CA',
+ 'postcode' => '90001',
+ 'country' => 'US',
+ )
+ ),
+ 'Addresses formatted after the store address should leave out the store\'s country again.'
+ );
+ }
+
/**
* @testdox backorder() sends a notification when the setting is enabled (default).
*/