Commit 6f99fdc6b33 for woocommerce
commit 6f99fdc6b33b0e6ee8bf351dd94efcbf29bbd519
Author: Anand Rajaram <anandrajaram21@gmail.com>
Date: Tue Jun 16 12:46:41 2026 +0530
Skip saving checkout shipping totals to customer meta (#65544)
diff --git a/plugins/woocommerce/changelog/65544-65104-stale-user-meta b/plugins/woocommerce/changelog/65544-65104-stale-user-meta
new file mode 100644
index 00000000000..1b5b989d407
--- /dev/null
+++ b/plugins/woocommerce/changelog/65544-65104-stale-user-meta
@@ -0,0 +1,4 @@
+Significance: minor
+Type: fix
+
+Fixes checkout flow to not save shipping_method in customer meta
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/class-wc-checkout.php b/plugins/woocommerce/includes/class-wc-checkout.php
index 520c1ef8036..3fd0f65557c 100644
--- a/plugins/woocommerce/includes/class-wc-checkout.php
+++ b/plugins/woocommerce/includes/class-wc-checkout.php
@@ -21,6 +21,17 @@ defined( 'ABSPATH' ) || exit;
class WC_Checkout {
use CogsAwareTrait;
+ /**
+ * Checkout/order-level shipping fields that should not be persisted as generic meta.
+ *
+ * @var string[]
+ */
+ private const SHIPPING_FIELDS_EXCLUDED_FROM_META = array(
+ 'shipping_method',
+ 'shipping_total',
+ 'shipping_tax',
+ );
+
/**
* The single instance of the class.
*
@@ -421,19 +432,15 @@ class WC_Checkout {
'billing' => true,
);
- $shipping_fields = array(
- 'shipping_method' => true,
- 'shipping_total' => true,
- 'shipping_tax' => true,
- );
foreach ( $data as $key => $value ) {
if ( is_callable( array( $order, "set_{$key}" ) ) ) {
$order->{"set_{$key}"}( $value );
// Store custom fields prefixed with either shipping_ or billing_. This is for backwards compatibility with 2.6.x.
- } elseif ( isset( $fields_prefix[ current( explode( '_', $key ) ) ] ) ) {
- if ( ! isset( $shipping_fields[ $key ] ) ) {
- $order->update_meta_data( '_' . $key, $value );
- }
+ } elseif (
+ isset( $fields_prefix[ current( explode( '_', $key ) ) ] )
+ && ! in_array( $key, self::SHIPPING_FIELDS_EXCLUDED_FROM_META, true )
+ ) {
+ $order->update_meta_data( '_' . $key, $value );
}
}
@@ -1257,7 +1264,10 @@ class WC_Checkout {
$customer->{"set_{$key}"}( $value );
// Store custom fields prefixed with either shipping_ or billing_.
- } elseif ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) ) {
+ } elseif (
+ ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) )
+ && ! in_array( $key, self::SHIPPING_FIELDS_EXCLUDED_FROM_META, true )
+ ) {
$customer->update_meta_data( $key, $value );
}
}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/checkout/checkout.php b/plugins/woocommerce/tests/legacy/unit-tests/checkout/checkout.php
index 7b433ff8e38..658e30cce05 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/checkout/checkout.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/checkout/checkout.php
@@ -364,4 +364,59 @@ class WC_Tests_Checkout extends WC_Unit_Test_Case {
'The customer-chosen value for an "Any" variation attribute must be persisted on the order line item.'
);
}
+
+ /**
+ * @testdox Should not save checkout shipping fields as customer meta.
+ *
+ * @throws ReflectionException When unable to reflect the checkout method.
+ */
+ public function test_process_customer_does_not_save_checkout_shipping_fields_as_customer_meta(): void {
+ $user_id = wp_create_user( 'checkout-shipping-fields-customer', 'password', 'checkout-shipping-fields-customer@example.com' );
+ $this->assertNotWPError( $user_id );
+ wp_set_current_user( $user_id );
+
+ $process_customer = new ReflectionMethod( WC_Checkout::class, 'process_customer' );
+ $process_customer->setAccessible( true );
+ $process_customer->invoke(
+ WC_Checkout::instance(),
+ array(
+ 'billing_first_name' => 'Jane',
+ 'billing_last_name' => 'Customer',
+ 'billing_email' => 'checkout-shipping-fields-customer@example.com',
+ 'shipping_address_1' => '123 Test Street',
+ 'shipping_custom' => 'custom shipping value',
+ 'shipping_method' => array( 'flat_rate:1' ),
+ 'shipping_total' => '5.00',
+ 'shipping_tax' => '0.50',
+ )
+ );
+
+ $this->assertSame(
+ '123 Test Street',
+ get_user_meta( $user_id, 'shipping_address_1', true ),
+ 'Regular shipping address fields should still be saved as customer meta.'
+ );
+ $this->assertSame(
+ 'custom shipping value',
+ get_user_meta( $user_id, 'shipping_custom', true ),
+ 'Custom shipping fields should still be saved as customer meta.'
+ );
+ $this->assertSame(
+ '',
+ get_user_meta( $user_id, 'shipping_method', true ),
+ 'The selected checkout shipping method should not be saved as customer meta.'
+ );
+ $this->assertSame(
+ '',
+ get_user_meta( $user_id, 'shipping_total', true ),
+ 'Checkout shipping totals should not be saved as customer meta.'
+ );
+ $this->assertSame(
+ '',
+ get_user_meta( $user_id, 'shipping_tax', true ),
+ 'Checkout shipping taxes should not be saved as customer meta.'
+ );
+
+ wp_set_current_user( 0 );
+ }
}