diff --git a/includes/class-wc-customer.php b/includes/class-wc-customer.php index 2b41ad764e3..67925684182 100644 --- a/includes/class-wc-customer.php +++ b/includes/class-wc-customer.php @@ -1012,10 +1012,16 @@ class WC_Customer extends WC_Legacy_Customer { } /** - * Save data (either create or update depending on if we are working on an existing customer). + * Save data. Create when creating a new user/class, update when editing + * an existing user, and save session when working on a logged out guest + * session. * @since 2.7.0 */ public function save() { + if ( $this->_from_session && ! $this->_is_user ) { + $this->save_session_if_changed(); + return; + } if ( ! $this->_is_user ) { $this->create(); } else { diff --git a/tests/unit-tests/customer/crud.php b/tests/unit-tests/customer/crud.php index 91ceac4c588..46220f70882 100644 --- a/tests/unit-tests/customer/crud.php +++ b/tests/unit-tests/customer/crud.php @@ -416,6 +416,15 @@ class CustomerCRUD extends \WC_Unit_Test_Case { $session = new \WC_Customer(); $session->load_session(); $this->assertEquals( '124 South Street', $session->get_billing_address() ); + + $session = new \WC_Customer(); + $session->load_session(); + $session->set_billing_postcode( '32191' ); + $session->save(); + + // should still be session ID, not a created row, since we are working with guests/sessions + $this->assertFalse( is_numeric( $session->get_id() ) ); + $this->assertEquals( '32191' , $session->get_billing_postcode() ); } /**