PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-customer.php

https://gitlab.com/webkod3r/tripolis
PHP | 575 lines | 246 code | 70 blank | 259 comment | 26 complexity | 8c4116c8c9ca9a4e7e29878a4d125030 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * Customer
  7. *
  8. * The WooCommerce customer class handles storage of the current customer's data, such as location.
  9. *
  10. * @class WC_Customer
  11. * @version 2.3.0
  12. * @package WooCommerce/Classes
  13. * @category Class
  14. * @author WooThemes
  15. *
  16. * @property string $country
  17. * @property string $state
  18. * @property string $postcode
  19. * @property string $city
  20. * @property string $address_1
  21. * @property string $address_2
  22. * @property string $shipping_country
  23. * @property string $shipping_state
  24. * @property string $shipping_postcode
  25. * @property string $shipping_city
  26. * @property string $shipping_address_1
  27. * @property string $shipping_address_2
  28. * @property string $is_vat_exempt
  29. * @property string $calculated_shipping
  30. */
  31. class WC_Customer {
  32. /**
  33. * Stores customer data.
  34. *
  35. * @var array
  36. */
  37. protected $_data = array();
  38. /**
  39. * Stores bool when data is changed.
  40. *
  41. * @var bool
  42. */
  43. private $_changed = false;
  44. /**
  45. * Constructor for the customer class loads the customer data.
  46. *
  47. */
  48. public function __construct() {
  49. $this->_data = (array) WC()->session->get( 'customer' );
  50. // No data - set defaults
  51. if ( empty( $this->_data ) ) {
  52. $this->set_default_data();
  53. }
  54. // When leaving or ending page load, store data
  55. add_action( 'shutdown', array( $this, 'save_data' ), 10 );
  56. }
  57. /**
  58. * Save data function.
  59. */
  60. public function save_data() {
  61. if ( $this->_changed ) {
  62. WC()->session->set( 'customer', $this->_data );
  63. }
  64. }
  65. /**
  66. * __set function.
  67. *
  68. * @param mixed $property
  69. * @return bool
  70. */
  71. public function __isset( $property ) {
  72. if ( 'address' === $property ) {
  73. $property = 'address_1';
  74. }
  75. if ( 'shipping_address' === $property ) {
  76. $property = 'shipping_address_1';
  77. }
  78. return isset( $this->_data[ $property ] );
  79. }
  80. /**
  81. * __get function.
  82. *
  83. * @param string $property
  84. * @return string
  85. */
  86. public function __get( $property ) {
  87. if ( 'address' === $property ) {
  88. $property = 'address_1';
  89. }
  90. if ( 'shipping_address' === $property ) {
  91. $property = 'shipping_address_1';
  92. }
  93. return isset( $this->_data[ $property ] ) ? $this->_data[ $property ] : '';
  94. }
  95. /**
  96. * __set function.
  97. *
  98. * @param mixed $property
  99. * @param mixed $value
  100. */
  101. public function __set( $property, $value ) {
  102. if ( 'address' === $property ) {
  103. $property = 'address_1';
  104. }
  105. if ( 'shipping_address' === $property ) {
  106. $property = 'shipping_address_1';
  107. }
  108. $this->_data[ $property ] = $value;
  109. $this->_changed = true;
  110. }
  111. /**
  112. * Get default country for a customer.
  113. *
  114. * @return string
  115. */
  116. public function get_default_country() {
  117. $default = wc_get_customer_default_location();
  118. return $default['country'];
  119. }
  120. /**
  121. * Get default state for a customer.
  122. *
  123. * @return string
  124. */
  125. public function get_default_state() {
  126. $default = wc_get_customer_default_location();
  127. return $default['state'];
  128. }
  129. /**
  130. * Has calculated shipping?
  131. *
  132. * @return bool
  133. */
  134. public function has_calculated_shipping() {
  135. return ! empty( $this->calculated_shipping );
  136. }
  137. /**
  138. * Set customer address to match shop base address.
  139. */
  140. public function set_to_base() {
  141. $this->country = $this->get_default_country();
  142. $this->state = $this->get_default_state();
  143. $this->postcode = '';
  144. $this->city = '';
  145. }
  146. /**
  147. * Set customer shipping address to base address.
  148. */
  149. public function set_shipping_to_base() {
  150. $this->shipping_country = $this->get_default_country();
  151. $this->shipping_state = $this->get_default_state();
  152. $this->shipping_postcode = '';
  153. $this->shipping_city = '';
  154. }
  155. /**
  156. * Is customer outside base country (for tax purposes)?
  157. *
  158. * @return bool
  159. */
  160. public function is_customer_outside_base() {
  161. list( $country, $state ) = $this->get_taxable_address();
  162. if ( $country ) {
  163. $default = wc_get_base_location();
  164. if ( $default['country'] !== $country ) {
  165. return true;
  166. }
  167. if ( $default['state'] && $default['state'] !== $state ) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * Is the user a paying customer?
  175. *
  176. * @return bool
  177. */
  178. function is_paying_customer( $user_id ) {
  179. return '1' === get_user_meta( $user_id, 'paying_customer', true );
  180. }
  181. /**
  182. * Is customer VAT exempt?
  183. *
  184. * @return bool
  185. */
  186. public function is_vat_exempt() {
  187. return ( ! empty( $this->is_vat_exempt ) ) ? true : false;
  188. }
  189. /**
  190. * Gets the state from the current session.
  191. *
  192. * @return string
  193. */
  194. public function get_state() {
  195. return $this->state;
  196. }
  197. /**
  198. * Gets the country from the current session.
  199. *
  200. * @return string
  201. */
  202. public function get_country() {
  203. return $this->country;
  204. }
  205. /**
  206. * Gets the postcode from the current session.
  207. *
  208. * @return string
  209. */
  210. public function get_postcode() {
  211. return empty( $this->postcode ) ? '' : wc_format_postcode( $this->postcode, $this->get_country() );
  212. }
  213. /**
  214. * Get the city from the current session.
  215. *
  216. * @return string
  217. */
  218. public function get_city() {
  219. return $this->city;
  220. }
  221. /**
  222. * Gets the address from the current session.
  223. *
  224. * @return string
  225. */
  226. public function get_address() {
  227. return $this->address_1;
  228. }
  229. /**
  230. * Gets the address_2 from the current session.
  231. *
  232. * @return string
  233. */
  234. public function get_address_2() {
  235. return $this->address_2;
  236. }
  237. /**
  238. * Gets the state from the current session.
  239. *
  240. * @return string
  241. */
  242. public function get_shipping_state() {
  243. return $this->shipping_state;
  244. }
  245. /**
  246. * Gets the country from the current session.
  247. *
  248. * @return string
  249. */
  250. public function get_shipping_country() {
  251. return $this->shipping_country;
  252. }
  253. /**
  254. * Gets the postcode from the current session.
  255. *
  256. * @return string
  257. */
  258. public function get_shipping_postcode() {
  259. return empty( $this->shipping_postcode ) ? '' : wc_format_postcode( $this->shipping_postcode, $this->get_shipping_country() );
  260. }
  261. /**
  262. * Gets the city from the current session.
  263. *
  264. * @return string
  265. */
  266. public function get_shipping_city() {
  267. return $this->shipping_city;
  268. }
  269. /**
  270. * Gets the address from the current session.
  271. *
  272. * @return string
  273. */
  274. public function get_shipping_address() {
  275. return $this->shipping_address_1;
  276. }
  277. /**
  278. * Gets the address_2 from the current session.
  279. *
  280. * @return string
  281. */
  282. public function get_shipping_address_2() {
  283. return $this->shipping_address_2;
  284. }
  285. /**
  286. * Get taxable address.
  287. *
  288. * @return array
  289. */
  290. public function get_taxable_address() {
  291. $tax_based_on = get_option( 'woocommerce_tax_based_on' );
  292. // Check shipping method at this point to see if we need special handling
  293. if ( true == apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && WC()->cart->needs_shipping() && sizeof( array_intersect( WC()->session->get( 'chosen_shipping_methods', array() ), apply_filters( 'woocommerce_local_pickup_methods', array( 'local_pickup' ) ) ) ) > 0 ) {
  294. $tax_based_on = 'base';
  295. }
  296. if ( 'base' === $tax_based_on ) {
  297. $country = WC()->countries->get_base_country();
  298. $state = WC()->countries->get_base_state();
  299. $postcode = WC()->countries->get_base_postcode();
  300. $city = WC()->countries->get_base_city();
  301. } elseif ( 'billing' === $tax_based_on ) {
  302. $country = $this->get_country();
  303. $state = $this->get_state();
  304. $postcode = $this->get_postcode();
  305. $city = $this->get_city();
  306. } else {
  307. $country = $this->get_shipping_country();
  308. $state = $this->get_shipping_state();
  309. $postcode = $this->get_shipping_postcode();
  310. $city = $this->get_shipping_city();
  311. }
  312. return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
  313. }
  314. /**
  315. * Set default data for a customer.
  316. */
  317. public function set_default_data( $get_user_profile_data = true ) {
  318. $this->_data = array(
  319. 'postcode' => '',
  320. 'city' => '',
  321. 'address_1' => '',
  322. 'address_2' => '',
  323. 'state' => '',
  324. 'country' => '',
  325. 'shipping_postcode' => '',
  326. 'shipping_city' => '',
  327. 'shipping_address_1' => '',
  328. 'shipping_address_2' => '',
  329. 'shipping_state' => '',
  330. 'shipping_country' => '',
  331. 'is_vat_exempt' => false,
  332. 'calculated_shipping' => false
  333. );
  334. if ( is_user_logged_in() && $get_user_profile_data ) {
  335. foreach ( $this->_data as $key => $value ) {
  336. $meta_value = get_user_meta( get_current_user_id(), ( false === strstr( $key, 'shipping_' ) ? 'billing_' : '' ) . $key, true );
  337. $this->_data[ $key ] = $meta_value ? $meta_value : $this->_data[ $key ];
  338. }
  339. }
  340. if ( empty( $this->_data['country'] ) ) {
  341. $this->_data['country'] = $this->get_default_country();
  342. }
  343. if ( empty( $this->_data['shipping_country'] ) ) {
  344. $this->_data['shipping_country'] = $this->_data['country'];
  345. }
  346. if ( empty( $this->_data['state'] ) ) {
  347. $this->_data['state'] = $this->get_default_state();
  348. }
  349. if ( empty( $this->_data['shipping_state'] ) ) {
  350. $this->_data['shipping_state'] = $this->_data['state'];
  351. }
  352. }
  353. /**
  354. * Sets session data for the location.
  355. *
  356. * @param string $country
  357. * @param string $state
  358. * @param string $postcode (default: '')
  359. * @param string $city (default: '')
  360. */
  361. public function set_location( $country, $state, $postcode = '', $city = '' ) {
  362. $this->country = $country;
  363. $this->state = $state;
  364. $this->postcode = $postcode;
  365. $this->city = $city;
  366. }
  367. /**
  368. * Sets session data for the country.
  369. *
  370. * @param mixed $country
  371. */
  372. public function set_country( $country ) {
  373. $this->country = $country;
  374. }
  375. /**
  376. * Sets session data for the state.
  377. *
  378. * @param mixed $state
  379. */
  380. public function set_state( $state ) {
  381. $this->state = $state;
  382. }
  383. /**
  384. * Sets session data for the postcode.
  385. *
  386. * @param mixed $postcode
  387. */
  388. public function set_postcode( $postcode ) {
  389. $this->postcode = $postcode;
  390. }
  391. /**
  392. * Sets session data for the city.
  393. *
  394. * @param mixed $city
  395. */
  396. public function set_city( $city ) {
  397. $this->city = $city;
  398. }
  399. /**
  400. * Sets session data for the address.
  401. *
  402. * @param mixed $address
  403. */
  404. public function set_address( $address ) {
  405. $this->address_1 = $address;
  406. }
  407. /**
  408. * Sets session data for the $address.
  409. *
  410. * @param mixed $address
  411. */
  412. public function set_address_2( $address ) {
  413. $this->address_2 = $address;
  414. }
  415. /**
  416. * Sets session data for the location.
  417. *
  418. * @param string $country
  419. * @param string $state (default: '')
  420. * @param string $postcode (default: '')
  421. * @param string $city (default: '')
  422. */
  423. public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
  424. $this->shipping_country = $country;
  425. $this->shipping_state = $state;
  426. $this->shipping_postcode = $postcode;
  427. $this->shipping_city = $city;
  428. }
  429. /**
  430. * Sets session data for the country.
  431. *
  432. * @param string $country
  433. */
  434. public function set_shipping_country( $country ) {
  435. $this->shipping_country = $country;
  436. }
  437. /**
  438. * Sets session data for the state.
  439. *
  440. * @param string $state
  441. */
  442. public function set_shipping_state( $state ) {
  443. $this->shipping_state = $state;
  444. }
  445. /**
  446. * Sets session data for the postcode.
  447. *
  448. * @param string $postcode
  449. */
  450. public function set_shipping_postcode( $postcode ) {
  451. $this->shipping_postcode = $postcode;
  452. }
  453. /**
  454. * Sets session data for the city.
  455. *
  456. * @param string $city
  457. */
  458. public function set_shipping_city( $city ) {
  459. $this->shipping_city = $city;
  460. }
  461. /**
  462. * Sets session data for the address.
  463. *
  464. * @param string $address
  465. */
  466. public function set_shipping_address( $address ) {
  467. $this->shipping_address_1 = $address;
  468. }
  469. /**
  470. * Sets session data for the address_2.
  471. *
  472. * @param string $address
  473. */
  474. public function set_shipping_address_2( $address ) {
  475. $this->shipping_address_2 = $address;
  476. }
  477. /**
  478. * Sets session data for the tax exemption.
  479. *
  480. * @param bool $is_vat_exempt
  481. */
  482. public function set_is_vat_exempt( $is_vat_exempt ) {
  483. $this->is_vat_exempt = $is_vat_exempt;
  484. }
  485. /**
  486. * Calculated shipping.
  487. *
  488. * @param boolean $calculated
  489. */
  490. public function calculated_shipping( $calculated = true ) {
  491. $this->calculated_shipping = $calculated;
  492. }
  493. /**
  494. * Gets a user's downloadable products if they are logged in.
  495. *
  496. * @return array Array of downloadable products
  497. */
  498. public function get_downloadable_products() {
  499. $downloads = array();
  500. if ( is_user_logged_in() ) {
  501. $downloads = wc_get_customer_available_downloads( get_current_user_id() );
  502. }
  503. return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
  504. }
  505. }