From 2482e307e0553775fa7a00550da0cc90e31ff3d6 Mon Sep 17 00:00:00 2001 From: Seghir Nadir Date: Wed, 28 Oct 2020 15:18:22 +0100 Subject: [PATCH] Save order after creating an account (https://github.com/woocommerce/woocommerce-blocks/pull/3260) * Save order after creating an account * create account before saving * fix tests * remove order from createOrder * rename variable * fix tests --- .../src/Domain/Services/CreateAccount.php | 14 +++++--------- .../src/StoreApi/Routes/Checkout.php | 14 +++++++------- .../tests/php/Domain/Services/CreateAccount.php | 11 +++++++---- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php b/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php index c2257fe1141..3d5aee8acf5 100644 --- a/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php +++ b/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php @@ -98,31 +98,27 @@ class CreateAccount { } /** - * Create a user account for specified order and request (if necessary). + * Create a user account for specified request (if necessary). * If a new account is created: - * - The order is associated with the account. * - The user is logged in. * - * @param \WC_Order $order The order currently being processed. * @param \WP_REST_Request $request The current request object being handled. * * @throws Exception On error. * @return int The new user id, or 0 if no user was created. */ - public function from_order_request( \WC_Order $order, \WP_REST_Request $request ) { + public function from_order_request( \WP_REST_Request $request ) { if ( ! self::is_feature_enabled() || ! $this->should_create_customer_account( $request ) ) { return 0; } $customer_id = $this->create_customer_account( - $order->get_billing_email(), - $order->get_billing_first_name(), - $order->get_billing_last_name() + $request['billing_address']['email'], + $request['billing_address']['first_name'], + $request['billing_address']['last_name'] ); - // Log the customer in and associate with the order. wc_set_customer_auth_cookie( $customer_id ); - $order->set_customer_id( get_current_user_id() ); return $customer_id; } diff --git a/plugins/woocommerce-blocks/src/StoreApi/Routes/Checkout.php b/plugins/woocommerce-blocks/src/StoreApi/Routes/Checkout.php index ab8e5c76fd8..92ca8c7643f 100644 --- a/plugins/woocommerce-blocks/src/StoreApi/Routes/Checkout.php +++ b/plugins/woocommerce-blocks/src/StoreApi/Routes/Checkout.php @@ -158,12 +158,6 @@ class Checkout extends AbstractRoute { // Ensure order still matches cart. $order_controller->update_order_from_cart( $order_object ); - // If any form fields were posted, update the order. - $this->update_order_from_request( $order_object, $request ); - - // Check order is still valid. - $order_controller->validate_order_before_payment( $order_object ); - // Create a new user account as necessary. // Note - CreateAccount class includes feature gating logic (i.e. this // may not create an account depending on build). @@ -173,11 +167,17 @@ class Checkout extends AbstractRoute { // for setting initial password. try { $create_account = Package::container()->get( CreateAccount::class ); - $create_account->from_order_request( $order_object, $request ); + $create_account->from_order_request( $request ); + $order_object->set_customer_id( get_current_user_id() ); } catch ( Exception $error ) { $this->handle_error( $error ); } } + // If any form fields were posted, update the order. + $this->update_order_from_request( $order_object, $request ); + + // Check order is still valid. + $order_controller->validate_order_before_payment( $order_object ); // Persist customer address data to account. $order_controller->sync_customer_data_with_order( $order_object ); diff --git a/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php b/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php index 2ccdb92e861..e57da27c7ac 100644 --- a/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php +++ b/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php @@ -42,15 +42,18 @@ class CreateAccount extends WP_UnitTestCase { $test_request = new \WP_REST_Request(); $should_create_account = array_key_exists( 'should_create_account', $options ) ? $options['should_create_account'] : false; $test_request->set_param( 'should_create_account', $should_create_account ); + $test_request->set_param( 'billing_address', [ + 'email' => $email, + 'first_name' => $first_name, + 'last_name' => $last_name + ]); $test_order = new \WC_Order(); - $test_order->set_billing_email( $email ); - $test_order->set_billing_first_name( $first_name ); - $test_order->set_billing_last_name( $last_name ); /// -- End test-specific setup. - $user_id = $this->get_test_instance()->from_order_request( $test_order, $test_request ); + $user_id = $this->get_test_instance()->from_order_request( $test_request ); + $test_order->set_customer_id( $user_id ); /// -- Undo test-specific setup; restore previous state. update_option( 'woocommerce_enable_guest_checkout', $tmp_enable_guest_checkout );