How can I redirect the user to a custom URL?

Top WooCommerce & WordPress Plugins Forums Email Verification for WooCommerce How can I redirect the user to a custom URL?

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #101528
    Gavin Mair
    Participant

    Hi,

    I have email verification turned on. Currently the user is redirected to ‘/login’ after checkout. Do you know how I can change the redirect to go to a custom URL (‘/welcome’ in my case)?

    Please let me know if you require more details.

    Kind regards,

    Gavin

    #101529
    Gavin Mair
    Participant

    Hi,

    An update: I am using the following PHP snippet to redirect users back to a custom login form if they log out (instead of the default WooCommerce one at ‘/my-account’). I need a way to change the redirect to a custom URL after the user has completed the WooCommerce checkout. I want to send them to a ‘/welcome’ page where I have a custom message asking them to check their email for a verification link.

    add_action('wp_logout', 'redirect_after_logout');
    function redirect_after_logout() {
        $loginURL = get_site_url() . "/login";
        wp_redirect($loginURL);
        exit();
    }
    
    #101530
    Zohaib
    Participant

    Hi Gavin,

    In the plugin’s settings, Advanced >  Prevent login after register option has a feature to redirect users.

    You can also use the Prevent login after checkout feature to logout users who purchase and show them an activation notice on the “Thank you” page.

    Please let me know if both options are not suitable.

    #101531
    Gavin Mair
    Participant

    Hi Zohaib,

    Thank you for getting back to me.

    I’ve disabled a ‘thank you’ page customisation that was interfering with this as it’s not longer needed. I have the following settings on the ‘Email Verification > ‘Advanced’ section.

    • The redirect for ‘Prevent login after register’ is successfully going to a custom redirect URL of ‘/welcome’.
    • After blocking the ‘thank you’ page, users are now redirected to the WooCommerce ‘/my-account’ dashboard (where the login form appears) once they have completed checkout. I now need to change this so they are instead sent to ‘/welcome’.
    #101532
    Gavin Mair
    Participant

    Hi Zohaib,

    I unblocked the WooCommerce order received (‘thank you’) page and was able to redirect to a custom URL by using the  &alg_wc_ev_activate_account_message=323 query parameter that is added when email verification is pending after checkout.

    Hopefully this is a good way to go about this.

    Here is the PHP snippet for other users.

    /*
        About: Email Verification (WPFactory) | Redirect to a custom page from the order received page after WooCommerce checkout.
        Author: Toby
        Date: 03/03/2022
    */
    
    function td_redirect_when_awaiting_email_verification_on_order_received_page() {	
    // Inspect URL query parameters to detect if verification is pending. 
        global $wp;
        $page_url_with_query_params = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
        $awaiting_verification = str_contains($page_url_with_query_params, "&alg_wc_ev_activate_account_message=323");
    	
        if (is_order_received_page() && $awaiting_verification) {
    	// Do redirect.
            redirect_to_page("welcome");
        }	
    }
    
    // Redirect to a page given its slug.
    function redirect_to_page($pageSlug) {
        $URL = get_site_url() . "/$pageSlug";
        wp_redirect($URL); 
        exit; // wp_redirect() does not exit / die automatically.
    }
    
    // Register the action.
    add_action('template_redirect', 'td_redirect_when_awaiting_email_verification_on_order_received_page');
    
    #101533
    Gavin Mair
    Participant

    It seems that the number in &alg_wc_ev_activate_account_message= changed from 323 to 327. I’m not sure why. What’s the correct number for the following message?

    ‘Thank you for your registration. Your account has to be activated before you can log in. Please check your email.’

    #101534
    Pablo
    Moderator
    Plugin Support

    Hi Gavin,

    I didn’t test your code yet, but the number after ?alg_wc_ev_activate_account_message= is supposed to be the user ID.

    #101535
    Gavin Mair
    Participant

    Hi Pablo,

    Oh, it’s the user ID. In that case I’ll just check for the presence of the query parameter (by name) but not take into account its value. That way it will always work.

    Please let me know if you spot any flaws in my approach if you test the code.

    #101536
    Pablo
    Moderator
    Plugin Support

    I believe there could be a simpler way of getting what you want. If you just want to redirect after checkout, we have a filter for that. This is what you could try:

    add_filter( 'alg_wc_ev_redirect_after_checkout', function( $redirect_to, $user_id ){
    	$redirect_to = get_site_url() . '/welcome';
    	return $redirect_to;
    }, 11, 2 );
    
    #101537
    Gavin Mair
    Participant

    Hi Pablo,

    Ah, you have a filter for exactly that! Super. Thank you for sending that to me. I had to get something working today but I’ll give that snippet a go.

    Does you team plan to write a complete reference to the plugin’s actions and filters etc.? I’d love to see that.

    Great job on this plugin. I got it this week and it was very easy to configure. 🙂

    Thank you for yours and Zohaib’s help.

    Enjoy your weekend!

    #101538
    Pablo
    Moderator
    Plugin Support

    Anytime,

    Thanks for the kind words. We’d appreciate if you could leave a review here or here if you are enjoying the plugin/support 🙂

    We have the developers and FAQ tabs with some info but I know it’s just a little. We have plans to update it over time. By the way, I’m going to update it now with the filter I just sent you 😉

    See you. Have a good weekend as well!

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.