0

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! :-)

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.. Jan 20 '20 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.com/woocommerce-separate-login-registration. So basically what I have now is a shortcode specifically for the Registration part. Jan 20 '20 at 20:03
  • did you see my answer? does it work for you? Jan 22 '20 at 10:17
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,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.