get_customer_id() || $order->has_status( array( 'failed', 'cancelled' ) ) ) { return false; } $email = (string) $order->get_billing_email(); $created = $order->get_date_created(); return is_email( $email ) && ! email_exists( $email ) && $created && ( time() - $created->getTimestamp() ) < self::MAX_AGE; } /** * The box. * * @param \WC_Order|int $order Order or id. */ public static function render( $order ): void { $order = $order instanceof \WC_Order ? $order : wc_get_order( $order ); if ( self::$printed || ! self::eligible( $order ) ) { return; } self::$printed = true; wp_enqueue_script( 'aiolos-thankyou-account', Plugin::asset( 'js/thankyou-account.js' ), array(), Plugin::asset_version( 'js/thankyou-account.js' ), true ); wp_localize_script( 'aiolos-thankyou-account', 'aiolosTyAccount', array( 'url' => \WC_AJAX::get_endpoint( 'aiolos_ty_account' ), 'error' => __( 'Something went wrong. Please try again.', 'aiolos-checkout-for-woocommerce' ), ) ); ?>

get_billing_email() ) ); ?>

__( 'Your session expired. Please refresh the page.', 'aiolos-checkout-for-woocommerce' ) ), 403 ); } $order = wc_get_order( $order_id ); $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; if ( ! $order || ! hash_equals( (string) $order->get_order_key(), $key ) || ! self::eligible( $order ) ) { wp_send_json_error( array( 'message' => __( 'An account can no longer be created from this order. Use "Register" in My account.', 'aiolos-checkout-for-woocommerce' ) ), 409 ); } $password = isset( $_POST['password'] ) && is_string( $_POST['password'] ) ? wp_unslash( $_POST['password'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- a password is used as typed. if ( strlen( $password ) < 8 ) { wp_send_json_error( array( 'message' => __( 'Choose a password of at least 8 characters.', 'aiolos-checkout-for-woocommerce' ) ), 400 ); } $email = (string) $order->get_billing_email(); $user_id = wc_create_new_customer( $email, wc_create_new_customer_username( $email, array( 'first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), ) ), $password, array( 'first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), ) ); if ( is_wp_error( $user_id ) ) { wp_send_json_error( array( 'message' => wp_strip_all_tags( $user_id->get_error_message() ) ), 400 ); } // The addresses of the order become the account's. $customer = new \WC_Customer( $user_id ); foreach ( array( 'billing', 'shipping' ) as $type ) { foreach ( array( 'first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country', 'phone' ) as $field ) { $getter = "get_{$type}_{$field}"; $setter = "set_{$type}_{$field}"; if ( is_callable( array( $order, $getter ) ) && is_callable( array( $customer, $setter ) ) ) { $customer->{$setter}( $order->{$getter}() ); } } } $customer->set_billing_email( $order->get_billing_email() ); $customer->save(); $order->set_customer_id( $user_id ); $order->add_order_note( __( 'The customer created an account from the thank-you page.', 'aiolos-checkout-for-woocommerce' ) ); $order->save(); wc_update_new_customer_past_orders( $user_id ); wc_set_customer_auth_cookie( $user_id ); wp_send_json_success( array( 'message' => __( 'Your account is ready and you are logged in.', 'aiolos-checkout-for-woocommerce' ), 'link' => wc_get_page_permalink( 'myaccount' ), 'linkText' => __( 'Go to My account', 'aiolos-checkout-for-woocommerce' ), ) ); } }