Why sometimes wp_redirect() reset cookie?

Question

I’ve been trying to set cookie to mark users that get redirected before checking out because of incompleted profile. The problem is when wp_redirect is executing and new page is opened, the set cookie dissapears. Here’s my 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) ) {
      setcookie('wsk_redirected_from_cart',true,time()+3600);
      wp_redirect(bp_core_get_user_domain( $user->id ) . bp_get_profile_slug() . '/edit/group/2');
      exit;
    }
  }
});

And you can see how the cookie was set for just a short while and disappears after the new page is loaded in this short video: https://www.loom.com/share/dbb549eb44204214b78263a1dfebc21a

I’ve tried simplifying the context by doing this:

if ( $_GET['test'] ) {
  setcookie('wsk_redirected_from_cart',true,time()+3600);
  wp_redirect(home_url('/members'));
  exit;
}

This time the cookie persists even after the new page is loaded.

So I am stuck here. On what condition will a cookie get automatically deleted? Also I noticed another abnormally: In the video at 00:10 you can see the short-lived cookie’s "Expire" is session, but it is supposed to be an exact time (one hour later). In the simplified context above, the cookie’s expire is too an exact time instead of "session". Maybe this is related to the cookie’s disapparance?

0
shenkwen 4 months 2022-04-09T15:26:49-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse