Update Session => Db

Hi,

Does CS have some built-in function to 'force' update session => db?

Something like:

fn_update_db_session($cart['user_data']['email'], 'email@example.com'); // one post

OR

fn_update_db_session(); // whole session 

/gabbo

If I understand you correctly, you can use the following code:

Tygh::$app['session']['cart']['user_data']['email'] = "email@example.com";

or

Tygh::$app['session']['cart'] = array();

If I understand you correctly, you can use the following code:

Tygh::$app['session']['cart']['user_data']['email'] = "email@example.com";

or

Tygh::$app['session']['cart'] = array();

Hi,

I have not tryed that, do you know if it updates db to, or just $_SESSION?

I'll try it later when I come home..

/gabbo

Hi,

I have not tryed that, do you know if it updates db to, or just $_SESSION?

I'll try it later when I come home..

/gabbo

This code updates only session.

This code updates only session.

Ok, do you know any funktion to write session to db?

/gabbo

Ok, do you know any funktion to write session to db?

/gabbo

Could you please give more details about your task?

Session is saved to DB on every page completion. cscart_sessions.

Session is saved to DB on every page completion. cscart_sessions.

Right. But then the cart session in restored, the fn_calculate_cart_content function is called. It can override all changes

Depends on whether he wants the 'cart' object or something else (he's not being specific about what he wants to retrieve). I.e. he may be storing some value like a page count, referer, etc. and wants to know how he can look it up asynchronously (like query to see what sessions came from a particular referer or IP or something - of course I'm guessing here). But you're right, the cart is recalculated. But don't know if it's the saved cart he's after. :-)

Session is saved to DB on every page completion. cscart_sessions.

Sorry all.. my fault. I found why, and now it works.

I'm about to upgrade my CS 422 to 439 and I'm using klarna checkout paymentprocessor (Iframe) and I decide to rewrite my paymentscript for the latest API. My problem was that the email address I wrote in checkout (step one) was not saved in the session (db) (to be reloaded in the callback later)... I thought... but I had missed one thing (I wrote this script 2-3 years ago) :-)

In my callbackscript I have:

  if ($klarna_order['status'] == "checkout_complete") {
    // Restart session
    if (!empty($klarna_sess_id)) {
        Tygh::$app['session']->resetID($klarna_sess_id);
        fn_payments_set_company_id(0, Tygh::$app['session']['settings']['company_id']['value']);
        Tygh::$app['session']['cart'] = empty(Tygh::$app['session']['cart']) ? array() : Tygh::$app['session']['cart'];
        $cart = & Tygh::$app['session']['cart'];
        Tygh::$app['session']['auth'] = empty(Tygh::$app['session']['auth']) ? array() : Tygh::$app['session']['auth'];
        $auth = & Tygh::$app['session']['auth'];
    }
    
    // Debug
    fn_gab_writelog('Restart session: $cart in klarna_callback.php', $cart);
    fn_gab_writelog('Restart session: $auth in klarna_callback.php', $auth);    

    // Save cs-cart orginal e-mailaddress for later use before klarna customer data update $cart['user_data']
    $user_email = $cart['user_data']['email'];
    
    // Loading customer data from klarna and update session. Note: Only active profilefields is in session.
    $klarna_user_data = fn_gab_get_userinfo($klarna_order);
    Tygh::$app['session']['cart']['user_data'] = $klarna_user_data;
    
    ...

And in function fn_gab_get_userinfo

function fn_gab_get_userinfo($klarna_order) {
    $user_data = array(
        'firstname' => !empty($klarna_order['billing_address']['given_name']) ? $klarna_order['billing_address']['given_name'] : '',
        'lastname' =>  !empty($klarna_order['billing_address']['family_name']) ? $klarna_order['billing_address']['family_name'] : '',
        //'email' => !empty($klarna_order['billing_address']['email']) ? $klarna_order['billing_address']['email'] : '', // låt kund behålla separat inloggnings e-post
        //'company' => '',
        'phone' =>  !empty($klarna_order['billing_address']['phone']) ? preg_replace('/\s+/', '', $klarna_order['billing_address']['phone']) : '',
        //'fax' => '',
    ...

Oops! I replaced user_data array without 'email' :-)

/gab