WordPress/WC session not carried to next page










-1















Simply trying to store and retrieve unregistered customer data from page to page in a custom checkout template. I've stripped it down to the bare bones to try to pinpoint the issue.



This



<?php
$customer = WC()->customer;
$customer->set_billing_first_name("WORKING!!!");
$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=2)
'country' => string 'US' (length=2)
'first_name' => string 'WORKING!!!' (length=10)


But then this



<?php
$customer = WC()->customer;
//$customer->set_billing_first_name("WORKING!!!");
//$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=1)
'country' => string 'US' (length=2)


Even though I should still be firmly within the same session, and therefore should get the data stored before the comments. All I did was refresh the page after commenting out those two lines.



Am I completely wrong about these methods?




Checked



  1. Environment is configured entirely correctly. Even had someone else double-check it for me. URLs, caches, etc.


  2. It does appear to work when logged in, but the vast majority of users never do so that's not very helpful.


  3. Have tried this on two different servers (one local, one remote) and have the same issue.


  4. Started fresh with a new WP+WC install, created a blank theme, functions.php that does the above on init code. Same exact issue.










share|improve this question



















  • 1





    That is a normal thing if $customer->get_id() is 0 which happens when you're not logged-in. WooCommerce stores the session identifier (the user ID or an auto-generated customer ID) in the browser's cookies, and not the superglobal $_SESSION. Try var_dump( $customer->get_billing(), $customer->get_id(), WC->session->get_customer_id() ) and see the output when you're logged-in and not.

    – Sally CJ
    Nov 11 '18 at 23:55












  • @SallyCJ ah okay so my root assumption that this stores customer data in a session is false? I am seeing the wp_woocommerce_session_xxx=yyyy cookie passed up in the request... it there a built-in way to store this customer data temporarily?

    – Randy Hall
    Nov 12 '18 at 0:02






  • 1





    Your assumption is basically true; however, the session data (other than the session identifier; e.g. cart contents) are stored in prefixwoocommerce_sessions table - e.g. wp_woocommerce_sessions. And this is same to how the WordPress authentication works, where your user ID is saved in the cookie, but the rest of your session are saved in the database. You can use WC()->session->set() to save data temporarily, which can persist across pages - but of course cookies must be enabled in the user's browser.

    – Sally CJ
    Nov 12 '18 at 0:19











  • @SallyCJ I'm seeing the same issue, using WC()->session, setting a var, then commenting out the set and refreshing the page. The value I saved and successfully returned using WC()->session->set() and WC()->session->get() is just flat out not there after navigation.

    – Randy Hall
    Nov 12 '18 at 8:27






  • 1





    Check my answer and let me know how it goes. Hopefully it works..

    – Sally CJ
    Nov 12 '18 at 10:29















-1















Simply trying to store and retrieve unregistered customer data from page to page in a custom checkout template. I've stripped it down to the bare bones to try to pinpoint the issue.



This



<?php
$customer = WC()->customer;
$customer->set_billing_first_name("WORKING!!!");
$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=2)
'country' => string 'US' (length=2)
'first_name' => string 'WORKING!!!' (length=10)


But then this



<?php
$customer = WC()->customer;
//$customer->set_billing_first_name("WORKING!!!");
//$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=1)
'country' => string 'US' (length=2)


Even though I should still be firmly within the same session, and therefore should get the data stored before the comments. All I did was refresh the page after commenting out those two lines.



Am I completely wrong about these methods?




Checked



  1. Environment is configured entirely correctly. Even had someone else double-check it for me. URLs, caches, etc.


  2. It does appear to work when logged in, but the vast majority of users never do so that's not very helpful.


  3. Have tried this on two different servers (one local, one remote) and have the same issue.


  4. Started fresh with a new WP+WC install, created a blank theme, functions.php that does the above on init code. Same exact issue.










share|improve this question



















  • 1





    That is a normal thing if $customer->get_id() is 0 which happens when you're not logged-in. WooCommerce stores the session identifier (the user ID or an auto-generated customer ID) in the browser's cookies, and not the superglobal $_SESSION. Try var_dump( $customer->get_billing(), $customer->get_id(), WC->session->get_customer_id() ) and see the output when you're logged-in and not.

    – Sally CJ
    Nov 11 '18 at 23:55












  • @SallyCJ ah okay so my root assumption that this stores customer data in a session is false? I am seeing the wp_woocommerce_session_xxx=yyyy cookie passed up in the request... it there a built-in way to store this customer data temporarily?

    – Randy Hall
    Nov 12 '18 at 0:02






  • 1





    Your assumption is basically true; however, the session data (other than the session identifier; e.g. cart contents) are stored in prefixwoocommerce_sessions table - e.g. wp_woocommerce_sessions. And this is same to how the WordPress authentication works, where your user ID is saved in the cookie, but the rest of your session are saved in the database. You can use WC()->session->set() to save data temporarily, which can persist across pages - but of course cookies must be enabled in the user's browser.

    – Sally CJ
    Nov 12 '18 at 0:19











  • @SallyCJ I'm seeing the same issue, using WC()->session, setting a var, then commenting out the set and refreshing the page. The value I saved and successfully returned using WC()->session->set() and WC()->session->get() is just flat out not there after navigation.

    – Randy Hall
    Nov 12 '18 at 8:27






  • 1





    Check my answer and let me know how it goes. Hopefully it works..

    – Sally CJ
    Nov 12 '18 at 10:29













-1












-1








-1








Simply trying to store and retrieve unregistered customer data from page to page in a custom checkout template. I've stripped it down to the bare bones to try to pinpoint the issue.



This



<?php
$customer = WC()->customer;
$customer->set_billing_first_name("WORKING!!!");
$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=2)
'country' => string 'US' (length=2)
'first_name' => string 'WORKING!!!' (length=10)


But then this



<?php
$customer = WC()->customer;
//$customer->set_billing_first_name("WORKING!!!");
//$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=1)
'country' => string 'US' (length=2)


Even though I should still be firmly within the same session, and therefore should get the data stored before the comments. All I did was refresh the page after commenting out those two lines.



Am I completely wrong about these methods?




Checked



  1. Environment is configured entirely correctly. Even had someone else double-check it for me. URLs, caches, etc.


  2. It does appear to work when logged in, but the vast majority of users never do so that's not very helpful.


  3. Have tried this on two different servers (one local, one remote) and have the same issue.


  4. Started fresh with a new WP+WC install, created a blank theme, functions.php that does the above on init code. Same exact issue.










share|improve this question
















Simply trying to store and retrieve unregistered customer data from page to page in a custom checkout template. I've stripped it down to the bare bones to try to pinpoint the issue.



This



<?php
$customer = WC()->customer;
$customer->set_billing_first_name("WORKING!!!");
$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=2)
'country' => string 'US' (length=2)
'first_name' => string 'WORKING!!!' (length=10)


But then this



<?php
$customer = WC()->customer;
//$customer->set_billing_first_name("WORKING!!!");
//$customer->save();
var_dump($customer->get_billing());
?>


Outputs this



array (size=1)
'country' => string 'US' (length=2)


Even though I should still be firmly within the same session, and therefore should get the data stored before the comments. All I did was refresh the page after commenting out those two lines.



Am I completely wrong about these methods?




Checked



  1. Environment is configured entirely correctly. Even had someone else double-check it for me. URLs, caches, etc.


  2. It does appear to work when logged in, but the vast majority of users never do so that's not very helpful.


  3. Have tried this on two different servers (one local, one remote) and have the same issue.


  4. Started fresh with a new WP+WC install, created a blank theme, functions.php that does the above on init code. Same exact issue.







php wordpress session woocommerce






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 23:13







Randy Hall

















asked Nov 8 '18 at 3:42









Randy HallRandy Hall

2,17673778




2,17673778







  • 1





    That is a normal thing if $customer->get_id() is 0 which happens when you're not logged-in. WooCommerce stores the session identifier (the user ID or an auto-generated customer ID) in the browser's cookies, and not the superglobal $_SESSION. Try var_dump( $customer->get_billing(), $customer->get_id(), WC->session->get_customer_id() ) and see the output when you're logged-in and not.

    – Sally CJ
    Nov 11 '18 at 23:55












  • @SallyCJ ah okay so my root assumption that this stores customer data in a session is false? I am seeing the wp_woocommerce_session_xxx=yyyy cookie passed up in the request... it there a built-in way to store this customer data temporarily?

    – Randy Hall
    Nov 12 '18 at 0:02






  • 1





    Your assumption is basically true; however, the session data (other than the session identifier; e.g. cart contents) are stored in prefixwoocommerce_sessions table - e.g. wp_woocommerce_sessions. And this is same to how the WordPress authentication works, where your user ID is saved in the cookie, but the rest of your session are saved in the database. You can use WC()->session->set() to save data temporarily, which can persist across pages - but of course cookies must be enabled in the user's browser.

    – Sally CJ
    Nov 12 '18 at 0:19











  • @SallyCJ I'm seeing the same issue, using WC()->session, setting a var, then commenting out the set and refreshing the page. The value I saved and successfully returned using WC()->session->set() and WC()->session->get() is just flat out not there after navigation.

    – Randy Hall
    Nov 12 '18 at 8:27






  • 1





    Check my answer and let me know how it goes. Hopefully it works..

    – Sally CJ
    Nov 12 '18 at 10:29












  • 1





    That is a normal thing if $customer->get_id() is 0 which happens when you're not logged-in. WooCommerce stores the session identifier (the user ID or an auto-generated customer ID) in the browser's cookies, and not the superglobal $_SESSION. Try var_dump( $customer->get_billing(), $customer->get_id(), WC->session->get_customer_id() ) and see the output when you're logged-in and not.

    – Sally CJ
    Nov 11 '18 at 23:55












  • @SallyCJ ah okay so my root assumption that this stores customer data in a session is false? I am seeing the wp_woocommerce_session_xxx=yyyy cookie passed up in the request... it there a built-in way to store this customer data temporarily?

    – Randy Hall
    Nov 12 '18 at 0:02






  • 1





    Your assumption is basically true; however, the session data (other than the session identifier; e.g. cart contents) are stored in prefixwoocommerce_sessions table - e.g. wp_woocommerce_sessions. And this is same to how the WordPress authentication works, where your user ID is saved in the cookie, but the rest of your session are saved in the database. You can use WC()->session->set() to save data temporarily, which can persist across pages - but of course cookies must be enabled in the user's browser.

    – Sally CJ
    Nov 12 '18 at 0:19











  • @SallyCJ I'm seeing the same issue, using WC()->session, setting a var, then commenting out the set and refreshing the page. The value I saved and successfully returned using WC()->session->set() and WC()->session->get() is just flat out not there after navigation.

    – Randy Hall
    Nov 12 '18 at 8:27






  • 1





    Check my answer and let me know how it goes. Hopefully it works..

    – Sally CJ
    Nov 12 '18 at 10:29







1




1





That is a normal thing if $customer->get_id() is 0 which happens when you're not logged-in. WooCommerce stores the session identifier (the user ID or an auto-generated customer ID) in the browser's cookies, and not the superglobal $_SESSION. Try var_dump( $customer->get_billing(), $customer->get_id(), WC->session->get_customer_id() ) and see the output when you're logged-in and not.

– Sally CJ
Nov 11 '18 at 23:55






That is a normal thing if $customer->get_id() is 0 which happens when you're not logged-in. WooCommerce stores the session identifier (the user ID or an auto-generated customer ID) in the browser's cookies, and not the superglobal $_SESSION. Try var_dump( $customer->get_billing(), $customer->get_id(), WC->session->get_customer_id() ) and see the output when you're logged-in and not.

– Sally CJ
Nov 11 '18 at 23:55














@SallyCJ ah okay so my root assumption that this stores customer data in a session is false? I am seeing the wp_woocommerce_session_xxx=yyyy cookie passed up in the request... it there a built-in way to store this customer data temporarily?

– Randy Hall
Nov 12 '18 at 0:02





@SallyCJ ah okay so my root assumption that this stores customer data in a session is false? I am seeing the wp_woocommerce_session_xxx=yyyy cookie passed up in the request... it there a built-in way to store this customer data temporarily?

– Randy Hall
Nov 12 '18 at 0:02




1




1





Your assumption is basically true; however, the session data (other than the session identifier; e.g. cart contents) are stored in prefixwoocommerce_sessions table - e.g. wp_woocommerce_sessions. And this is same to how the WordPress authentication works, where your user ID is saved in the cookie, but the rest of your session are saved in the database. You can use WC()->session->set() to save data temporarily, which can persist across pages - but of course cookies must be enabled in the user's browser.

– Sally CJ
Nov 12 '18 at 0:19





Your assumption is basically true; however, the session data (other than the session identifier; e.g. cart contents) are stored in prefixwoocommerce_sessions table - e.g. wp_woocommerce_sessions. And this is same to how the WordPress authentication works, where your user ID is saved in the cookie, but the rest of your session are saved in the database. You can use WC()->session->set() to save data temporarily, which can persist across pages - but of course cookies must be enabled in the user's browser.

– Sally CJ
Nov 12 '18 at 0:19













@SallyCJ I'm seeing the same issue, using WC()->session, setting a var, then commenting out the set and refreshing the page. The value I saved and successfully returned using WC()->session->set() and WC()->session->get() is just flat out not there after navigation.

– Randy Hall
Nov 12 '18 at 8:27





@SallyCJ I'm seeing the same issue, using WC()->session, setting a var, then commenting out the set and refreshing the page. The value I saved and successfully returned using WC()->session->set() and WC()->session->get() is just flat out not there after navigation.

– Randy Hall
Nov 12 '18 at 8:27




1




1





Check my answer and let me know how it goes. Hopefully it works..

– Sally CJ
Nov 12 '18 at 10:29





Check my answer and let me know how it goes. Hopefully it works..

– Sally CJ
Nov 12 '18 at 10:29












2 Answers
2






active

oldest

votes


















2





+50









If $customer->save() doesn't persist the changes you made to the customer's data (e.g. $customer->set_billing_first_name('Test')), then it's likely because the customer is not registered to the site or not logged-in, where $customer->get_id() is 0.



And that is normal because the user's ID or the session's ID is required in order to properly save the changes and make it persistent across different pages.



So when the user is not registered/logged-in, WooCommerce doesn't start its session until the user logs in, or that he/she added a product into the cart.



But you can manually start the session, like so: (add the code to the active theme's functions.php file)



add_action( 'woocommerce_init', function()
if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );

);


And then, changes to the customer's data would then be carried on to other pages so long as cookies are enabled on the browser, because just like WordPress, WooCommerce stores its session ID (the user ID or an auto-generated ID/hash) in the cookies — and the session ID is used to set/retrieve the session data in the database — the table name is woocommerce_sessions if without any table prefix.



Try this after starting the WooCommerce session:



$customer = WC()->customer;
// Change 'Test' if necessary - e.g. 'Something unique 123'
if ( 'Test' !== $customer->get_billing_first_name() )
$customer->set_billing_first_name( 'Test' );
echo 'First name added to session<br>';
else
echo 'First name read from session<br>';



And this one — you should see a new date on each page load: (well, the one you've set previously)



echo WC()->session->get( 'test', 'None set' ) . '<br>';
WC()->session->set( 'test', current_time( 'mysql' ) );
echo WC()->session->get( 'test', 'It can't be empty' );





share|improve this answer

























  • You are my hero today. I would seriously pay you to answer my questions about WP. So many gaps in my knowledge...

    – Randy Hall
    Nov 13 '18 at 2:23






  • 1





    Well, I always learned from people's code, and eventually became better in time. And I'm sure you too can. :) But hey, I also still have gaps in my knowledge because nobody is perfect... xD And you can always ask your question on Stack Overflow - or for WordPress-specific like creating custom WordPress REST API endpoints, then you should ask it on WordPress Stack Exchange. (Note though, WooCommerce questions are off-topic there..)

    – Sally CJ
    Nov 13 '18 at 10:06












  • Been stuck on this one for days, you seem like you understand the bones much better than I do stackoverflow.com/questions/53362482/…

    – Randy Hall
    Nov 20 '18 at 14:42






  • 1





    Sorry Randy, I forgot about that question, and by the time I wanted to answer it, you've already posted an answer, so I +1 it and I'm sure all is good now. :)

    – Sally CJ
    Nov 23 '18 at 8:34


















0














Are you using the Url set in Wordpress? It seems that it stores the cookie based on the url configured in the app rather than the actual one.



Hope this helps.



Source: https://en.blogpascher.com/wordpress-tutorial/How-correct-the-loss-of-session-on-wordpress






share|improve this answer























  • This is actually a local copy of my site. Do you have a quick reference for where/what settings to check?

    – Randy Hall
    Nov 11 '18 at 0:10











  • Either in wp-config.php or in the interface under General Settings

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:12











  • Check WP_SITEURL In wp-config.php

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:14











  • Interestingly, there is no WP_SITEURL set in the wp-config.php in the root of this site. As in the property is not listed. This is a site that is hosted on wpengine and they do have some custom settings in here, but I'm uncertain if that would have any effect. But I like this train of thought, working through more now

    – Randy Hall
    Nov 11 '18 at 0:28











  • It is also in the interface codex.wordpress.org/Changing_The_Site_URL

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:32










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201243%2fwordpress-wc-session-not-carried-to-next-page%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2





+50









If $customer->save() doesn't persist the changes you made to the customer's data (e.g. $customer->set_billing_first_name('Test')), then it's likely because the customer is not registered to the site or not logged-in, where $customer->get_id() is 0.



And that is normal because the user's ID or the session's ID is required in order to properly save the changes and make it persistent across different pages.



So when the user is not registered/logged-in, WooCommerce doesn't start its session until the user logs in, or that he/she added a product into the cart.



But you can manually start the session, like so: (add the code to the active theme's functions.php file)



add_action( 'woocommerce_init', function()
if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );

);


And then, changes to the customer's data would then be carried on to other pages so long as cookies are enabled on the browser, because just like WordPress, WooCommerce stores its session ID (the user ID or an auto-generated ID/hash) in the cookies — and the session ID is used to set/retrieve the session data in the database — the table name is woocommerce_sessions if without any table prefix.



Try this after starting the WooCommerce session:



$customer = WC()->customer;
// Change 'Test' if necessary - e.g. 'Something unique 123'
if ( 'Test' !== $customer->get_billing_first_name() )
$customer->set_billing_first_name( 'Test' );
echo 'First name added to session<br>';
else
echo 'First name read from session<br>';



And this one — you should see a new date on each page load: (well, the one you've set previously)



echo WC()->session->get( 'test', 'None set' ) . '<br>';
WC()->session->set( 'test', current_time( 'mysql' ) );
echo WC()->session->get( 'test', 'It can't be empty' );





share|improve this answer

























  • You are my hero today. I would seriously pay you to answer my questions about WP. So many gaps in my knowledge...

    – Randy Hall
    Nov 13 '18 at 2:23






  • 1





    Well, I always learned from people's code, and eventually became better in time. And I'm sure you too can. :) But hey, I also still have gaps in my knowledge because nobody is perfect... xD And you can always ask your question on Stack Overflow - or for WordPress-specific like creating custom WordPress REST API endpoints, then you should ask it on WordPress Stack Exchange. (Note though, WooCommerce questions are off-topic there..)

    – Sally CJ
    Nov 13 '18 at 10:06












  • Been stuck on this one for days, you seem like you understand the bones much better than I do stackoverflow.com/questions/53362482/…

    – Randy Hall
    Nov 20 '18 at 14:42






  • 1





    Sorry Randy, I forgot about that question, and by the time I wanted to answer it, you've already posted an answer, so I +1 it and I'm sure all is good now. :)

    – Sally CJ
    Nov 23 '18 at 8:34















2





+50









If $customer->save() doesn't persist the changes you made to the customer's data (e.g. $customer->set_billing_first_name('Test')), then it's likely because the customer is not registered to the site or not logged-in, where $customer->get_id() is 0.



And that is normal because the user's ID or the session's ID is required in order to properly save the changes and make it persistent across different pages.



So when the user is not registered/logged-in, WooCommerce doesn't start its session until the user logs in, or that he/she added a product into the cart.



But you can manually start the session, like so: (add the code to the active theme's functions.php file)



add_action( 'woocommerce_init', function()
if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );

);


And then, changes to the customer's data would then be carried on to other pages so long as cookies are enabled on the browser, because just like WordPress, WooCommerce stores its session ID (the user ID or an auto-generated ID/hash) in the cookies — and the session ID is used to set/retrieve the session data in the database — the table name is woocommerce_sessions if without any table prefix.



Try this after starting the WooCommerce session:



$customer = WC()->customer;
// Change 'Test' if necessary - e.g. 'Something unique 123'
if ( 'Test' !== $customer->get_billing_first_name() )
$customer->set_billing_first_name( 'Test' );
echo 'First name added to session<br>';
else
echo 'First name read from session<br>';



And this one — you should see a new date on each page load: (well, the one you've set previously)



echo WC()->session->get( 'test', 'None set' ) . '<br>';
WC()->session->set( 'test', current_time( 'mysql' ) );
echo WC()->session->get( 'test', 'It can't be empty' );





share|improve this answer

























  • You are my hero today. I would seriously pay you to answer my questions about WP. So many gaps in my knowledge...

    – Randy Hall
    Nov 13 '18 at 2:23






  • 1





    Well, I always learned from people's code, and eventually became better in time. And I'm sure you too can. :) But hey, I also still have gaps in my knowledge because nobody is perfect... xD And you can always ask your question on Stack Overflow - or for WordPress-specific like creating custom WordPress REST API endpoints, then you should ask it on WordPress Stack Exchange. (Note though, WooCommerce questions are off-topic there..)

    – Sally CJ
    Nov 13 '18 at 10:06












  • Been stuck on this one for days, you seem like you understand the bones much better than I do stackoverflow.com/questions/53362482/…

    – Randy Hall
    Nov 20 '18 at 14:42






  • 1





    Sorry Randy, I forgot about that question, and by the time I wanted to answer it, you've already posted an answer, so I +1 it and I'm sure all is good now. :)

    – Sally CJ
    Nov 23 '18 at 8:34













2





+50







2





+50



2




+50





If $customer->save() doesn't persist the changes you made to the customer's data (e.g. $customer->set_billing_first_name('Test')), then it's likely because the customer is not registered to the site or not logged-in, where $customer->get_id() is 0.



And that is normal because the user's ID or the session's ID is required in order to properly save the changes and make it persistent across different pages.



So when the user is not registered/logged-in, WooCommerce doesn't start its session until the user logs in, or that he/she added a product into the cart.



But you can manually start the session, like so: (add the code to the active theme's functions.php file)



add_action( 'woocommerce_init', function()
if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );

);


And then, changes to the customer's data would then be carried on to other pages so long as cookies are enabled on the browser, because just like WordPress, WooCommerce stores its session ID (the user ID or an auto-generated ID/hash) in the cookies — and the session ID is used to set/retrieve the session data in the database — the table name is woocommerce_sessions if without any table prefix.



Try this after starting the WooCommerce session:



$customer = WC()->customer;
// Change 'Test' if necessary - e.g. 'Something unique 123'
if ( 'Test' !== $customer->get_billing_first_name() )
$customer->set_billing_first_name( 'Test' );
echo 'First name added to session<br>';
else
echo 'First name read from session<br>';



And this one — you should see a new date on each page load: (well, the one you've set previously)



echo WC()->session->get( 'test', 'None set' ) . '<br>';
WC()->session->set( 'test', current_time( 'mysql' ) );
echo WC()->session->get( 'test', 'It can't be empty' );





share|improve this answer















If $customer->save() doesn't persist the changes you made to the customer's data (e.g. $customer->set_billing_first_name('Test')), then it's likely because the customer is not registered to the site or not logged-in, where $customer->get_id() is 0.



And that is normal because the user's ID or the session's ID is required in order to properly save the changes and make it persistent across different pages.



So when the user is not registered/logged-in, WooCommerce doesn't start its session until the user logs in, or that he/she added a product into the cart.



But you can manually start the session, like so: (add the code to the active theme's functions.php file)



add_action( 'woocommerce_init', function()
if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );

);


And then, changes to the customer's data would then be carried on to other pages so long as cookies are enabled on the browser, because just like WordPress, WooCommerce stores its session ID (the user ID or an auto-generated ID/hash) in the cookies — and the session ID is used to set/retrieve the session data in the database — the table name is woocommerce_sessions if without any table prefix.



Try this after starting the WooCommerce session:



$customer = WC()->customer;
// Change 'Test' if necessary - e.g. 'Something unique 123'
if ( 'Test' !== $customer->get_billing_first_name() )
$customer->set_billing_first_name( 'Test' );
echo 'First name added to session<br>';
else
echo 'First name read from session<br>';



And this one — you should see a new date on each page load: (well, the one you've set previously)



echo WC()->session->get( 'test', 'None set' ) . '<br>';
WC()->session->set( 'test', current_time( 'mysql' ) );
echo WC()->session->get( 'test', 'It can't be empty' );






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 10:45

























answered Nov 12 '18 at 10:25









Sally CJSally CJ

7,6772516




7,6772516












  • You are my hero today. I would seriously pay you to answer my questions about WP. So many gaps in my knowledge...

    – Randy Hall
    Nov 13 '18 at 2:23






  • 1





    Well, I always learned from people's code, and eventually became better in time. And I'm sure you too can. :) But hey, I also still have gaps in my knowledge because nobody is perfect... xD And you can always ask your question on Stack Overflow - or for WordPress-specific like creating custom WordPress REST API endpoints, then you should ask it on WordPress Stack Exchange. (Note though, WooCommerce questions are off-topic there..)

    – Sally CJ
    Nov 13 '18 at 10:06












  • Been stuck on this one for days, you seem like you understand the bones much better than I do stackoverflow.com/questions/53362482/…

    – Randy Hall
    Nov 20 '18 at 14:42






  • 1





    Sorry Randy, I forgot about that question, and by the time I wanted to answer it, you've already posted an answer, so I +1 it and I'm sure all is good now. :)

    – Sally CJ
    Nov 23 '18 at 8:34

















  • You are my hero today. I would seriously pay you to answer my questions about WP. So many gaps in my knowledge...

    – Randy Hall
    Nov 13 '18 at 2:23






  • 1





    Well, I always learned from people's code, and eventually became better in time. And I'm sure you too can. :) But hey, I also still have gaps in my knowledge because nobody is perfect... xD And you can always ask your question on Stack Overflow - or for WordPress-specific like creating custom WordPress REST API endpoints, then you should ask it on WordPress Stack Exchange. (Note though, WooCommerce questions are off-topic there..)

    – Sally CJ
    Nov 13 '18 at 10:06












  • Been stuck on this one for days, you seem like you understand the bones much better than I do stackoverflow.com/questions/53362482/…

    – Randy Hall
    Nov 20 '18 at 14:42






  • 1





    Sorry Randy, I forgot about that question, and by the time I wanted to answer it, you've already posted an answer, so I +1 it and I'm sure all is good now. :)

    – Sally CJ
    Nov 23 '18 at 8:34
















You are my hero today. I would seriously pay you to answer my questions about WP. So many gaps in my knowledge...

– Randy Hall
Nov 13 '18 at 2:23





You are my hero today. I would seriously pay you to answer my questions about WP. So many gaps in my knowledge...

– Randy Hall
Nov 13 '18 at 2:23




1




1





Well, I always learned from people's code, and eventually became better in time. And I'm sure you too can. :) But hey, I also still have gaps in my knowledge because nobody is perfect... xD And you can always ask your question on Stack Overflow - or for WordPress-specific like creating custom WordPress REST API endpoints, then you should ask it on WordPress Stack Exchange. (Note though, WooCommerce questions are off-topic there..)

– Sally CJ
Nov 13 '18 at 10:06






Well, I always learned from people's code, and eventually became better in time. And I'm sure you too can. :) But hey, I also still have gaps in my knowledge because nobody is perfect... xD And you can always ask your question on Stack Overflow - or for WordPress-specific like creating custom WordPress REST API endpoints, then you should ask it on WordPress Stack Exchange. (Note though, WooCommerce questions are off-topic there..)

– Sally CJ
Nov 13 '18 at 10:06














Been stuck on this one for days, you seem like you understand the bones much better than I do stackoverflow.com/questions/53362482/…

– Randy Hall
Nov 20 '18 at 14:42





Been stuck on this one for days, you seem like you understand the bones much better than I do stackoverflow.com/questions/53362482/…

– Randy Hall
Nov 20 '18 at 14:42




1




1





Sorry Randy, I forgot about that question, and by the time I wanted to answer it, you've already posted an answer, so I +1 it and I'm sure all is good now. :)

– Sally CJ
Nov 23 '18 at 8:34





Sorry Randy, I forgot about that question, and by the time I wanted to answer it, you've already posted an answer, so I +1 it and I'm sure all is good now. :)

– Sally CJ
Nov 23 '18 at 8:34













0














Are you using the Url set in Wordpress? It seems that it stores the cookie based on the url configured in the app rather than the actual one.



Hope this helps.



Source: https://en.blogpascher.com/wordpress-tutorial/How-correct-the-loss-of-session-on-wordpress






share|improve this answer























  • This is actually a local copy of my site. Do you have a quick reference for where/what settings to check?

    – Randy Hall
    Nov 11 '18 at 0:10











  • Either in wp-config.php or in the interface under General Settings

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:12











  • Check WP_SITEURL In wp-config.php

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:14











  • Interestingly, there is no WP_SITEURL set in the wp-config.php in the root of this site. As in the property is not listed. This is a site that is hosted on wpengine and they do have some custom settings in here, but I'm uncertain if that would have any effect. But I like this train of thought, working through more now

    – Randy Hall
    Nov 11 '18 at 0:28











  • It is also in the interface codex.wordpress.org/Changing_The_Site_URL

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:32















0














Are you using the Url set in Wordpress? It seems that it stores the cookie based on the url configured in the app rather than the actual one.



Hope this helps.



Source: https://en.blogpascher.com/wordpress-tutorial/How-correct-the-loss-of-session-on-wordpress






share|improve this answer























  • This is actually a local copy of my site. Do you have a quick reference for where/what settings to check?

    – Randy Hall
    Nov 11 '18 at 0:10











  • Either in wp-config.php or in the interface under General Settings

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:12











  • Check WP_SITEURL In wp-config.php

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:14











  • Interestingly, there is no WP_SITEURL set in the wp-config.php in the root of this site. As in the property is not listed. This is a site that is hosted on wpengine and they do have some custom settings in here, but I'm uncertain if that would have any effect. But I like this train of thought, working through more now

    – Randy Hall
    Nov 11 '18 at 0:28











  • It is also in the interface codex.wordpress.org/Changing_The_Site_URL

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:32













0












0








0







Are you using the Url set in Wordpress? It seems that it stores the cookie based on the url configured in the app rather than the actual one.



Hope this helps.



Source: https://en.blogpascher.com/wordpress-tutorial/How-correct-the-loss-of-session-on-wordpress






share|improve this answer













Are you using the Url set in Wordpress? It seems that it stores the cookie based on the url configured in the app rather than the actual one.



Hope this helps.



Source: https://en.blogpascher.com/wordpress-tutorial/How-correct-the-loss-of-session-on-wordpress







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 0:08









Andrei Dumitrescu-TudorAndrei Dumitrescu-Tudor

19719




19719












  • This is actually a local copy of my site. Do you have a quick reference for where/what settings to check?

    – Randy Hall
    Nov 11 '18 at 0:10











  • Either in wp-config.php or in the interface under General Settings

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:12











  • Check WP_SITEURL In wp-config.php

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:14











  • Interestingly, there is no WP_SITEURL set in the wp-config.php in the root of this site. As in the property is not listed. This is a site that is hosted on wpengine and they do have some custom settings in here, but I'm uncertain if that would have any effect. But I like this train of thought, working through more now

    – Randy Hall
    Nov 11 '18 at 0:28











  • It is also in the interface codex.wordpress.org/Changing_The_Site_URL

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:32

















  • This is actually a local copy of my site. Do you have a quick reference for where/what settings to check?

    – Randy Hall
    Nov 11 '18 at 0:10











  • Either in wp-config.php or in the interface under General Settings

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:12











  • Check WP_SITEURL In wp-config.php

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:14











  • Interestingly, there is no WP_SITEURL set in the wp-config.php in the root of this site. As in the property is not listed. This is a site that is hosted on wpengine and they do have some custom settings in here, but I'm uncertain if that would have any effect. But I like this train of thought, working through more now

    – Randy Hall
    Nov 11 '18 at 0:28











  • It is also in the interface codex.wordpress.org/Changing_The_Site_URL

    – Andrei Dumitrescu-Tudor
    Nov 11 '18 at 0:32
















This is actually a local copy of my site. Do you have a quick reference for where/what settings to check?

– Randy Hall
Nov 11 '18 at 0:10





This is actually a local copy of my site. Do you have a quick reference for where/what settings to check?

– Randy Hall
Nov 11 '18 at 0:10













Either in wp-config.php or in the interface under General Settings

– Andrei Dumitrescu-Tudor
Nov 11 '18 at 0:12





Either in wp-config.php or in the interface under General Settings

– Andrei Dumitrescu-Tudor
Nov 11 '18 at 0:12













Check WP_SITEURL In wp-config.php

– Andrei Dumitrescu-Tudor
Nov 11 '18 at 0:14





Check WP_SITEURL In wp-config.php

– Andrei Dumitrescu-Tudor
Nov 11 '18 at 0:14













Interestingly, there is no WP_SITEURL set in the wp-config.php in the root of this site. As in the property is not listed. This is a site that is hosted on wpengine and they do have some custom settings in here, but I'm uncertain if that would have any effect. But I like this train of thought, working through more now

– Randy Hall
Nov 11 '18 at 0:28





Interestingly, there is no WP_SITEURL set in the wp-config.php in the root of this site. As in the property is not listed. This is a site that is hosted on wpengine and they do have some custom settings in here, but I'm uncertain if that would have any effect. But I like this train of thought, working through more now

– Randy Hall
Nov 11 '18 at 0:28













It is also in the interface codex.wordpress.org/Changing_The_Site_URL

– Andrei Dumitrescu-Tudor
Nov 11 '18 at 0:32





It is also in the interface codex.wordpress.org/Changing_The_Site_URL

– Andrei Dumitrescu-Tudor
Nov 11 '18 at 0:32

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201243%2fwordpress-wc-session-not-carried-to-next-page%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế