Can’t see cookie after page reload

I use ajax in my wordpress theme to add product to cart. This is the php function:

function updateCart(){
    $cart = array();
    $prodID = "";
    $response = array();
    $cookie_name = 'cart';
    if(isset($_REQUEST['prodID'])){
      $prodID = $_REQUEST['prodID'];
    }else{
      $prodID = false;
    }

    if(isset($_COOKIE['cart'])){
       $cart = json_decode( stripcslashes($_COOKIE[$cookie_name]), true);
       array_push($cart, $prodID);
    if(setcookie( $cookie_name, json_encode($cart, true), time() + (24 * 60   * 60), COOKIEPATH, COOKIE_DOMAIN )){
     $response['cookie'] = 'cart_exist';
     wp_send_json_success( $response );
    }else{
        wp_send_json_error( );
      }
    }else{
if($prodID){
  array_push($cart, $prodID);
}

if(setcookie( $cookie_name, json_encode($cart, true), time() + (24 * 60 * 60))){
  $response['cookie'] = 'cart_not_exist';
  $response['cart'] = $_COOKIE['cart'];
  wp_send_json_success( $response );
}else{
      wp_send_json_error( );
    }
   }
  }

After page reload i can’t see and access the $_COOKIE[‘cart’]. On desktop version of my site this code work perfect. This only happens on mobile vesrion (i use another version of wordpress theme for mobile site).

Related posts