WC_Customer_Data_Store_Session

WC Customer Data Store which stores the data in session.

Defined (1)

The class is defined in the following location(s).

/includes/data-stores/class-wc-customer-data-store-session.php  
  1. class WC_Customer_Data_Store_Session extends WC_Data_Store_WP implements WC_Customer_Data_Store_Interface, WC_Object_Data_Store_Interface { 
  2.  
  3. /** 
  4. * Keys which are also stored in a session (so we can make sure they get updated...) 
  5. * @var array 
  6. */ 
  7. protected $session_keys = array( 
  8. 'billing_postcode',  
  9. 'billing_city',  
  10. 'billing_address_1',  
  11. 'billing_address',  
  12. 'billing_address_2',  
  13. 'billing_state',  
  14. 'billing_country',  
  15. 'shipping_postcode',  
  16. 'shipping_city',  
  17. 'shipping_address_1',  
  18. 'shipping_address',  
  19. 'shipping_address_2',  
  20. 'shipping_state',  
  21. 'shipping_country',  
  22. 'is_vat_exempt',  
  23. 'calculated_shipping',  
  24. 'billing_first_name',  
  25. 'billing_last_name',  
  26. 'billing_company',  
  27. 'billing_phone',  
  28. 'billing_email',  
  29. 'shipping_first_name',  
  30. 'shipping_last_name',  
  31. 'shipping_company',  
  32. ); 
  33.  
  34. /** 
  35. * Simply update the session. 
  36. * @param WC_Customer 
  37. */ 
  38. public function create( &$customer ) { 
  39. $this->save_to_session( $customer ); 
  40.  
  41. /** 
  42. * Simply update the session. 
  43. * @param WC_Customer 
  44. */ 
  45. public function update( &$customer ) { 
  46. $this->save_to_session( $customer ); 
  47.  
  48. /** 
  49. * Saves all customer data to the session. 
  50. * @param WC_Customer 
  51. */ 
  52. public function save_to_session( $customer ) { 
  53. $data = array(); 
  54. foreach ( $this->session_keys as $session_key ) { 
  55. $function_key = $session_key; 
  56. if ( 'billing_' === substr( $session_key, 0, 8 ) ) { 
  57. $session_key = str_replace( 'billing_', '', $session_key ); 
  58. $data[ $session_key ] = $customer->{"get_$function_key"}( 'edit' ); 
  59. if ( WC()->session->get( 'customer' ) !== $data ) { 
  60. WC()->session->set( 'customer', $data ); 
  61.  
  62. /** 
  63. * Read customer data from the session. 
  64. * @since 3.0.0 
  65. * @param WC_Customer 
  66. */ 
  67. public function read( &$customer ) { 
  68. $data = (array) WC()->session->get( 'customer' ); 
  69. if ( ! empty( $data ) ) { 
  70. foreach ( $this->session_keys as $session_key ) { 
  71. $function_key = $session_key; 
  72. if ( 'billing_' === substr( $session_key, 0, 8 ) ) { 
  73. $session_key = str_replace( 'billing_', '', $session_key ); 
  74. if ( ! empty( $data[ $session_key ] ) && is_callable( array( $customer, "set_{$function_key}" ) ) ) { 
  75. // Only set from session if data is already missing. 
  76. if ( ! $customer->{"get_{$function_key}"}() ) { 
  77. $customer->{"set_{$function_key}"}( $data[ $session_key ] ); 
  78. $this->set_defaults( $customer ); 
  79. $customer->set_object_read( true ); 
  80.  
  81. /** 
  82. * Load default values if props are unset. 
  83. * @param WC_Customer 
  84. */ 
  85. protected function set_defaults( &$customer ) { 
  86. try { 
  87. $default = wc_get_customer_default_location(); 
  88.  
  89. if ( ! $customer->get_billing_country() ) { 
  90. $customer->set_billing_country( $default['country'] ); 
  91.  
  92. if ( ! $customer->get_shipping_country() ) { 
  93. $customer->set_shipping_country( $customer->get_billing_country() ); 
  94.  
  95. if ( ! $customer->get_billing_state() ) { 
  96. $customer->set_billing_state( $default['state'] ); 
  97.  
  98. if ( ! $customer->get_shipping_state() ) { 
  99. $customer->set_shipping_state( $customer->get_billing_state() ); 
  100.  
  101. if ( ! $customer->get_billing_email() && is_user_logged_in() ) { 
  102. $current_user = wp_get_current_user(); 
  103. $customer->set_billing_email( $current_user->user_email ); 
  104. } catch ( WC_Data_Exception $e ) {} 
  105.  
  106. /** 
  107. * Deletes a customer from the database. 
  108. * @since 3.0.0 
  109. * @param WC_Customer 
  110. * @param array $args Array of args to pass to the delete method. 
  111. */ 
  112. public function delete( &$customer, $args = array() ) { 
  113. WC()->session->set( 'customer', null ); 
  114.  
  115. /** 
  116. * Gets the customers last order. 
  117. * @since 3.0.0 
  118. * @param WC_Customer 
  119. * @return WC_Order|false 
  120. */ 
  121. public function get_last_order( &$customer ) { 
  122. return false; 
  123.  
  124. /** 
  125. * Return the number of orders this customer has. 
  126. * @since 3.0.0 
  127. * @param WC_Customer 
  128. * @return integer 
  129. */ 
  130. public function get_order_count( &$customer ) { 
  131. return 0; 
  132.  
  133. /** 
  134. * Return how much money this customer has spent. 
  135. * @since 3.0.0 
  136. * @param WC_Customer 
  137. * @return float 
  138. */ 
  139. public function get_total_spent( &$customer ) { 
  140. return 0;