I am sending the standard WooCommerce billing and/or shipping data to a custom API endpoint.
function dc_set_customer_fields($request){
$ret = array('errors' => array());
$params = $request->get_params();
$WCcustomer = WC()->customer;
$WCcheckout = WC()->checkout();
$ret['ckfields'] = $WCcheckout->checkout_fields;
$ret['errors'] = dc_validate_fields($params);
$ret['count'] = count($ret['errors']);
$ret['eval'] = count($ret['errors']) === 0;
if (count($ret['errors']) === 0){
foreach ($params as $name => $value) {
$ret["set_".$name] = wc_clean($value);
$WCcustomer->{"set_".$name}(wc_clean($value));
}
}
$WCcustomer->save_data();
$ret['billing'] = $WCcustomer->get_billing();
$ret['shipping'] = $WCcustomer->get_shipping();
return $ret;
}
In the return, billing and shipping are both set, so I know that the WC()->customer->set_... methods are saving data and the WC()->customer->get_billing() is working to retrieve it.
Absolutely everything in my API method performs and returns as I expect so far as I can see.
Then in my page I have a script tag that spits out the customer data when I refresh or go to another page:
<script type="text/javascript">
<?php
$WCcustomer = WC()->customer;
$billing = json_encode($WCcustomer->get_billing());
$shipping = json_encode($WCcustomer->get_shipping());
//billing/shipping info
echo "window.preloadBilling = $billing;";
echo "window.preloadShipping = $shipping;";
?>
</script>
But alas, there is no data - except the billing and shipping countries being set to "US" as is our default.
I did try explicitly setting the session_start() right before my page php just to be sure, and no change.
Any pointers to why the WC()->customer data would disappear between pages?
More attempts:
I moved my session_start() call to the top of functions.php.
I also explicitly set
$_SESSION['testing'] = "myval";
This printed to the page. I then commented out the above line, and it was still printing to the page. So the PHP session object appears to be working, reinforcing that this is more likely an issue with the way I'm using the baked-in Wordpress/WooCommerce session handling.
I am also getting fancy with some custom routing in my functions.php that conditionally ignores the do_parse_request filter by returning false for certain custom application pages based on the request URL. I thought maybe there was something in there that was setting up session and I was breaking it, but I see the same problem of not receiving the saved data on non-filtered pages as well.
from WooCommerce customer data in session disappears
No comments:
Post a Comment