WC_Customer_Data_Store_Session::read
Read customer data from the session unless the user has logged in, in which case the stored ID will differ from the actual ID.
Method of the class: WC_Customer_Data_Store_Session{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Customer_Data_Store_Session = new WC_Customer_Data_Store_Session(); $WC_Customer_Data_Store_Session->read( $customer );
- $customer(WC_Customer) (required) (passed by reference — &)
- Customer object.
Changelog
| Since 3.0.0 | Introduced. |
WC_Customer_Data_Store_Session::read() WC Customer Data Store Session::read code WC 11.3.0
public function read( &$customer ) {
$data = (array) WC()->session->get( 'customer' );
/**
* The session belongs to this customer if $data is not empty, and the ID matches the logged in user ID.
*
* Empty session values must be applied too (hence isset and not empty below): the session snapshot always contains all the keys,
* so an empty value means the field was explicitly cleared and must override the value loaded from the database.
*/
if ( isset( $data['id'] ) && $data['id'] === (string) $customer->get_id() ) {
// If the user was saved after this session was (e.g. by checkout), the session's copies of saved fields such as
// addresses are out of date, so only apply the session-only values. The user has no copy of those to fall back on.
$is_current = isset( $data['date_modified'] ) && $data['date_modified'] === (string) $customer->get_date_modified( 'edit' );
foreach ( $is_current ? $this->session_keys : $this->session_only_keys as $session_key ) {
if ( in_array( $session_key, array( 'id', 'date_modified' ), true ) ) {
continue;
}
$function_key = $session_key;
if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
$session_key = str_replace( 'billing_', '', $session_key );
}
if ( isset( $data[ $session_key ] ) && is_callable( array( $customer, "set_{$function_key}" ) ) ) {
if ( 'meta_data' === $session_key ) {
if ( is_array( $data[ $session_key ] ) ) {
foreach ( $data[ $session_key ] as $meta_data_value ) {
if ( ! isset( $meta_data_value['key'], $meta_data_value['value'] ) ) {
continue;
}
$customer->add_meta_data( $meta_data_value['key'], $meta_data_value['value'], true );
}
}
} else {
$customer->{"set_{$function_key}"}( $data[ $session_key ] );
}
}
}
}
$this->set_defaults( $customer );
$customer->set_object_read( true );
}