skip to Main Content

I am building an online course website with WordPress + Buddyboss + Woocommerce + Learndash. Due to our policy, all users are required to complete their profile before they can place an order. I was able to achieve this with the following code:

add_action('woocommerce_before_checkout_form',function(){
  if (is_user_logged_in()){
    $user = wp_get_current_user();
    if ( bpprocn_has_incomplete_profile($user->id) ) { //check if profile is completed
      if (wp_redirect(bp_core_get_user_domain( $user->id ) . bp_get_profile_slug() . '/edit/group/2')) exit; //redirect user to profile edit page
    }
  }
});

Now, after users finish editing their profile and click "save", I need to redirect them back to their cart. I was able to build a custom action that triggers after the "save profile" button is rendered and use this action to show a link next to the "save profile" button.

add_action('wsk_add_continue_shop_button',function(){
    $user = wp_get_current_user();
    if (WC()->cart->get_cart_contents_count() && !bpprocn_has_incomplete_profile($user->id)) echo '<a class="uk-button" href="/cart">continue shopping</a>';  // if user's cart is not empty and profile is completed, show the "continue shopping" link.
  });

This is of course not the best solution because it relies on users to click the link, and if some users overlook the newly added link, their user experience will be very bad. So I definitely need to automatically redirect users back to their cart once they complete their profile and click "save profile".

I can’t simply do wp_redirect() in the above code, because that would redirect those users who have completed their profile, have something in their cart and get to the profile editing page, maening these users will never see the profile editing page unless they clear their cart.

My knowledge of PHP and WordPress is quite limited, so I need some advice here. One thing I can think of is to check if $_POST is empty, if it is not empty then it probably means the user just submit some changes or new infos of their profile, and in these cases I can do wp_redirect. But is this approach reliable? If a user just wants to edit his profile and he has something in the cart, he will be redirected too, which is unwanted.

Another question I can think of is, when I do the first redirection ( when users try to place an order, redirect them to their profile editting page if their profile is incompleted ), is there a way to mark this user as "This user has just tried to place an order and been redirected" so he can be distinguished from those users who didn’t try to place an order and just want to edit their profile? It seems to me to do this I need to tap into session data, but I have close to zero knowledge about session manipulation… Does wordpress has anything helpful in this aspect?

2

Answers


  1. Chosen as BEST ANSWER

    I've thought of a way to solve this. It seems working, need more test though.

    add_action('woocommerce_before_checkout_form',function(){
      if (is_user_logged_in()){
        $user = wp_get_current_user();
        if ( bpprocn_has_incomplete_profile($user->id) ) { //check if profile is completed
          if (wp_redirect(bp_core_get_user_domain( $user->id ) . bp_get_profile_slug() . '/edit/group/2?redirected=wsk_checkout')) exit; //redirect user to profile edit page with a GET parameter
        }
      }
    });
    

    If user tries to checkout without a completed profile, they will get redirected to the profile editing page with a GET parameter. On this page, if they complete their profile and save changes, a "profile_update" action will trigger and the profile editing page reloads with a HTTP_REFERER that contains the previously set GET parameter:

    add_action('profile_update',function(){
      $user = wp_get_current_user();
      if (WC()->cart->get_cart_contents_count() && !bpprocn_has_incomplete_profile($user->id) && str_contains($_SERVER['HTTP_REFERER'],'wsk_checkout')) { 
          wp_redirect('/checkout');
          exit;
        }
      });
    

    The above code checks if the user's cart is not empty and his profile is completed and the GET parameter exists in HTTP_REFERER, if yes, redirect them back to checkout page.

    This way, users simply trying to update their profile and happen to have something in the cart won't get redirected to the checkout page because they won't have the GET parameter.


  2. Maybe you can use the the original WordPress Event – profile_update.
    It fires directly after an existing user is updated.

    add_action( ‘profile_update’, ‘checkUserUpdate’, 10, 1 );

    function checkUserUpdate( $user_id ) {
    // check if your user profile ist complete and your WC cart ist not empty and redirect him to the cart
    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search