Ecommerce Shopify WordPress Discussion

wp loged in methods doesnt working like signon ord set cookie

hi everyone im trying to fix a bug in a wordpress site problme is when i try to register new users it works but they didnt logged in or it takes some time to displays a user loged in this is my func and last method i used public function verifyCode() { $phone = $this->_preparePhoneNumber(); $this->_verifyOtp( $phone ); $user = get_user_by( 'login', $phone ); $isRegistration = false; $user_password = wp_generate_password(); if ( ! $user ) { $userId = wp_create_user( $phone, $user_password, $phone . '' ); $user = new WP_User( $userId ); $user->set_role( $_POST['isVendor'] ? 'seller' : 'customer' ); $isRegistration = true; $credentials = array( 'user_login' => $phone, 'user_password' => $user_password, 'remember' => true, ); $user = wp_signon( $credentials, false ); } else { $userId = $user->ID; wp_set_current_user( $userId ); wp_set_auth_cookie( $userId ); } wp_send_json_success( [ ...$this->_getLoginResponse( $user ), 'isRegistration' => $isRegistration ] ); } by the way my login is working when user is existed but when i create a new user it doesnt logged in thnaks for your helps and wp cookie is set in my browser after register but when i try is_user_logged_in() it dosent work
You're using wp_set_current_user and wp_set_auth_cookie for existing users, but not for new users. These functions are crucial for setting the current user and there are many things that needs to fix, I have revised your code, please check public function verifyCode() { $phone = $this->_preparePhoneNumber(); $this->_verifyOtp( $phone ); $user = get_user_by( 'login', $phone ); $isRegistration = false; if ( ! $user ) { $user_password = wp_generate_password(); $userId = wp_create_user( $phone, $user_password, $phone . '' ); $user = get_user_by( 'id', $userId ); // Reload user data $user->set_role( $_POST['isVendor'] ? 'seller' : 'customer' ); $isRegistration = true; wp_set_current_user( $userId ); wp_set_auth_cookie( $userId, true ); // Set the login cookie } else { $userId = $user->ID; wp_set_current_user( $userId ); wp_set_auth_cookie( $userId ); } wp_send_json_success( [ ...$this->_getLoginResponse( $user ), 'isRegistration' => $isRegistration ] ); }

January 10, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.