session ) ) { return false; } $tracking_key = $woocommerce->session->get_customer_id(); if ( $tracking_key ) { $guest = IG_ES_Guest_Factory::get_by_key( $tracking_key ); return $guest; } return false; } /** * Updates the current session based on the customer's email. * * Create the customer for the email if needed and contains logic to handle when a customers email changes. * * Cases to handle: * * - Registered user is logged in or remembered via cookie = bail * - Email matches existing customer * - Cookie customer exists * - Cookie and matched customer are the same = do nothing * - Cookie and matched customer are different = cookie must be changed, clear cart from previous key to avoid duplicates * - No cookie customer = Set new cookie to matched customer key * - Email is new * - Cookie customer exists * - Customer data is locked = create new customer, change cookie, clear cart from previous key to avoid duplicates * - Customer data is not locked = update customer email * - No cookie customer = Set new cookie to matched customer key * * @param string $new_email * @param string $language * * @return IG_ES_Customer|false */ public static function set_session_by_captured_email( $new_email, $language = '' ) { if ( ! is_email( $new_email ) || headers_sent() || ! self::session_tracking_enabled() ) { // must have a valid email, be able to set cookies, have session tracking enabled return false; } $new_email = ES_Clean::email( $new_email ); $existing_session_customer = self::get_session_customer(); // existing session customer from cookie $customer_matching_email = IG_ES_Customer_Factory::get_by_email( $new_email, false ); // important! don't create new customer $email_is_new = false === $customer_matching_email; if ( $existing_session_customer && $existing_session_customer->is_registered() ) { return $existing_session_customer; // bail if a registered user is already being tracked } // Check if a customer already exists matching the supplied email if ( $customer_matching_email ) { if ( ! ( $existing_session_customer && $new_email === $existing_session_customer->get_email() ) ) { // Customer has changed so delete the cart for the existing customer // To avoid duplicate abandoned cart emails if ( $existing_session_customer ) { $existing_session_customer->delete_cart(); } } // Set the matched customer as the new customer $new_customer = $customer_matching_email; } else { // Is there an existing session customer if ( $existing_session_customer ) { // Check if existing and new emails are the same // This is actually impossible considering the previous logic but it's probably more confusing to omit this if ( $existing_session_customer->get_email() === $new_email ) { // Nothing to do $new_customer = $existing_session_customer; } else { $guest = $existing_session_customer->get_guest(); // customer can not be a registered user at this point if ( $guest->is_locked() ) { // email has changed and guest is locked so we must create a new guest // first clear the old guests cart, to avoid duplicate abandoned cart emails $guest->delete_cart(); $new_customer = IG_ES_Customer_Factory::get_by_email( $new_email ); } else { // Guest is not locked so we can simply update guest email $guest->set_email( $new_email ); $guest->save(); // Set the new customer to the existing session customer $new_customer = $existing_session_customer; } } } else { // There is no session customer, so create one $new_customer = IG_ES_Customer_Factory::get_by_email( $new_email ); } } // init the new customer tracking, also saves/updates the language // if ( $new_customer ) { // self::set_session_customer( $new_customer, $language ); // } // update the stored cart if ( IG_ES_Abandoned_Cart_Options::is_cart_tracking_enabled() ) { IG_ES_WC_Carts::update_stored_customer_cart( $new_customer ); } return $new_customer; } /** * Returns the current session customer and takes into account session tracking cookies. * * @return Customer|false */ public static function get_session_customer() { global $woocommerce; if ( is_user_logged_in() ) { return ig_es_get_logged_in_customer(); } if ( ! self::session_tracking_enabled() ) { return false; } // Can't look up the customer in this situation. if ( ! isset( $woocommerce->session ) ) { return ''; } $tracking_key = $woocommerce->session->get_customer_id(); // uses the newly set key if it exists and can be set if ( $tracking_key ) { $guest = IG_ES_Guest_Factory::get_by_key( $tracking_key ); if ( $guest instanceof IG_ES_Guest ) { $customer = new IG_ES_Customer(); $customer->set_prop( 'guest_id', $guest->get_id() ); $customer->exists = true; return $customer; } } return false; } /** * Check if we can track user session */ public static function session_tracking_enabled() { if ( isset( $_COOKIE['ig_es_session_tracking_disabled'] ) ) { return false; } return true; } }