Home » Php » WordPress Exchange session details between ajax-calls and normal functions/request

WordPress Exchange session details between ajax-calls and normal functions/request

Posted by: admin October 26, 2017 Leave a comment

Questions:

I am facing issue while getting session data by AJAX Call

I have 6 session variables 2 are set by AJAX and 4 are set By normal request
but when I call AJAX request then I only get 2 session variables which are set by AJAX other 4 variable get null and if I call normal page request then I get 4 session values which are set by normal pages/request and other 2 are null.

All the 6 session values or getting by AJAX call on development site but problem occurs on live site where some session values or getting some or not also my live site has https protocol

Any help would pr Appreciated
JAVASCRIPT CODE:-

jQuery(document).on('click', ".update_cart" , function(e) {


    $cart_product=jQuery(this).parent();

    var data = { action: 'update_cart',
          donation: jQuery(".donation").val(),
          donation_amount: jQuery(".donation_amount").val(),
    };  
    jQuery(".full_loader_circle").removeClass("hide");
   jQuery.post(ajaxurl,data,function(response) {
     var response=jQuery.parseJSON( response );

   });  

});

PHP CODE:-

function update_cart()
{

            global $wpdb;

            $table_transaction = $wpdb->prefix . "transactions";
            $table_name = $wpdb->prefix . "program_sessions";
            $table_session_classes = $wpdb->prefix . "session_classes";
            $table_classes_registrations = $wpdb->prefix . "classes_registrations";

            if(!(is_user_logged_in()))
            {
                exit();
            }

            /* update donation amount */

            //update donation value if set
           if(trim($_POST["donation"])!="")
           {

                    $_SESSION["donation"]=abs($_POST["donation"]);
                    $_SESSION["donation_amount"]=abs($_POST["donation_amount"]);

           }
           else 
           {
                unset($_SESSION["donation"]);
                unset($_SESSION["donation_amount"]);
           }

            /* item count */
            $grand_cart_items = 0;
            $table_classes_registrations = $wpdb->prefix."classes_registrations";
            if(isset($_SESSION["cart_items"]) && !empty($_SESSION["cart_items"]))
            {
                $grand_cart_items = 0;
                foreach($_SESSION["cart_items"] as $each_transaction_id)
                {
                    $conditions_classes_regirstration = " where transaction_id = ".$each_transaction_id." order by id asc";
                    $result_classes_regirstration =  wfc_fetch_tablerecords($table_classes_registrations,$columns_classes_regirstration="*",$conditions_classes_regirstration,$limit="");
                    $grand_cart_items = count($result_classes_regirstration) + $grand_cart_items;
                }
            }
}

I can get the value of donation amount but cart_items retuns null but it ok on normal request or page reload and it also getting right value in Development server but not live.

Normal SESSION RESPONSE:-

array(4) { ["category_id"]=> string(2) "30" ["program_id"]=> string(3) "146" ["transaction_id"]=> int(4092) ["cart_items"]=> array(1) { [0]=> int(4092) } }

AJAX SESSION RESPONSE

array(2) {

  ["donation"]=>
  int(5)
  ["donation_amount"]=>
  int(0)
}

expected result should be mix of both

Answers: