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 10.3.3
public function read( &$customer ) {
$data = (array) WC()->session->get( 'customer' );
/**
* There is a valid session if $data is not empty, and the ID matches the logged in user ID.
*
* If the user object has been updated since the session was created (based on date_modified) we should not load the session - data should be reloaded.
*/
if ( isset( $data['id'], $data['date_modified'] ) && $data['id'] === (string) $customer->get_id() && $data['date_modified'] === (string) $customer->get_date_modified( 'edit' ) ) {
foreach ( $this->session_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 ( ! empty( $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}"}( wp_unslash( $data[ $session_key ] ) );
}
}
}
}
$this->set_defaults( $customer );
$customer->set_object_read( true );
}