From f4353f6aa970c9410573adc3b980d2c734cffd03 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Thu, 4 Aug 2016 11:39:34 -0700 Subject: [PATCH] Don't save to the database if we are working with sessions, save to the session when save() is called instead. --- includes/class-wc-customer.php | 8 +++++++- tests/unit-tests/customer/crud.php | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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() ); } /**