WC_Customer_Data_Store_Session::read()publicWC 3.0.0

Read customer data from the session unless the user has logged in, in which case the stored ID will differ from the actual ID.

Метод класса: WC_Customer_Data_Store_Session{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$WC_Customer_Data_Store_Session = new WC_Customer_Data_Store_Session();
$WC_Customer_Data_Store_Session->read( $customer );
$customer(WC_Customer) (обязательный) (передается по ссылке — &)
Customer object.

Список изменений

С версии 3.0.0 Введена.

Код WC_Customer_Data_Store_Session::read() WC 8.7.0

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 ) {
					$meta_data_values = json_decode( wp_unslash( $data[ $session_key ] ), true );
					if ( $meta_data_values ) {
						foreach ( $meta_data_values 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 );
}