• Resolved jtm12

    (@jtm12)


    I was wondering if anyone had a suggestion for a way around a redirect conflict between woocommerce and WishList Member.

    I would be happy to get general suggestions for going a different direction as opposed to code. But code is welcome, too.

    I have WishList Member installed on a development site to protect some medical products that are for sale in a woocommerce store.

    Doctors can register through WishList Member and log in to see products that require a prescription and are protected by the plugin. Doctors also have acccess to other protected material.

    General shoppers can buy over-the-counter products that show up to all visitors.

    WishList Member’s redirect functionality by level was one of the things I liked about it. But now it has become a problem for non-doctor customers.

    Doctors are already logged in when they access their shop, so they are not the problem.

    WishList overrides all login redirects; this includes the returning customer login redirect on the woocommerce checkout page.

    I can turn the override off in the WishList Member dashboard, but then I have to do the WL login redirects manually (I don’t have the skill).

    If WishList Member’s login redirect override is turned on, I can’t get my returning shopper back to the checkout page after logging in.

    I can get rid of the checkout page “Returning Customer Click Here to Log in” box in woocommerce, but that just creates a new problem. Checkout fields don’t automatically populate, and the order isn’t linked to a returning customer if I don’t ask the customer to log in.

    If I try a plugin such as Pete’s Login Redirect, it doesn’t even see the WishList Member’s levels to set the redirects by hand.

    Based on my skill level, I think I need to keep the WishList Member redirect on for membership levels, and then figure out another way to get the woocommerce shopper to log in on checkout and keep going in the shopping process.

    I have contacted WL. This is outside the scope of plugin support.

    Since you know the woocommerce logic so well, is there another way to get the returning customer to log in and get back to the checkout page? Perhaps something other than a redirect function?

    Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I do not have WishList Member plugin to test the complete scenario, but I have tested the following code to redirect only customer user role to Checkout page after login. Place this snippet in your theme’s functions.php file and let me know how it goes:

    add_filter('woocommerce_login_redirect', 'wcs_customer_redirect', 10, 2);
    
    function wcs_customer_redirect( $redirect, $user ) {
        // Check if current user is a customer
        if ( in_array( 'customer', $user->roles ) ) {
            // Set the inteded redirect URL to your own site's checkout page.
         	$redirect = home_url() . '/checkout/';
        }
        return $redirect;
    }
    • This reply was modified 8 years, 3 months ago by Prasad Nevase. Reason: trimmed extra spaces
    Thread Starter jtm12

    (@jtm12)

    Thank you very much for the code.

    I’m sorry to say it did not override the WishList Member redirect when a returning woo customer tried to log in on the checkout page. The customer went to the home page.

    I tried upping the priority number in your code to 999. No change.

    I studied the WL Member code after searching for login_redirect. It appears to be sending non-admins and those who are not WL members to the home page upon login. It is also sending WL members who are not assigned to a level to the home page. And it is taking all woo customers who register and treating them as WL members without a level.

    I don’t find any code in WL Member similar to yours. I was looking for some sort of priority number that I could outdo.

    I went into the code in WL Member and tried to manually change those redirects to the checkout page, and that had no effect either. Perhaps I wasn’t altering the correct file. And that’s not a solution. I just wanted to see if I could accomplish something.

    I’ll continue working on this.

    Thank you for your help. I’ve learned a lot sorting through this mess.

    Instead of increasing the priority number to 999 can you try reducing it to 5, then 4 then 3 like that & test if it works? Because lower numbers correspond with earlier execution.

    Also, I had one question – whether both Doctors and other customers get the same role – “Customer” ?

    Thread Starter jtm12

    (@jtm12)

    I tried reducing the priority number all the way down to 1 and never succeeded.

    To answer your question: WishList assigns the doctors the role of subscriber, but they also are assigned to the level of ‘Doctors.’

    I’m now thinking it might be easier to go the other way, turning off WishList’s login redirect function and doing the doctors’ login redirect myself. That way, woocommerce will handle the checkout login redirect correctly.

    I have one piece of code working that checks WishList users. In the woocommerce cart empty template, I needed to make the “return to shop” link conditional on the user.

    The following works:

    
    <?php global $woocommerce;
        if( is_cart() && WC()->cart->cart_contents_count == 0){
          
    // get the current user level.
    $user = wp_get_current_user();
    
    // Get user levels from WishlistMembers
    $levels = WLMAPI::GetUserLevels($user->ID);
    
    //then run the check for the level you want
    if(in_array('Doctors', $levels)){ ?>
    
    	// link to store for doctors	
    	
    	<?php
    
         } else { ?>
    
    	// link to store for general public	
    
    <?php
         }
    
    }
    
    ?>  
    

    So I tried to combine that with the simplest login_redirect code I could come up with; this would redirect doctors to their landing page anytime they came to the site. But it is not working the way I have it built.

    
    add_filter( 'login_redirect', 'my_login_redirect', 10, 2 );
    
    function my_login_redirect( $redirect, $user ) {
    
    	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    
    	$current_user = wp_get_current_user();
    
    	$levels = WLMAPI::GetUserLevels($current_user->ID);
    
    		if ( in_array('Doctors', $levels ) ) { 
      
       		  	$redirect = home_url() . '/doctors/';
    			}
    		return $redirect;
    
    	}
    }
    
    Thread Starter jtm12

    (@jtm12)

    This is working. Next, I’m going to try to add in checking if the user is an admin:

    
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
    
    function my_login_redirect( $redirect_to, $request, $user ) {
    
    	$levels = WLMAPI::GetUserLevels($user->ID);
    
    		if ( in_array('Doctors', $levels ) ) { 
    		$url = home_url( "/doctors/" );
     		return esc_url( $url );
    
    		}
    }
    
    Thread Starter jtm12

    (@jtm12)

    The final solution I came up with was to turn off WishList Member’s login redirect override, which seems to have the final say on redirects over other WordPress functions. The woocommerce store now correctly redirects returning customers who log in on the checkout page back to the checkout page to continue their purchase.

    This forced me to write a login redirect for my one WishList member level while also sending admins (who automatically are members of WL levels) to the admin dashboard.

    This is what I came up with and it’s working. It may not be the best way to do this:

    
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
    
    function my_login_redirect( $redirect_to, $request, $user ) {
    
    	$levels = WLMAPI::GetUserLevels($user->ID);
    
    	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    	
    		if ( in_array( 'administrator', $user->roles ) ) {
    			$url = home_url( '/wp-admin/' );
    			return $url;
    		} elseif  ( in_array('Doctors', $levels ) ) { 
    			$url = home_url( '/doctors/' );
     			return $url;
     		} else {
    			return home_url();
    
    		}
    	}
    
    }
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Returning customer login redirect conflict on checkout page’ is closed to new replies.