* Save order after creating an account

* create account before saving

* fix tests

* remove order from createOrder

* rename variable

* fix tests
This commit is contained in:
Seghir Nadir 2020-10-28 15:18:22 +01:00 committed by GitHub
parent 55007b4601
commit 2482e307e0
3 changed files with 19 additions and 20 deletions

View File

@ -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;
}

View File

@ -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 );

View File

@ -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 );