admin管理员组

文章数量:1287963

I am trying to change the WooCommerce Registration button redirect link. This is the code I use for the redirect (which works just fine):

function custom_registration_redirect() {
        if(is_user_logged_in()) {
            return home_url('/checkout/');
        }
    }

add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);

The issue is: I need to change the redirect based on the page the user is actually on, so I tried doing this:

add_action('wp', 'page_check');
function page_check() {
if (is_page(29179)) {
    function custom_registration_redirect() {
        if(is_user_logged_in()) {
            return home_url('/checkout/');
        }
    }
    add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);
}

but for some reason it doesn't work... I know I'm probably missing something here but I don't know what...

Thanks in advance for your help! :-)

I am trying to change the WooCommerce Registration button redirect link. This is the code I use for the redirect (which works just fine):

function custom_registration_redirect() {
        if(is_user_logged_in()) {
            return home_url('/checkout/');
        }
    }

add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);

The issue is: I need to change the redirect based on the page the user is actually on, so I tried doing this:

add_action('wp', 'page_check');
function page_check() {
if (is_page(29179)) {
    function custom_registration_redirect() {
        if(is_user_logged_in()) {
            return home_url('/checkout/');
        }
    }
    add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);
}

but for some reason it doesn't work... I know I'm probably missing something here but I don't know what...

Thanks in advance for your help! :-)

Share Improve this question asked Jan 19, 2020 at 12:47 Sylvain RoulinSylvain Roulin 1 3
  • Please it's not clear: where is the register form placed ? in a widget? because otherwise you should exactly know the page you're in..normally the registration form is already in checkout page.. – Andrea Somovigo Commented Jan 20, 2020 at 13:30
  • Thanks for the reply! It is placed on a page that I created and then I separated the Registration form from the Login form, by following this tutorial: businessbloomer/woocommerce-separate-login-registration. So basically what I have now is a shortcode specifically for the Registration part. – Sylvain Roulin Commented Jan 20, 2020 at 20:03
  • did you see my answer? does it work for you? – Andrea Somovigo Commented Jan 22, 2020 at 10:17
Add a comment  | 

1 Answer 1

Reset to default 1

The woocommerce_registration_redirect is a filter hook, but NOT an action hook. I've tried the shortcode provided in the mentioned link in your comment above and it works for me in this way:

function AS_redirection_after_registration($redirection_url) {

   if($redirection_url =="first-page-slug") // $redirection_url is the slug of the current page where the shortcode is placed
    return home_url('/my-page');
   elseif($redirection_url=="second-page-slug")
    return home_url('/my-second-page');

  return home_url('/checkout/');
}
add_filter( 'woocommerce_registration_redirect', 'AS_redirection_after_registration', 10, 1 );

So the $redirection_url parameter passed to the filter gives you the current page slug where the shortcode is placed and, basing on that, you can decide where to redirect to after registration. Remember to always return something when using filter hooks,

本文标签: WooCommerce Registration redirect based on page ID