Home > Software design >  Add user-id to redirect url after WooCommerce registration
Add user-id to redirect url after WooCommerce registration

Time:08-20

I'm currently using this in functions.php:

add_filter("woocommerce_registration_redirect", "wcs_register_redirect");
function wcs_register_redirect($redirect) {
  $redirect = "https://mywebsite";
  return $redirect;
}

To redirect the user after registration. If I wanted to add the user ID to the end of that string - how would I achieve that?

so it would look something like this:

add_filter("woocommerce_registration_redirect", "wcs_register_redirect");
function wcs_register_redirect($redirect) {
  $redirect = "https://mywebsite&reference-id=[Wordpress User ID]";
  return $redirect;
}

I'm assuming there may need to be another step due to no user ID having yet been created at the point of form submission...

CodePudding user response:

WooCommerce sets the user auth cookie after registration, so you can get the user ID by using get_current_user_id()

add_filter("woocommerce_registration_redirect", "wcs_register_redirect");
function wcs_register_redirect($redirect) {
  $user_id  = get_current_user_id();
  $redirect = "https://mywebsite&reference-id=" . $user_id;
  return $redirect;
}
  • Related