Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

php - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...