• JCM

    (@chrisjeret)


    hello, I have a webpage with login and registration and also a product page. I have created the login/registration page using ultimate member plugin and created product page with woo-commerce plugin.

    How do I get a user when trying to buy a product to login/register first.

    Any help would be much appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
    1. In the WordPress dashboard, go to WooCommerce → Settings → Checkout.
    2. Untick the ‘Enable guest checkout’ box. This will force users to create an account when they buy from your WooCommerce store.
    Thread Starter JCM

    (@chrisjeret)

    Hi @abretado1985 , When i do this then the login and registration page uses the WooCommerce respectively. On my webpage there is already a Registration and Login page created with Ultimate Member plugin.

    I’m still unclear of this setup … So does that mean WooCommerce login/registration page is separate from the Ultimate Member plugin pages i have created?

    • This reply was modified 1 year, 7 months ago by JCM.

    Give this a try. Add to your functions.php file or create a custom plugin. Make sure to update the Ultimate Member login and register page names:

    function redirect_woocommerce_to_ultimate_member() {
        // Ultimate Member login page URL
        $login_page = get_permalink( get_page_by_title('Login') ); // Change 'Login' to your login page title
    
        // Ultimate Member registration page URL
        $register_page = get_permalink( get_page_by_title('Register') ); // Change 'Register' to your registration page title
    
        if (is_account_page() && !is_user_logged_in()) {
            // Redirecting to Ultimate Member login page if it's WooCommerce account page and user is not logged in
            wp_redirect( $login_page );
            exit;
        }
    
        if (is_checkout() && !is_user_logged_in()) {
            // Redirecting to Ultimate Member registration page if it's WooCommerce checkout page and user is not logged in
            wp_redirect( $register_page );
            exit;
        }
    }
    
    add_action( 'template_redirect', 'redirect_woocommerce_to_ultimate_member' );
    
    Thread Starter JCM

    (@chrisjeret)

    @abretado1985 ,

    So i have added the code. Now when i checkout as a visitor it opens up the Register form which i created in Ultimate Member but it shows the homepage and not the checkout product page. So i have to do a checkout again and this time it shows the woo-commerce registration form.

    • This reply was modified 1 year, 7 months ago by JCM.

    Replace the previous code with the one below. It should get the current URL as a query parameter and redirect back to it once registered or logged in.

    function redirect_woocommerce_to_ultimate_member() {
        if (is_checkout() && !is_user_logged_in()) {
            // Get current checkout URL
            $checkout_url = add_query_arg( array() );
    
            // Choose between Ultimate Member login or registration page
            $ultimate_member_page_title = is_wc_endpoint_url('order-pay') ? 'Login' : 'Register'; // Change 'Login' and 'Register' to your page titles
    
            // Ultimate Member page URL
            $ultimate_member_page_url = get_permalink( get_page_by_title($ultimate_member_page_title) );
            
            // Appending the current checkout URL as a query parameter
            $redirect_url = add_query_arg( 'redirect_to', urlencode( $checkout_url ), $ultimate_member_page_url );
    
            // Redirecting to Ultimate Member page
            wp_redirect( $redirect_url );
            exit;
        }
    }
    add_action( 'template_redirect', 'redirect_woocommerce_to_ultimate_member' );
    
    function um_redirect_after_login_or_registration( $url, $user ) {
        if ( isset( $_REQUEST['redirect_to'] ) ) {
            $url = urldecode( $_REQUEST['redirect_to'] );
        }
        return $url;
    }
    add_filter( 'um_registration_login_url', 'um_redirect_after_login_or_registration', 10, 2 );
    add_filter( 'um_login_redirect_url', 'um_redirect_after_login_or_registration', 10, 2 );
    

    To prompt users to login or register before buying a product on your webpage, you can follow these steps:

    1. Enable User Registration: Make sure you have the user registration feature enabled in the Ultimate Member plugin or any other registration plugin you are using.
    2. Set WooCommerce to Require Login: In WooCommerce settings, you can enable the option to require users to be logged in before making a purchase. To do this, go to WooCommerce > Settings > Accounts & Privacy and check the box that says “Enable customer registration on the “checkout” page.”
    3. Redirect Non-Logged-In Users: You can use a plugin or custom code to redirect users who are not logged in to the login or registration page when they try to access the product page or the checkout page. There are various plugins available for this purpose, or you can implement custom code based on your specific needs.
    4. Customize Error Messages: You may want to customize the error messages that users see when prompted to login or register before purchasing a product. This will make the process more user-friendly and clear.
    5. I Have done this same thing on IMG helping hands you can see there.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to restrict access to certain page’ is closed to new replies.