PageRenderTime 22ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/shop quần áo starloveshop.com/wp-content/plugins/woocommerce/includes/class-wc-customer.php

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