PageRenderTime 30ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

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

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