Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Prevent fatal errors when using Cart Tokens and creating new accounts on checkout #11785

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/StoreApi/Routes/V1/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,21 @@ private function process_customer( \WP_REST_Request $request ) {
$request['billing_address']['first_name'],
$request['billing_address']['last_name']
);
// Log the customer in.
wc_set_customer_auth_cookie( $customer_id );

// Associate customer with the order.
// Associate customer with the order. This is done before login to ensure the order is associated with
// the correct customer if login fails.
$this->order->set_customer_id( $customer_id );
$this->order->save();

// Log the customer in to WordPress. Doing this inline instead of using `wc_set_customer_auth_cookie`
// because wc_set_customer_auth_cookie forces the use of session cookie.
wp_set_current_user( $customer_id );
wp_set_auth_cookie( $customer_id, true );

// Init session cookie if the session cookie handler exists.
if ( is_callable( [ WC()->session, 'init_session_cookie' ] ) ) {
WC()->session->init_session_cookie();
}
}
} catch ( \Exception $error ) {
switch ( $error->getMessage() ) {
Expand Down
Loading