Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce redirect after registration

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?

like image 284
John B. Avatar asked May 19 '15 22:05

John B.


2 Answers

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/');
like image 72
LoicTheAztec Avatar answered Oct 19 '22 08:10

LoicTheAztec


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);
like image 27
borisdiakur Avatar answered Oct 19 '22 10:10

borisdiakur