WCPBC_Customer

WCPBC_Customer.

Defined (1)

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

/includes/class-wcpbc-customer.php  
  1. class WCPBC_Customer { 
  2.  
  3. /** Stores customer price based on country data as an array */ 
  4. protected $_data; 
  5.  
  6. /** Stores bool when data is changed */ 
  7. private $_changed = false; 
  8.  
  9. /** 
  10. * Constructor for the wcpbc_customer class loads the data. 
  11. * @access public 
  12. */ 
  13.  
  14. public function __construct() {  
  15.  
  16. $this->_data = WC()->session->get( 'wcpbc_customer' );  
  17.  
  18. $wc_customer_country = wcpbc_get_woocommerce_country();  
  19.  
  20. if ( empty( $this->_data ) || ! in_array( $wc_customer_country, $this->countries ) || ( $this->timestamp < get_option( 'wc_price_based_country_timestamp' ) ) ) { 
  21.  
  22. $this->set_country( $wc_customer_country ); 
  23.  
  24. if ( ! WC()->session->has_session() ) { 
  25. WC()->session->set_customer_session_cookie(true); 
  26.  
  27. // When leaving or ending page load, store data 
  28. add_action( 'shutdown', array( $this, 'save_data' ), 10 );  
  29. }  
  30.  
  31. /** 
  32. * save_data function. 
  33. * @access public 
  34. */ 
  35. public function save_data() { 
  36.  
  37. if ( $this->_changed ) { 
  38. WC()->session->set( 'wcpbc_customer', $this->_data );  
  39. }  
  40.  
  41.  
  42. /** 
  43. * __get function. 
  44. * @access public 
  45. * @param string $property 
  46. * @return string 
  47. */ 
  48. public function __get( $property ) { 
  49.  
  50. $value = isset( $this->_data[ $property ] ) ? $this->_data[ $property ] : ''; 
  51.  
  52. if ( $property === 'countries' && ! $value) { 
  53. $value = array();  
  54.  
  55. return $value; 
  56.  
  57. /** 
  58. * Sets wcpbc data form country. 
  59. * @access public 
  60. * @param mixed $country 
  61. * @return boolean 
  62. */ 
  63. public function set_country( $country ) { 
  64.  
  65. $has_region = false; 
  66.  
  67. $this->_data = array();  
  68.  
  69. foreach ( WCPBC()->get_regions() as $key => $group_data ) {  
  70.  
  71. if ( in_array( $country, $group_data['countries'] ) ) { 
  72. $this->_data = array_merge( $group_data, array( 'group_key' => $key, 'timestamp' => time() ) ); 
  73. $has_region = true; 
  74. break; 
  75. }  
  76.  
  77.  
  78. $this->_changed = true; 
  79.  
  80. return $has_region; 
  81. }