From ffb74a7b0918319778657b08b53c279a8aed8175 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 2 Aug 2016 18:28:31 +0100 Subject: [PATCH] Add user to site on login Closes #11409 --- includes/class-wc-form-handler.php | 22 ++++++++++++++++------ readme.txt | 1 + 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/includes/class-wc-form-handler.php b/includes/class-wc-form-handler.php index 7d8cc89bd74..b2aca304b2d 100644 --- a/includes/class-wc-form-handler.php +++ b/includes/class-wc-form-handler.php @@ -873,9 +873,12 @@ class WC_Form_Handler { if ( ! empty( $_POST['login'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-login' ) ) { try { - $creds = array(); - $username = trim( $_POST['username'] ); + $creds = array( + 'user_password' => $_POST['password'], + 'remember' => isset( $_POST['rememberme'] ), + ); + $username = trim( $_POST['username'] ); $validation_error = new WP_Error(); $validation_error = apply_filters( 'woocommerce_process_login_errors', $validation_error, $_POST['username'], $_POST['password'] ); @@ -904,10 +907,17 @@ class WC_Form_Handler { $creds['user_login'] = $username; } - $creds['user_password'] = $_POST['password']; - $creds['remember'] = isset( $_POST['rememberme'] ); - $secure_cookie = is_ssl() ? true : false; - $user = wp_signon( apply_filters( 'woocommerce_login_credentials', $creds ), $secure_cookie ); + // On multisite, ensure user exists on current site, if not add them before allowing login. + if ( is_multisite() ) { + $user_data = get_user_by( 'login', $username ); + + if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { + add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' ); + } + } + + // Perform the login + $user = wp_signon( apply_filters( 'woocommerce_login_credentials', $creds ), is_ssl() ); if ( is_wp_error( $user ) ) { $message = $user->get_error_message(); diff --git a/readme.txt b/readme.txt index 8f837af4cad..0afc8bc48be 100644 --- a/readme.txt +++ b/readme.txt @@ -164,6 +164,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Improved handling of shop page rewrite rules to allow subpages. * Redirect to login after password reset. * When using authorizations in PayPal standard, automatically capture funds when the order goes processing/completed. +* On multisite, when a user logs into a store with an account on a site, but not the current site, rather than error, add the user to the current site as a customer. [See changelog for all versions](https://raw.githubusercontent.com/woothemes/woocommerce/master/CHANGELOG.txt).