8

I am trying to redirect user after Woocommerce registration. I have tried everything and it is not working.

I have tried some methods found on internet but they didn't work…

When I change 'myaccount' to another permalink it just freezes when you click register.. not sure why.

wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'welcome' ) ) 

I even tried with the page id

wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( '1072' ) ) 

Any help?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
John B.
  • 161
  • 1
  • 4
  • 12

6 Answers6

26

Important update (working on last version 3.2.x)


First the woocommerce_registration_redirect is a filter hook, but NOT an action hook.

A filter hook has always at least one argument and always require to return something. It can be the main function argument (the first one) or some custom value.

The correct tested and functional code is:

add_filter( 'woocommerce_registration_redirect', 'custom_redirection_after_registration', 10, 1 );
function custom_redirection_after_registration( $redirection_url ){
    // Change the redirection Url
    $redirection_url = get_home_url(); // Home page

    return $redirection_url; // Always return something
}

Code goes in function.php file of your active child theme (or theme)...


Some common redirections urls used in woocommerce:

  • Get the "Home" page Url: $redirection_url = get_home_url();
  • Get the "Shop" page url: $redirection_url = get_permalink( wc_get_page_id( 'shop' ) );
  • Get the "Cart" page Url: $redirection_url = wc_get_cart_url();
  • Get the "Checkout" page Url: $redirection_url = wc_get_checkout_url();
  • Get the "My account" or registration page Url:
    $redirection_url = get_permalink( wc_get_page_id( 'myaccount' ) );
  • Get other post or pages by ID: $redirection_url = get_permalink( $post_id );
    (where $post_id is the id of the post or the page)
  • Get post or pages by path (example): $redirection_url = home_url('/product/ninja/');
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Thank you for this, searched all day and this was the best solution I've seen! – Daniel May 13 '21 at 17:26
  • @LoicTheAztec After registration from registration form on account page , I want user to auto-login and directly enters to the Dashboard page rather than going back to login page. – Ali Raza Mar 09 '22 at 09:54
17

The accepted answer didn’t work for me. What worked for me was this:

// After registration, logout the user and redirect to home page
function custom_registration_redirect() {
    wp_logout();
    return home_url('/');
}
add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);
borisdiakur
  • 10,387
  • 7
  • 68
  • 100
  • This will fail if you deactivate Woocommerce – Tim Hallman Nov 28 '16 at 20:32
  • 1
    Why would you want to log the user out after registration? – Claytronicon Mar 12 '18 at 19:47
  • 1
    @claytronicon We needed to implement an approval mechanism at that time. The user needed to be approved first before being able to login. `wp_logout()` deleted the user session directly after it was created. – borisdiakur Mar 13 '18 at 19:26
  • It seems the redirect doesn't work when pointed to other than home's domain, even to it's subdomain. Falls back to my-account/ – Fanky Dec 29 '20 at 18:07
1

You want to use a filter like this:

function plugin_registration_redirect() {
    return home_url( '/page-to-show' );
}

add_filter( 'registration_redirect', 'plugin_registration_redirect' );

Or, specifically to your code:

function plugin_registration_redirect() {
    $url = wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'welcome' );
    return $url;
}

add_filter( 'registration_redirect', 'plugin_registration_redirect' );
Tim Hallman
  • 854
  • 15
  • 27
1

WP WooCommerce Redirect is a WordPress plugin to redirect your WooCommerce website after register or login! You can set any custom page or custom redirect according to user role.

You can set user login and register redirection page without any codding knowledge to your WooCommerce website . Your customer reduce time for using this plugin and get their destination easily.

I have developed a plugin for this issue. Also given bellow of raw code for redirect without any plugin.

//Redirect users to custom URL based on their role after login
function wp_woo_custom_redirect( $redirect, $user ) {

// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'my-account' ) );

if( $role == 'administrator' ) {

    //Redirect administrators to the dashboard
    $admin_redirect = get_option('admin_redirect');
    $redirect = $admin_redirect;
} elseif ( $role == 'shop-manager' ) {

    //Redirect shop managers to the dashboard
    $shop_manager_redirect = get_option('shop_manager_redirect');
    $redirect = $shop_manager_redirect;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {

    //Redirect customers and subscribers to the "My Account" page
    $customer_redirect = get_option('customer_redirect');
    $redirect = $customer_redirect;
} else {

    //Redirect any other role to the previous visited page or, if not available, to the home
    $redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wp_woo_custom_redirect', 10, 2 );

If you feel comfort to work with plugin or without code? You can download and install my plugin "WP WooCommerce Redirect"

csehasib
  • 365
  • 3
  • 7
1

Add this filter in theme function file.

function filter_woocommerce_registration_redirect( $var ) { 
    // make filter magic happen here... 
    return get_page_link(3598); // 3598 id page id.
}; 

Add the filter:

add_filter( 'woocommerce_registration_redirect', 
    'filter_woocommerce_registration_redirect', 10, 1 );
clemens
  • 16,716
  • 11
  • 50
  • 65
Rohit Ghoghari
  • 321
  • 3
  • 4
0

If anyone is looking for the Login redirection version that works for the woocommerce my-account login:


add_filter('woocommerce_login_redirect', 'hs_login_redirect');
function hs_login_redirect( $redirection_url ) {
    $redirection_url = get_home_url();
    return $redirection_url;
}
alamoot
  • 1,966
  • 7
  • 30
  • 50