PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, April 9, 2022

[FIXED] What is the general practice to redirect users back to their shopping cart after a certain criteria is matched

 April 09, 2022     php, session, wordpress     No comments   

Issue

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?


Solution

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 }



Answered By - J P
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing