save_to_session( $customer ); } /** * Simply update the session. * * @param WC_Customer $customer */ public function update( &$customer ) { $this->save_to_session( $customer ); } /** * Saves all customer data to the session. * * @param WC_Customer $customer */ public function save_to_session( $customer ) { $data = array(); foreach ( $this->session_keys as $session_key ) { $function_key = $session_key; if ( 'billing_' === substr( $session_key, 0, 8 ) ) { $session_key = str_replace( 'billing_', '', $session_key ); } $data[ $session_key ] = $customer->{"get_$function_key"}( 'edit' ); } if ( WC()->session->get( 'customer' ) !== $data ) { WC()->session->set( 'customer', $data ); } } /** * Read customer data from the session unless the user has logged in, in * which case the stored ID will differ from the actual ID. * * @since 3.0.0 * @param WC_Customer $customer */ public function read( &$customer ) { $data = (array) WC()->session->get( 'customer' ); if ( ! empty( $data ) && isset( $data['id'] ) && $data['id'] === $customer->get_id() ) { foreach ( $this->session_keys as $session_key ) { $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}" ) ) ) { $customer->{"set_{$function_key}"}( $data[ $session_key ] ); } } } $this->set_defaults( $customer ); $customer->set_object_read( true ); } /** * Load default values if props are unset. * * @param WC_Customer $customer */ protected function set_defaults( &$customer ) { try { $default = wc_get_customer_default_location(); if ( ! $customer->get_billing_country() ) { $customer->set_billing_country( $default['country'] ); } if ( ! $customer->get_shipping_country() ) { $customer->set_shipping_country( $customer->get_billing_country() ); } if ( ! $customer->get_billing_state() ) { $customer->set_billing_state( $default['state'] ); } if ( ! $customer->get_shipping_state() ) { $customer->set_shipping_state( $customer->get_billing_state() ); } if ( ! $customer->get_billing_email() && is_user_logged_in() ) { $current_user = wp_get_current_user(); $customer->set_billing_email( $current_user->user_email ); } } catch ( WC_Data_Exception $e ) {} } /** * Deletes a customer from the database. * * @since 3.0.0 * @param WC_Customer $customer * @param array $args Array of args to pass to the delete method. */ public function delete( &$customer, $args = array() ) { WC()->session->set( 'customer', null ); } /** * Gets the customers last order. * * @since 3.0.0 * @param WC_Customer * @return WC_Order|false */ public function get_last_order( &$customer ) { return false; } /** * Return the number of orders this customer has. * * @since 3.0.0 * @param WC_Customer * @return integer */ public function get_order_count( &$customer ) { return 0; } /** * Return how much money this customer has spent. * * @since 3.0.0 * @param WC_Customer * @return float */ public function get_total_spent( &$customer ) { return 0; } }