WP / WC missing argument in WooCommerce function

I have Wordpress installed with WooCommerce, and I'm trying to use this code to log in to the administrator:

if ( !is_user_logged_in() ) { $user = get_userdatabylogin( $username ); // get_user_by('login', $user_login); $id = $user->ID; wp_set_current_user( $id, $user->user_login ); wp_set_auth_cookie( $id ); do_action( 'wp_login', $user->user_login ); } 

But it returns this error message:

Warning: missing argument 2 for wc_maybe_store_user_agent () in /woocommerce/includes/wc-core-functions.php plugins on line 1516

I tried to watch online, but no one seemed to have this problem.

+9
php wordpress wordpress-hook woocommerce
source share
2 answers
 if (!is_user_logged_in()) { //determine WordPress user account to impersonate $user_login = 'guest'; //get user ID $user = get_userdatabylogin($user_login); // below WP 3.3.0 //$user = get_user_by('login', $user_login); above or equals WP 3.3.0 $user_id = $user->ID; //login wp_set_current_user($user_id, $user_login); wp_set_auth_cookie($user_id); do_action('wp_login', $user_login); } 

There is such a hook in WooCommerce.

 add_action( 'wp_login', 'wc_maybe_store_user_agent', 10, 2 ); 

" Wc_maybe_store_user_agent " expects 2 parameters. If the user ID is not set, it will show the error you mentioned. Check out my snippet above

The function is on line 1516 in the plugins /woocommerce/includes/wc-core-functions.php

 function wc_maybe_store_user_agent( $user_login, $user ) { if ( 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) && user_can( $user, 'manage_woocommerce' ) ) { $admin_user_agents = array_filter( (array) get_option( 'woocommerce_tracker_ua', array() ) ); $admin_user_agents[] = wc_get_user_agent(); update_option( 'woocommerce_tracker_ua', array_unique( $admin_user_agents ) ); } } add_action( 'wp_login', 'wc_maybe_store_user_agent', 10, 2 ); 
+10
source share

You have missed one option. Try this.

 do_action( 'wp_login', $user->user_login, $user ); 

Source: https://developer.wordpress.org/reference/hooks/wp_login/

+6
source share

All Articles