Nav apraksta

class-wc-customer.php 29KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. <?php
  2. /**
  3. * The WooCommerce customer class handles storage of the current customer's data, such as location.
  4. *
  5. * @package WooCommerce\Classes
  6. * @version 3.0.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. require_once dirname( __FILE__ ) . '/legacy/class-wc-legacy-customer.php';
  10. /**
  11. * Customer class.
  12. */
  13. class WC_Customer extends WC_Legacy_Customer {
  14. /**
  15. * Stores customer data.
  16. *
  17. * @var array
  18. */
  19. protected $data = array(
  20. 'date_created' => null,
  21. 'date_modified' => null,
  22. 'email' => '',
  23. 'first_name' => '',
  24. 'last_name' => '',
  25. 'display_name' => '',
  26. 'role' => 'customer',
  27. 'username' => '',
  28. 'billing' => array(
  29. 'first_name' => '',
  30. 'last_name' => '',
  31. 'company' => '',
  32. 'address_1' => '',
  33. 'address_2' => '',
  34. 'city' => '',
  35. 'postcode' => '',
  36. 'country' => '',
  37. 'state' => '',
  38. 'email' => '',
  39. 'phone' => '',
  40. ),
  41. 'shipping' => array(
  42. 'first_name' => '',
  43. 'last_name' => '',
  44. 'company' => '',
  45. 'address_1' => '',
  46. 'address_2' => '',
  47. 'city' => '',
  48. 'postcode' => '',
  49. 'country' => '',
  50. 'state' => '',
  51. 'phone' => '',
  52. ),
  53. 'is_paying_customer' => false,
  54. );
  55. /**
  56. * Stores a password if this needs to be changed. Write-only and hidden from _data.
  57. *
  58. * @var string
  59. */
  60. protected $password = '';
  61. /**
  62. * Stores if user is VAT exempt for this session.
  63. *
  64. * @var string
  65. */
  66. protected $is_vat_exempt = false;
  67. /**
  68. * Stores if user has calculated shipping in this session.
  69. *
  70. * @var string
  71. */
  72. protected $calculated_shipping = false;
  73. /**
  74. * This is the name of this object type.
  75. *
  76. * @since 5.6.0
  77. * @var string
  78. */
  79. protected $object_type = 'customer';
  80. /**
  81. * Load customer data based on how WC_Customer is called.
  82. *
  83. * If $customer is 'new', you can build a new WC_Customer object. If it's empty, some
  84. * data will be pulled from the session for the current user/customer.
  85. *
  86. * @param WC_Customer|int $data Customer ID or data.
  87. * @param bool $is_session True if this is the customer session.
  88. * @throws Exception If customer cannot be read/found and $data is set.
  89. */
  90. public function __construct( $data = 0, $is_session = false ) {
  91. parent::__construct( $data );
  92. if ( $data instanceof WC_Customer ) {
  93. $this->set_id( absint( $data->get_id() ) );
  94. } elseif ( is_numeric( $data ) ) {
  95. $this->set_id( $data );
  96. }
  97. $this->data_store = WC_Data_Store::load( 'customer' );
  98. // If we have an ID, load the user from the DB.
  99. if ( $this->get_id() ) {
  100. try {
  101. $this->data_store->read( $this );
  102. } catch ( Exception $e ) {
  103. $this->set_id( 0 );
  104. $this->set_object_read( true );
  105. }
  106. } else {
  107. $this->set_object_read( true );
  108. }
  109. // If this is a session, set or change the data store to sessions. Changes do not persist in the database.
  110. if ( $is_session && isset( WC()->session ) ) {
  111. $this->data_store = WC_Data_Store::load( 'customer-session' );
  112. $this->data_store->read( $this );
  113. }
  114. }
  115. /**
  116. * Delete a customer and reassign posts..
  117. *
  118. * @param int $reassign Reassign posts and links to new User ID.
  119. * @since 3.0.0
  120. * @return bool
  121. */
  122. public function delete_and_reassign( $reassign = null ) {
  123. if ( $this->data_store ) {
  124. $this->data_store->delete(
  125. $this,
  126. array(
  127. 'force_delete' => true,
  128. 'reassign' => $reassign,
  129. )
  130. );
  131. $this->set_id( 0 );
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * Is customer outside base country (for tax purposes)?
  138. *
  139. * @return bool
  140. */
  141. public function is_customer_outside_base() {
  142. list( $country, $state ) = $this->get_taxable_address();
  143. if ( $country ) {
  144. $default = wc_get_base_location();
  145. if ( $default['country'] !== $country ) {
  146. return true;
  147. }
  148. if ( $default['state'] && $default['state'] !== $state ) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. /**
  155. * Return this customer's avatar.
  156. *
  157. * @since 3.0.0
  158. * @return string
  159. */
  160. public function get_avatar_url() {
  161. return get_avatar_url( $this->get_email() );
  162. }
  163. /**
  164. * Get taxable address.
  165. *
  166. * @return array
  167. */
  168. public function get_taxable_address() {
  169. $tax_based_on = get_option( 'woocommerce_tax_based_on' );
  170. // Check shipping method at this point to see if we need special handling.
  171. if ( true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && count( array_intersect( wc_get_chosen_shipping_method_ids(), apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) ) ) ) > 0 ) {
  172. $tax_based_on = 'base';
  173. }
  174. if ( 'base' === $tax_based_on ) {
  175. $country = WC()->countries->get_base_country();
  176. $state = WC()->countries->get_base_state();
  177. $postcode = WC()->countries->get_base_postcode();
  178. $city = WC()->countries->get_base_city();
  179. } elseif ( 'billing' === $tax_based_on ) {
  180. $country = $this->get_billing_country();
  181. $state = $this->get_billing_state();
  182. $postcode = $this->get_billing_postcode();
  183. $city = $this->get_billing_city();
  184. } else {
  185. $country = $this->get_shipping_country();
  186. $state = $this->get_shipping_state();
  187. $postcode = $this->get_shipping_postcode();
  188. $city = $this->get_shipping_city();
  189. }
  190. return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
  191. }
  192. /**
  193. * Gets a customer's downloadable products.
  194. *
  195. * @return array Array of downloadable products
  196. */
  197. public function get_downloadable_products() {
  198. $downloads = array();
  199. if ( $this->get_id() ) {
  200. $downloads = wc_get_customer_available_downloads( $this->get_id() );
  201. }
  202. return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
  203. }
  204. /**
  205. * Is customer VAT exempt?
  206. *
  207. * @return bool
  208. */
  209. public function is_vat_exempt() {
  210. return $this->get_is_vat_exempt();
  211. }
  212. /**
  213. * Has calculated shipping?
  214. *
  215. * @return bool
  216. */
  217. public function has_calculated_shipping() {
  218. return $this->get_calculated_shipping();
  219. }
  220. /**
  221. * Indicates if the customer has a non-empty shipping address.
  222. *
  223. * Note that this does not indicate if the customer's shipping address
  224. * is complete, only that one or more fields are populated.
  225. *
  226. * @since 5.3.0
  227. *
  228. * @return bool
  229. */
  230. public function has_shipping_address() {
  231. foreach ( $this->get_shipping() as $address_field ) {
  232. // Trim guards against a case where a subset of saved shipping address fields contain whitespace.
  233. if ( strlen( trim( $address_field ) ) > 0 ) {
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. /**
  240. * Get if customer is VAT exempt?
  241. *
  242. * @since 3.0.0
  243. * @return bool
  244. */
  245. public function get_is_vat_exempt() {
  246. return $this->is_vat_exempt;
  247. }
  248. /**
  249. * Get password (only used when updating the user object).
  250. *
  251. * @return string
  252. */
  253. public function get_password() {
  254. return $this->password;
  255. }
  256. /**
  257. * Has customer calculated shipping?
  258. *
  259. * @return bool
  260. */
  261. public function get_calculated_shipping() {
  262. return $this->calculated_shipping;
  263. }
  264. /**
  265. * Set if customer has tax exemption.
  266. *
  267. * @param bool $is_vat_exempt If is vat exempt.
  268. */
  269. public function set_is_vat_exempt( $is_vat_exempt ) {
  270. $this->is_vat_exempt = wc_string_to_bool( $is_vat_exempt );
  271. }
  272. /**
  273. * Calculated shipping?
  274. *
  275. * @param bool $calculated If shipping is calculated.
  276. */
  277. public function set_calculated_shipping( $calculated = true ) {
  278. $this->calculated_shipping = wc_string_to_bool( $calculated );
  279. }
  280. /**
  281. * Set customer's password.
  282. *
  283. * @since 3.0.0
  284. * @param string $password Password.
  285. */
  286. public function set_password( $password ) {
  287. $this->password = $password;
  288. }
  289. /**
  290. * Gets the customers last order.
  291. *
  292. * @return WC_Order|false
  293. */
  294. public function get_last_order() {
  295. return $this->data_store->get_last_order( $this );
  296. }
  297. /**
  298. * Return the number of orders this customer has.
  299. *
  300. * @return integer
  301. */
  302. public function get_order_count() {
  303. return $this->data_store->get_order_count( $this );
  304. }
  305. /**
  306. * Return how much money this customer has spent.
  307. *
  308. * @return float
  309. */
  310. public function get_total_spent() {
  311. return $this->data_store->get_total_spent( $this );
  312. }
  313. /*
  314. |--------------------------------------------------------------------------
  315. | Getters
  316. |--------------------------------------------------------------------------
  317. */
  318. /**
  319. * Return the customer's username.
  320. *
  321. * @since 3.0.0
  322. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  323. * @return string
  324. */
  325. public function get_username( $context = 'view' ) {
  326. return $this->get_prop( 'username', $context );
  327. }
  328. /**
  329. * Return the customer's email.
  330. *
  331. * @since 3.0.0
  332. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  333. * @return string
  334. */
  335. public function get_email( $context = 'view' ) {
  336. return $this->get_prop( 'email', $context );
  337. }
  338. /**
  339. * Return customer's first name.
  340. *
  341. * @since 3.0.0
  342. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  343. * @return string
  344. */
  345. public function get_first_name( $context = 'view' ) {
  346. return $this->get_prop( 'first_name', $context );
  347. }
  348. /**
  349. * Return customer's last name.
  350. *
  351. * @since 3.0.0
  352. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  353. * @return string
  354. */
  355. public function get_last_name( $context = 'view' ) {
  356. return $this->get_prop( 'last_name', $context );
  357. }
  358. /**
  359. * Return customer's display name.
  360. *
  361. * @since 3.1.0
  362. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  363. * @return string
  364. */
  365. public function get_display_name( $context = 'view' ) {
  366. return $this->get_prop( 'display_name', $context );
  367. }
  368. /**
  369. * Return customer's user role.
  370. *
  371. * @since 3.0.0
  372. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  373. * @return string
  374. */
  375. public function get_role( $context = 'view' ) {
  376. return $this->get_prop( 'role', $context );
  377. }
  378. /**
  379. * Return the date this customer was created.
  380. *
  381. * @since 3.0.0
  382. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  383. * @return WC_DateTime|null object if the date is set or null if there is no date.
  384. */
  385. public function get_date_created( $context = 'view' ) {
  386. return $this->get_prop( 'date_created', $context );
  387. }
  388. /**
  389. * Return the date this customer was last updated.
  390. *
  391. * @since 3.0.0
  392. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  393. * @return WC_DateTime|null object if the date is set or null if there is no date.
  394. */
  395. public function get_date_modified( $context = 'view' ) {
  396. return $this->get_prop( 'date_modified', $context );
  397. }
  398. /**
  399. * Gets a prop for a getter method.
  400. *
  401. * @since 3.0.0
  402. * @param string $prop Name of prop to get.
  403. * @param string $address billing or shipping.
  404. * @param string $context What the value is for. Valid values are 'view' and 'edit'. What the value is for. Valid values are view and edit.
  405. * @return mixed
  406. */
  407. protected function get_address_prop( $prop, $address = 'billing', $context = 'view' ) {
  408. $value = null;
  409. if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
  410. $value = isset( $this->changes[ $address ][ $prop ] ) ? $this->changes[ $address ][ $prop ] : $this->data[ $address ][ $prop ];
  411. if ( 'view' === $context ) {
  412. $value = apply_filters( $this->get_hook_prefix() . $address . '_' . $prop, $value, $this );
  413. }
  414. }
  415. return $value;
  416. }
  417. /**
  418. * Get billing.
  419. *
  420. * @since 3.2.0
  421. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  422. * @return array
  423. */
  424. public function get_billing( $context = 'view' ) {
  425. $value = null;
  426. $prop = 'billing';
  427. if ( array_key_exists( $prop, $this->data ) ) {
  428. $changes = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : array();
  429. $value = array_merge( $this->data[ $prop ], $changes );
  430. if ( 'view' === $context ) {
  431. $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
  432. }
  433. }
  434. return $value;
  435. }
  436. /**
  437. * Get billing_first_name.
  438. *
  439. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  440. * @return string
  441. */
  442. public function get_billing_first_name( $context = 'view' ) {
  443. return $this->get_address_prop( 'first_name', 'billing', $context );
  444. }
  445. /**
  446. * Get billing_last_name.
  447. *
  448. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  449. * @return string
  450. */
  451. public function get_billing_last_name( $context = 'view' ) {
  452. return $this->get_address_prop( 'last_name', 'billing', $context );
  453. }
  454. /**
  455. * Get billing_company.
  456. *
  457. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  458. * @return string
  459. */
  460. public function get_billing_company( $context = 'view' ) {
  461. return $this->get_address_prop( 'company', 'billing', $context );
  462. }
  463. /**
  464. * Get billing_address_1.
  465. *
  466. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  467. * @return string
  468. */
  469. public function get_billing_address( $context = 'view' ) {
  470. return $this->get_billing_address_1( $context );
  471. }
  472. /**
  473. * Get billing_address_1.
  474. *
  475. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  476. * @return string
  477. */
  478. public function get_billing_address_1( $context = 'view' ) {
  479. return $this->get_address_prop( 'address_1', 'billing', $context );
  480. }
  481. /**
  482. * Get billing_address_2.
  483. *
  484. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  485. * @return string $value
  486. */
  487. public function get_billing_address_2( $context = 'view' ) {
  488. return $this->get_address_prop( 'address_2', 'billing', $context );
  489. }
  490. /**
  491. * Get billing_city.
  492. *
  493. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  494. * @return string $value
  495. */
  496. public function get_billing_city( $context = 'view' ) {
  497. return $this->get_address_prop( 'city', 'billing', $context );
  498. }
  499. /**
  500. * Get billing_state.
  501. *
  502. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  503. * @return string
  504. */
  505. public function get_billing_state( $context = 'view' ) {
  506. return $this->get_address_prop( 'state', 'billing', $context );
  507. }
  508. /**
  509. * Get billing_postcode.
  510. *
  511. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  512. * @return string
  513. */
  514. public function get_billing_postcode( $context = 'view' ) {
  515. return $this->get_address_prop( 'postcode', 'billing', $context );
  516. }
  517. /**
  518. * Get billing_country.
  519. *
  520. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  521. * @return string
  522. */
  523. public function get_billing_country( $context = 'view' ) {
  524. return $this->get_address_prop( 'country', 'billing', $context );
  525. }
  526. /**
  527. * Get billing_email.
  528. *
  529. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  530. * @return string
  531. */
  532. public function get_billing_email( $context = 'view' ) {
  533. return $this->get_address_prop( 'email', 'billing', $context );
  534. }
  535. /**
  536. * Get billing_phone.
  537. *
  538. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  539. * @return string
  540. */
  541. public function get_billing_phone( $context = 'view' ) {
  542. return $this->get_address_prop( 'phone', 'billing', $context );
  543. }
  544. /**
  545. * Get shipping.
  546. *
  547. * @since 3.2.0
  548. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  549. * @return array
  550. */
  551. public function get_shipping( $context = 'view' ) {
  552. $value = null;
  553. $prop = 'shipping';
  554. if ( array_key_exists( $prop, $this->data ) ) {
  555. $changes = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : array();
  556. $value = array_merge( $this->data[ $prop ], $changes );
  557. if ( 'view' === $context ) {
  558. $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
  559. }
  560. }
  561. return $value;
  562. }
  563. /**
  564. * Get shipping_first_name.
  565. *
  566. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  567. * @return string
  568. */
  569. public function get_shipping_first_name( $context = 'view' ) {
  570. return $this->get_address_prop( 'first_name', 'shipping', $context );
  571. }
  572. /**
  573. * Get shipping_last_name.
  574. *
  575. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  576. * @return string
  577. */
  578. public function get_shipping_last_name( $context = 'view' ) {
  579. return $this->get_address_prop( 'last_name', 'shipping', $context );
  580. }
  581. /**
  582. * Get shipping_company.
  583. *
  584. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  585. * @return string
  586. */
  587. public function get_shipping_company( $context = 'view' ) {
  588. return $this->get_address_prop( 'company', 'shipping', $context );
  589. }
  590. /**
  591. * Get shipping_address_1.
  592. *
  593. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  594. * @return string
  595. */
  596. public function get_shipping_address( $context = 'view' ) {
  597. return $this->get_shipping_address_1( $context );
  598. }
  599. /**
  600. * Get shipping_address_1.
  601. *
  602. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  603. * @return string
  604. */
  605. public function get_shipping_address_1( $context = 'view' ) {
  606. return $this->get_address_prop( 'address_1', 'shipping', $context );
  607. }
  608. /**
  609. * Get shipping_address_2.
  610. *
  611. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  612. * @return string
  613. */
  614. public function get_shipping_address_2( $context = 'view' ) {
  615. return $this->get_address_prop( 'address_2', 'shipping', $context );
  616. }
  617. /**
  618. * Get shipping_city.
  619. *
  620. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  621. * @return string
  622. */
  623. public function get_shipping_city( $context = 'view' ) {
  624. return $this->get_address_prop( 'city', 'shipping', $context );
  625. }
  626. /**
  627. * Get shipping_state.
  628. *
  629. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  630. * @return string
  631. */
  632. public function get_shipping_state( $context = 'view' ) {
  633. return $this->get_address_prop( 'state', 'shipping', $context );
  634. }
  635. /**
  636. * Get shipping_postcode.
  637. *
  638. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  639. * @return string
  640. */
  641. public function get_shipping_postcode( $context = 'view' ) {
  642. return $this->get_address_prop( 'postcode', 'shipping', $context );
  643. }
  644. /**
  645. * Get shipping_country.
  646. *
  647. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  648. * @return string
  649. */
  650. public function get_shipping_country( $context = 'view' ) {
  651. return $this->get_address_prop( 'country', 'shipping', $context );
  652. }
  653. /**
  654. * Get shipping phone.
  655. *
  656. * @since 5.6.0
  657. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  658. * @return string
  659. */
  660. public function get_shipping_phone( $context = 'view' ) {
  661. return $this->get_address_prop( 'phone', 'shipping', $context );
  662. }
  663. /**
  664. * Is the user a paying customer?
  665. *
  666. * @since 3.0.0
  667. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  668. * @return bool
  669. */
  670. public function get_is_paying_customer( $context = 'view' ) {
  671. return $this->get_prop( 'is_paying_customer', $context );
  672. }
  673. /*
  674. |--------------------------------------------------------------------------
  675. | Setters
  676. |--------------------------------------------------------------------------
  677. */
  678. /**
  679. * Set customer's username.
  680. *
  681. * @since 3.0.0
  682. * @param string $username Username.
  683. */
  684. public function set_username( $username ) {
  685. $this->set_prop( 'username', $username );
  686. }
  687. /**
  688. * Set customer's email.
  689. *
  690. * @since 3.0.0
  691. * @param string $value Email.
  692. */
  693. public function set_email( $value ) {
  694. if ( $value && ! is_email( $value ) ) {
  695. $this->error( 'customer_invalid_email', __( 'Invalid email address', 'woocommerce' ) );
  696. }
  697. $this->set_prop( 'email', sanitize_email( $value ) );
  698. }
  699. /**
  700. * Set customer's first name.
  701. *
  702. * @since 3.0.0
  703. * @param string $first_name First name.
  704. */
  705. public function set_first_name( $first_name ) {
  706. $this->set_prop( 'first_name', $first_name );
  707. }
  708. /**
  709. * Set customer's last name.
  710. *
  711. * @since 3.0.0
  712. * @param string $last_name Last name.
  713. */
  714. public function set_last_name( $last_name ) {
  715. $this->set_prop( 'last_name', $last_name );
  716. }
  717. /**
  718. * Set customer's display name.
  719. *
  720. * @since 3.1.0
  721. * @param string $display_name Display name.
  722. */
  723. public function set_display_name( $display_name ) {
  724. /* translators: 1: first name 2: last name */
  725. $this->set_prop( 'display_name', is_email( $display_name ) ? sprintf( _x( '%1$s %2$s', 'display name', 'woocommerce' ), $this->get_first_name(), $this->get_last_name() ) : $display_name );
  726. }
  727. /**
  728. * Set customer's user role(s).
  729. *
  730. * @since 3.0.0
  731. * @param mixed $role User role.
  732. */
  733. public function set_role( $role ) {
  734. global $wp_roles;
  735. if ( $role && ! empty( $wp_roles->roles ) && ! in_array( $role, array_keys( $wp_roles->roles ), true ) ) {
  736. $this->error( 'customer_invalid_role', __( 'Invalid role', 'woocommerce' ) );
  737. }
  738. $this->set_prop( 'role', $role );
  739. }
  740. /**
  741. * Set the date this customer was last updated.
  742. *
  743. * @since 3.0.0
  744. * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
  745. */
  746. public function set_date_created( $date = null ) {
  747. $this->set_date_prop( 'date_created', $date );
  748. }
  749. /**
  750. * Set the date this customer was last updated.
  751. *
  752. * @since 3.0.0
  753. * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
  754. */
  755. public function set_date_modified( $date = null ) {
  756. $this->set_date_prop( 'date_modified', $date );
  757. }
  758. /**
  759. * Set customer address to match shop base address.
  760. *
  761. * @since 3.0.0
  762. */
  763. public function set_billing_address_to_base() {
  764. $base = wc_get_customer_default_location();
  765. $this->set_billing_location( $base['country'], $base['state'], '', '' );
  766. }
  767. /**
  768. * Set customer shipping address to base address.
  769. *
  770. * @since 3.0.0
  771. */
  772. public function set_shipping_address_to_base() {
  773. $base = wc_get_customer_default_location();
  774. $this->set_shipping_location( $base['country'], $base['state'], '', '' );
  775. }
  776. /**
  777. * Sets all address info at once.
  778. *
  779. * @param string $country Country.
  780. * @param string $state State.
  781. * @param string $postcode Postcode.
  782. * @param string $city City.
  783. */
  784. public function set_billing_location( $country, $state = '', $postcode = '', $city = '' ) {
  785. $address_data = $this->get_prop( 'billing', 'edit' );
  786. $address_data['address_1'] = '';
  787. $address_data['address_2'] = '';
  788. $address_data['city'] = $city;
  789. $address_data['state'] = $state;
  790. $address_data['postcode'] = $postcode;
  791. $address_data['country'] = $country;
  792. $this->set_prop( 'billing', $address_data );
  793. }
  794. /**
  795. * Sets all shipping info at once.
  796. *
  797. * @param string $country Country.
  798. * @param string $state State.
  799. * @param string $postcode Postcode.
  800. * @param string $city City.
  801. */
  802. public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
  803. $address_data = $this->get_prop( 'shipping', 'edit' );
  804. $address_data['address_1'] = '';
  805. $address_data['address_2'] = '';
  806. $address_data['city'] = $city;
  807. $address_data['state'] = $state;
  808. $address_data['postcode'] = $postcode;
  809. $address_data['country'] = $country;
  810. $this->set_prop( 'shipping', $address_data );
  811. }
  812. /**
  813. * Sets a prop for a setter method.
  814. *
  815. * @since 3.0.0
  816. * @param string $prop Name of prop to set.
  817. * @param string $address Name of address to set. billing or shipping.
  818. * @param mixed $value Value of the prop.
  819. */
  820. protected function set_address_prop( $prop, $address, $value ) {
  821. if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
  822. if ( true === $this->object_read ) {
  823. if ( $value !== $this->data[ $address ][ $prop ] || ( isset( $this->changes[ $address ] ) && array_key_exists( $prop, $this->changes[ $address ] ) ) ) {
  824. $this->changes[ $address ][ $prop ] = $value;
  825. }
  826. } else {
  827. $this->data[ $address ][ $prop ] = $value;
  828. }
  829. }
  830. }
  831. /**
  832. * Set billing_first_name.
  833. *
  834. * @param string $value Billing first name.
  835. */
  836. public function set_billing_first_name( $value ) {
  837. $this->set_address_prop( 'first_name', 'billing', $value );
  838. }
  839. /**
  840. * Set billing_last_name.
  841. *
  842. * @param string $value Billing last name.
  843. */
  844. public function set_billing_last_name( $value ) {
  845. $this->set_address_prop( 'last_name', 'billing', $value );
  846. }
  847. /**
  848. * Set billing_company.
  849. *
  850. * @param string $value Billing company.
  851. */
  852. public function set_billing_company( $value ) {
  853. $this->set_address_prop( 'company', 'billing', $value );
  854. }
  855. /**
  856. * Set billing_address_1.
  857. *
  858. * @param string $value Billing address line 1.
  859. */
  860. public function set_billing_address( $value ) {
  861. $this->set_billing_address_1( $value );
  862. }
  863. /**
  864. * Set billing_address_1.
  865. *
  866. * @param string $value Billing address line 1.
  867. */
  868. public function set_billing_address_1( $value ) {
  869. $this->set_address_prop( 'address_1', 'billing', $value );
  870. }
  871. /**
  872. * Set billing_address_2.
  873. *
  874. * @param string $value Billing address line 2.
  875. */
  876. public function set_billing_address_2( $value ) {
  877. $this->set_address_prop( 'address_2', 'billing', $value );
  878. }
  879. /**
  880. * Set billing_city.
  881. *
  882. * @param string $value Billing city.
  883. */
  884. public function set_billing_city( $value ) {
  885. $this->set_address_prop( 'city', 'billing', $value );
  886. }
  887. /**
  888. * Set billing_state.
  889. *
  890. * @param string $value Billing state.
  891. */
  892. public function set_billing_state( $value ) {
  893. $this->set_address_prop( 'state', 'billing', $value );
  894. }
  895. /**
  896. * Set billing_postcode.
  897. *
  898. * @param string $value Billing postcode.
  899. */
  900. public function set_billing_postcode( $value ) {
  901. $this->set_address_prop( 'postcode', 'billing', $value );
  902. }
  903. /**
  904. * Set billing_country.
  905. *
  906. * @param string $value Billing country.
  907. */
  908. public function set_billing_country( $value ) {
  909. $this->set_address_prop( 'country', 'billing', $value );
  910. }
  911. /**
  912. * Set billing_email.
  913. *
  914. * @param string $value Billing email.
  915. */
  916. public function set_billing_email( $value ) {
  917. if ( $value && ! is_email( $value ) ) {
  918. $this->error( 'customer_invalid_billing_email', __( 'Invalid billing email address', 'woocommerce' ) );
  919. }
  920. $this->set_address_prop( 'email', 'billing', sanitize_email( $value ) );
  921. }
  922. /**
  923. * Set billing_phone.
  924. *
  925. * @param string $value Billing phone.
  926. */
  927. public function set_billing_phone( $value ) {
  928. $this->set_address_prop( 'phone', 'billing', $value );
  929. }
  930. /**
  931. * Set shipping_first_name.
  932. *
  933. * @param string $value Shipping first name.
  934. */
  935. public function set_shipping_first_name( $value ) {
  936. $this->set_address_prop( 'first_name', 'shipping', $value );
  937. }
  938. /**
  939. * Set shipping_last_name.
  940. *
  941. * @param string $value Shipping last name.
  942. */
  943. public function set_shipping_last_name( $value ) {
  944. $this->set_address_prop( 'last_name', 'shipping', $value );
  945. }
  946. /**
  947. * Set shipping_company.
  948. *
  949. * @param string $value Shipping company.
  950. */
  951. public function set_shipping_company( $value ) {
  952. $this->set_address_prop( 'company', 'shipping', $value );
  953. }
  954. /**
  955. * Set shipping_address_1.
  956. *
  957. * @param string $value Shipping address line 1.
  958. */
  959. public function set_shipping_address( $value ) {
  960. $this->set_shipping_address_1( $value );
  961. }
  962. /**
  963. * Set shipping_address_1.
  964. *
  965. * @param string $value Shipping address line 1.
  966. */
  967. public function set_shipping_address_1( $value ) {
  968. $this->set_address_prop( 'address_1', 'shipping', $value );
  969. }
  970. /**
  971. * Set shipping_address_2.
  972. *
  973. * @param string $value Shipping address line 2.
  974. */
  975. public function set_shipping_address_2( $value ) {
  976. $this->set_address_prop( 'address_2', 'shipping', $value );
  977. }
  978. /**
  979. * Set shipping_city.
  980. *
  981. * @param string $value Shipping city.
  982. */
  983. public function set_shipping_city( $value ) {
  984. $this->set_address_prop( 'city', 'shipping', $value );
  985. }
  986. /**
  987. * Set shipping_state.
  988. *
  989. * @param string $value Shipping state.
  990. */
  991. public function set_shipping_state( $value ) {
  992. $this->set_address_prop( 'state', 'shipping', $value );
  993. }
  994. /**
  995. * Set shipping_postcode.
  996. *
  997. * @param string $value Shipping postcode.
  998. */
  999. public function set_shipping_postcode( $value ) {
  1000. $this->set_address_prop( 'postcode', 'shipping', $value );
  1001. }
  1002. /**
  1003. * Set shipping_country.
  1004. *
  1005. * @param string $value Shipping country.
  1006. */
  1007. public function set_shipping_country( $value ) {
  1008. $this->set_address_prop( 'country', 'shipping', $value );
  1009. }
  1010. /**
  1011. * Set shipping phone.
  1012. *
  1013. * @since 5.6.0
  1014. * @param string $value Shipping phone.
  1015. */
  1016. public function set_shipping_phone( $value ) {
  1017. $this->set_address_prop( 'phone', 'shipping', $value );
  1018. }
  1019. /**
  1020. * Set if the user a paying customer.
  1021. *
  1022. * @since 3.0.0
  1023. * @param bool $is_paying_customer If is a paying customer.
  1024. */
  1025. public function set_is_paying_customer( $is_paying_customer ) {
  1026. $this->set_prop( 'is_paying_customer', (bool) $is_paying_customer );
  1027. }
  1028. }