class-wc-customer.php 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. 'state' => '',
  36. 'postcode' => '',
  37. 'country' => '',
  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. 'state' => '',
  49. 'postcode' => '',
  50. 'country' => '',
  51. ),
  52. 'is_paying_customer' => false,
  53. );
  54. /**
  55. * Stores a password if this needs to be changed. Write-only and hidden from _data.
  56. *
  57. * @var string
  58. */
  59. protected $password = '';
  60. /**
  61. * Stores if user is VAT exempt for this session.
  62. *
  63. * @var string
  64. */
  65. protected $is_vat_exempt = false;
  66. /**
  67. * Stores if user has calculated shipping in this session.
  68. *
  69. * @var string
  70. */
  71. protected $calculated_shipping = false;
  72. /**
  73. * Load customer data based on how WC_Customer is called.
  74. *
  75. * If $customer is 'new', you can build a new WC_Customer object. If it's empty, some
  76. * data will be pulled from the session for the current user/customer.
  77. *
  78. * @param WC_Customer|int $data Customer ID or data.
  79. * @param bool $is_session True if this is the customer session.
  80. * @throws Exception If customer cannot be read/found and $data is set.
  81. */
  82. public function __construct( $data = 0, $is_session = false ) {
  83. parent::__construct( $data );
  84. if ( $data instanceof WC_Customer ) {
  85. $this->set_id( absint( $data->get_id() ) );
  86. } elseif ( is_numeric( $data ) ) {
  87. $this->set_id( $data );
  88. }
  89. $this->data_store = WC_Data_Store::load( 'customer' );
  90. // If we have an ID, load the user from the DB.
  91. if ( $this->get_id() ) {
  92. try {
  93. $this->data_store->read( $this );
  94. } catch ( Exception $e ) {
  95. $this->set_id( 0 );
  96. $this->set_object_read( true );
  97. }
  98. } else {
  99. $this->set_object_read( true );
  100. }
  101. // If this is a session, set or change the data store to sessions. Changes do not persist in the database.
  102. if ( $is_session ) {
  103. $this->data_store = WC_Data_Store::load( 'customer-session' );
  104. $this->data_store->read( $this );
  105. }
  106. }
  107. /**
  108. * Prefix for action and filter hooks on data.
  109. *
  110. * @since 3.0.0
  111. * @return string
  112. */
  113. protected function get_hook_prefix() {
  114. return 'woocommerce_customer_get_';
  115. }
  116. /**
  117. * Delete a customer and reassign posts..
  118. *
  119. * @param int $reassign Reassign posts and links to new User ID.
  120. * @since 3.0.0
  121. * @return bool
  122. */
  123. public function delete_and_reassign( $reassign = null ) {
  124. if ( $this->data_store ) {
  125. $this->data_store->delete(
  126. $this, 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. * Get if customer is VAT exempt?
  222. *
  223. * @since 3.0.0
  224. * @return bool
  225. */
  226. public function get_is_vat_exempt() {
  227. return $this->is_vat_exempt;
  228. }
  229. /**
  230. * Get password (only used when updating the user object).
  231. *
  232. * @return string
  233. */
  234. public function get_password() {
  235. return $this->password;
  236. }
  237. /**
  238. * Has customer calculated shipping?
  239. *
  240. * @return bool
  241. */
  242. public function get_calculated_shipping() {
  243. return $this->calculated_shipping;
  244. }
  245. /**
  246. * Set if customer has tax exemption.
  247. *
  248. * @param bool $is_vat_exempt If is vat exempt.
  249. */
  250. public function set_is_vat_exempt( $is_vat_exempt ) {
  251. $this->is_vat_exempt = wc_string_to_bool( $is_vat_exempt );
  252. }
  253. /**
  254. * Calculated shipping?
  255. *
  256. * @param bool $calculated If shipping is calculated.
  257. */
  258. public function set_calculated_shipping( $calculated = true ) {
  259. $this->calculated_shipping = wc_string_to_bool( $calculated );
  260. }
  261. /**
  262. * Set customer's password.
  263. *
  264. * @since 3.0.0
  265. * @param string $password Password.
  266. */
  267. public function set_password( $password ) {
  268. $this->password = wc_clean( $password );
  269. }
  270. /**
  271. * Gets the customers last order.
  272. *
  273. * @return WC_Order|false
  274. */
  275. public function get_last_order() {
  276. return $this->data_store->get_last_order( $this );
  277. }
  278. /**
  279. * Return the number of orders this customer has.
  280. *
  281. * @return integer
  282. */
  283. public function get_order_count() {
  284. return $this->data_store->get_order_count( $this );
  285. }
  286. /**
  287. * Return how much money this customer has spent.
  288. *
  289. * @return float
  290. */
  291. public function get_total_spent() {
  292. return $this->data_store->get_total_spent( $this );
  293. }
  294. /*
  295. |--------------------------------------------------------------------------
  296. | Getters
  297. |--------------------------------------------------------------------------
  298. */
  299. /**
  300. * Return the customer's username.
  301. *
  302. * @since 3.0.0
  303. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  304. * @return string
  305. */
  306. public function get_username( $context = 'view' ) {
  307. return $this->get_prop( 'username', $context );
  308. }
  309. /**
  310. * Return the customer's email.
  311. *
  312. * @since 3.0.0
  313. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  314. * @return string
  315. */
  316. public function get_email( $context = 'view' ) {
  317. return $this->get_prop( 'email', $context );
  318. }
  319. /**
  320. * Return customer's first name.
  321. *
  322. * @since 3.0.0
  323. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  324. * @return string
  325. */
  326. public function get_first_name( $context = 'view' ) {
  327. return $this->get_prop( 'first_name', $context );
  328. }
  329. /**
  330. * Return customer's last name.
  331. *
  332. * @since 3.0.0
  333. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  334. * @return string
  335. */
  336. public function get_last_name( $context = 'view' ) {
  337. return $this->get_prop( 'last_name', $context );
  338. }
  339. /**
  340. * Return customer's display name.
  341. *
  342. * @since 3.1.0
  343. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  344. * @return string
  345. */
  346. public function get_display_name( $context = 'view' ) {
  347. return $this->get_prop( 'display_name', $context );
  348. }
  349. /**
  350. * Return customer's user role.
  351. *
  352. * @since 3.0.0
  353. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  354. * @return string
  355. */
  356. public function get_role( $context = 'view' ) {
  357. return $this->get_prop( 'role', $context );
  358. }
  359. /**
  360. * Return the date this customer was created.
  361. *
  362. * @since 3.0.0
  363. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  364. * @return WC_DateTime|null object if the date is set or null if there is no date.
  365. */
  366. public function get_date_created( $context = 'view' ) {
  367. return $this->get_prop( 'date_created', $context );
  368. }
  369. /**
  370. * Return the date this customer was last updated.
  371. *
  372. * @since 3.0.0
  373. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  374. * @return WC_DateTime|null object if the date is set or null if there is no date.
  375. */
  376. public function get_date_modified( $context = 'view' ) {
  377. return $this->get_prop( 'date_modified', $context );
  378. }
  379. /**
  380. * Gets a prop for a getter method.
  381. *
  382. * @since 3.0.0
  383. * @param string $prop Name of prop to get.
  384. * @param string $address billing or shipping.
  385. * @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.
  386. * @return mixed
  387. */
  388. protected function get_address_prop( $prop, $address = 'billing', $context = 'view' ) {
  389. $value = null;
  390. if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
  391. $value = isset( $this->changes[ $address ][ $prop ] ) ? $this->changes[ $address ][ $prop ] : $this->data[ $address ][ $prop ];
  392. if ( 'view' === $context ) {
  393. $value = apply_filters( $this->get_hook_prefix() . $address . '_' . $prop, $value, $this );
  394. }
  395. }
  396. return $value;
  397. }
  398. /**
  399. * Get billing.
  400. *
  401. * @since 3.2.0
  402. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  403. * @return array
  404. */
  405. public function get_billing( $context = 'view' ) {
  406. return $this->get_prop( 'billing', $context );
  407. }
  408. /**
  409. * Get billing_first_name.
  410. *
  411. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  412. * @return string
  413. */
  414. public function get_billing_first_name( $context = 'view' ) {
  415. return $this->get_address_prop( 'first_name', 'billing', $context );
  416. }
  417. /**
  418. * Get billing_last_name.
  419. *
  420. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  421. * @return string
  422. */
  423. public function get_billing_last_name( $context = 'view' ) {
  424. return $this->get_address_prop( 'last_name', 'billing', $context );
  425. }
  426. /**
  427. * Get billing_company.
  428. *
  429. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  430. * @return string
  431. */
  432. public function get_billing_company( $context = 'view' ) {
  433. return $this->get_address_prop( 'company', 'billing', $context );
  434. }
  435. /**
  436. * Get billing_address_1.
  437. *
  438. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  439. * @return string
  440. */
  441. public function get_billing_address( $context = 'view' ) {
  442. return $this->get_billing_address_1( $context );
  443. }
  444. /**
  445. * Get billing_address_1.
  446. *
  447. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  448. * @return string
  449. */
  450. public function get_billing_address_1( $context = 'view' ) {
  451. return $this->get_address_prop( 'address_1', 'billing', $context );
  452. }
  453. /**
  454. * Get billing_address_2.
  455. *
  456. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  457. * @return string $value
  458. */
  459. public function get_billing_address_2( $context = 'view' ) {
  460. return $this->get_address_prop( 'address_2', 'billing', $context );
  461. }
  462. /**
  463. * Get billing_city.
  464. *
  465. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  466. * @return string $value
  467. */
  468. public function get_billing_city( $context = 'view' ) {
  469. return $this->get_address_prop( 'city', 'billing', $context );
  470. }
  471. /**
  472. * Get billing_state.
  473. *
  474. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  475. * @return string
  476. */
  477. public function get_billing_state( $context = 'view' ) {
  478. return $this->get_address_prop( 'state', 'billing', $context );
  479. }
  480. /**
  481. * Get billing_postcode.
  482. *
  483. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  484. * @return string
  485. */
  486. public function get_billing_postcode( $context = 'view' ) {
  487. return $this->get_address_prop( 'postcode', 'billing', $context );
  488. }
  489. /**
  490. * Get billing_country.
  491. *
  492. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  493. * @return string
  494. */
  495. public function get_billing_country( $context = 'view' ) {
  496. return $this->get_address_prop( 'country', 'billing', $context );
  497. }
  498. /**
  499. * Get billing_email.
  500. *
  501. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  502. * @return string
  503. */
  504. public function get_billing_email( $context = 'view' ) {
  505. return $this->get_address_prop( 'email', 'billing', $context );
  506. }
  507. /**
  508. * Get billing_phone.
  509. *
  510. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  511. * @return string
  512. */
  513. public function get_billing_phone( $context = 'view' ) {
  514. return $this->get_address_prop( 'phone', 'billing', $context );
  515. }
  516. /**
  517. * Get shipping.
  518. *
  519. * @since 3.2.0
  520. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  521. * @return array
  522. */
  523. public function get_shipping( $context = 'view' ) {
  524. return $this->get_prop( 'shipping', $context );
  525. }
  526. /**
  527. * Get shipping_first_name.
  528. *
  529. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  530. * @return string
  531. */
  532. public function get_shipping_first_name( $context = 'view' ) {
  533. return $this->get_address_prop( 'first_name', 'shipping', $context );
  534. }
  535. /**
  536. * Get shipping_last_name.
  537. *
  538. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  539. * @return string
  540. */
  541. public function get_shipping_last_name( $context = 'view' ) {
  542. return $this->get_address_prop( 'last_name', 'shipping', $context );
  543. }
  544. /**
  545. * Get shipping_company.
  546. *
  547. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  548. * @return string
  549. */
  550. public function get_shipping_company( $context = 'view' ) {
  551. return $this->get_address_prop( 'company', 'shipping', $context );
  552. }
  553. /**
  554. * Get shipping_address_1.
  555. *
  556. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  557. * @return string
  558. */
  559. public function get_shipping_address( $context = 'view' ) {
  560. return $this->get_shipping_address_1( $context );
  561. }
  562. /**
  563. * Get shipping_address_1.
  564. *
  565. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  566. * @return string
  567. */
  568. public function get_shipping_address_1( $context = 'view' ) {
  569. return $this->get_address_prop( 'address_1', 'shipping', $context );
  570. }
  571. /**
  572. * Get shipping_address_2.
  573. *
  574. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  575. * @return string
  576. */
  577. public function get_shipping_address_2( $context = 'view' ) {
  578. return $this->get_address_prop( 'address_2', 'shipping', $context );
  579. }
  580. /**
  581. * Get shipping_city.
  582. *
  583. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  584. * @return string
  585. */
  586. public function get_shipping_city( $context = 'view' ) {
  587. return $this->get_address_prop( 'city', 'shipping', $context );
  588. }
  589. /**
  590. * Get shipping_state.
  591. *
  592. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  593. * @return string
  594. */
  595. public function get_shipping_state( $context = 'view' ) {
  596. return $this->get_address_prop( 'state', 'shipping', $context );
  597. }
  598. /**
  599. * Get shipping_postcode.
  600. *
  601. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  602. * @return string
  603. */
  604. public function get_shipping_postcode( $context = 'view' ) {
  605. return $this->get_address_prop( 'postcode', 'shipping', $context );
  606. }
  607. /**
  608. * Get shipping_country.
  609. *
  610. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  611. * @return string
  612. */
  613. public function get_shipping_country( $context = 'view' ) {
  614. return $this->get_address_prop( 'country', 'shipping', $context );
  615. }
  616. /**
  617. * Is the user a paying customer?
  618. *
  619. * @since 3.0.0
  620. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  621. * @return bool
  622. */
  623. public function get_is_paying_customer( $context = 'view' ) {
  624. return $this->get_prop( 'is_paying_customer', $context );
  625. }
  626. /*
  627. |--------------------------------------------------------------------------
  628. | Setters
  629. |--------------------------------------------------------------------------
  630. */
  631. /**
  632. * Set customer's username.
  633. *
  634. * @since 3.0.0
  635. * @param string $username Username.
  636. */
  637. public function set_username( $username ) {
  638. $this->set_prop( 'username', $username );
  639. }
  640. /**
  641. * Set customer's email.
  642. *
  643. * @since 3.0.0
  644. * @param string $value Email.
  645. */
  646. public function set_email( $value ) {
  647. if ( $value && ! is_email( $value ) ) {
  648. $this->error( 'customer_invalid_email', __( 'Invalid email address', 'woocommerce' ) );
  649. }
  650. $this->set_prop( 'email', sanitize_email( $value ) );
  651. }
  652. /**
  653. * Set customer's first name.
  654. *
  655. * @since 3.0.0
  656. * @param string $first_name First name.
  657. */
  658. public function set_first_name( $first_name ) {
  659. $this->set_prop( 'first_name', $first_name );
  660. }
  661. /**
  662. * Set customer's last name.
  663. *
  664. * @since 3.0.0
  665. * @param string $last_name Last name.
  666. */
  667. public function set_last_name( $last_name ) {
  668. $this->set_prop( 'last_name', $last_name );
  669. }
  670. /**
  671. * Set customer's display name.
  672. *
  673. * @since 3.1.0
  674. * @param string $display_name Display name.
  675. */
  676. public function set_display_name( $display_name ) {
  677. /* translators: 1: first name 2: last name */
  678. $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 );
  679. }
  680. /**
  681. * Set customer's user role(s).
  682. *
  683. * @since 3.0.0
  684. * @param mixed $role User role.
  685. */
  686. public function set_role( $role ) {
  687. global $wp_roles;
  688. if ( $role && ! empty( $wp_roles->roles ) && ! in_array( $role, array_keys( $wp_roles->roles ), true ) ) {
  689. $this->error( 'customer_invalid_role', __( 'Invalid role', 'woocommerce' ) );
  690. }
  691. $this->set_prop( 'role', $role );
  692. }
  693. /**
  694. * Set the date this customer was last updated.
  695. *
  696. * @since 3.0.0
  697. * @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.
  698. */
  699. public function set_date_created( $date = null ) {
  700. $this->set_date_prop( 'date_created', $date );
  701. }
  702. /**
  703. * Set the date this customer was last updated.
  704. *
  705. * @since 3.0.0
  706. * @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.
  707. */
  708. public function set_date_modified( $date = null ) {
  709. $this->set_date_prop( 'date_modified', $date );
  710. }
  711. /**
  712. * Set customer address to match shop base address.
  713. *
  714. * @since 3.0.0
  715. */
  716. public function set_billing_address_to_base() {
  717. $base = wc_get_customer_default_location();
  718. $this->set_billing_location( $base['country'], $base['state'], '', '' );
  719. }
  720. /**
  721. * Set customer shipping address to base address.
  722. *
  723. * @since 3.0.0
  724. */
  725. public function set_shipping_address_to_base() {
  726. $base = wc_get_customer_default_location();
  727. $this->set_shipping_location( $base['country'], $base['state'], '', '' );
  728. }
  729. /**
  730. * Sets all address info at once.
  731. *
  732. * @param string $country Country.
  733. * @param string $state State.
  734. * @param string $postcode Postcode.
  735. * @param string $city City.
  736. */
  737. public function set_billing_location( $country, $state = '', $postcode = '', $city = '' ) {
  738. $billing = $this->get_prop( 'billing', 'edit' );
  739. $billing['country'] = $country;
  740. $billing['state'] = $state;
  741. $billing['postcode'] = $postcode;
  742. $billing['city'] = $city;
  743. $this->set_prop( 'billing', $billing );
  744. }
  745. /**
  746. * Sets all shipping info at once.
  747. *
  748. * @param string $country Country.
  749. * @param string $state State.
  750. * @param string $postcode Postcode.
  751. * @param string $city City.
  752. */
  753. public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
  754. $shipping = $this->get_prop( 'shipping', 'edit' );
  755. $shipping['country'] = $country;
  756. $shipping['state'] = $state;
  757. $shipping['postcode'] = $postcode;
  758. $shipping['city'] = $city;
  759. $this->set_prop( 'shipping', $shipping );
  760. }
  761. /**
  762. * Sets a prop for a setter method.
  763. *
  764. * @since 3.0.0
  765. * @param string $prop Name of prop to set.
  766. * @param string $address Name of address to set. billing or shipping.
  767. * @param mixed $value Value of the prop.
  768. */
  769. protected function set_address_prop( $prop, $address = 'billing', $value ) {
  770. if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
  771. if ( true === $this->object_read ) {
  772. if ( $value !== $this->data[ $address ][ $prop ] || ( isset( $this->changes[ $address ] ) && array_key_exists( $prop, $this->changes[ $address ] ) ) ) {
  773. $this->changes[ $address ][ $prop ] = $value;
  774. }
  775. } else {
  776. $this->data[ $address ][ $prop ] = $value;
  777. }
  778. }
  779. }
  780. /**
  781. * Set billing_first_name.
  782. *
  783. * @param string $value Billing first name.
  784. */
  785. public function set_billing_first_name( $value ) {
  786. $this->set_address_prop( 'first_name', 'billing', $value );
  787. }
  788. /**
  789. * Set billing_last_name.
  790. *
  791. * @param string $value Billing last name.
  792. */
  793. public function set_billing_last_name( $value ) {
  794. $this->set_address_prop( 'last_name', 'billing', $value );
  795. }
  796. /**
  797. * Set billing_company.
  798. *
  799. * @param string $value Billing company.
  800. */
  801. public function set_billing_company( $value ) {
  802. $this->set_address_prop( 'company', 'billing', $value );
  803. }
  804. /**
  805. * Set billing_address_1.
  806. *
  807. * @param string $value Billing address line 1.
  808. */
  809. public function set_billing_address( $value ) {
  810. $this->set_billing_address_1( $value );
  811. }
  812. /**
  813. * Set billing_address_1.
  814. *
  815. * @param string $value Billing address line 1.
  816. */
  817. public function set_billing_address_1( $value ) {
  818. $this->set_address_prop( 'address_1', 'billing', $value );
  819. }
  820. /**
  821. * Set billing_address_2.
  822. *
  823. * @param string $value Billing address line 2.
  824. */
  825. public function set_billing_address_2( $value ) {
  826. $this->set_address_prop( 'address_2', 'billing', $value );
  827. }
  828. /**
  829. * Set billing_city.
  830. *
  831. * @param string $value Billing city.
  832. */
  833. public function set_billing_city( $value ) {
  834. $this->set_address_prop( 'city', 'billing', $value );
  835. }
  836. /**
  837. * Set billing_state.
  838. *
  839. * @param string $value Billing state.
  840. */
  841. public function set_billing_state( $value ) {
  842. $this->set_address_prop( 'state', 'billing', $value );
  843. }
  844. /**
  845. * Set billing_postcode.
  846. *
  847. * @param string $value Billing postcode.
  848. */
  849. public function set_billing_postcode( $value ) {
  850. $this->set_address_prop( 'postcode', 'billing', $value );
  851. }
  852. /**
  853. * Set billing_country.
  854. *
  855. * @param string $value Billing country.
  856. */
  857. public function set_billing_country( $value ) {
  858. $this->set_address_prop( 'country', 'billing', $value );
  859. }
  860. /**
  861. * Set billing_email.
  862. *
  863. * @param string $value Billing email.
  864. */
  865. public function set_billing_email( $value ) {
  866. if ( $value && ! is_email( $value ) ) {
  867. $this->error( 'customer_invalid_billing_email', __( 'Invalid billing email address', 'woocommerce' ) );
  868. }
  869. $this->set_address_prop( 'email', 'billing', sanitize_email( $value ) );
  870. }
  871. /**
  872. * Set billing_phone.
  873. *
  874. * @param string $value Billing phone.
  875. */
  876. public function set_billing_phone( $value ) {
  877. $this->set_address_prop( 'phone', 'billing', $value );
  878. }
  879. /**
  880. * Set shipping_first_name.
  881. *
  882. * @param string $value Shipping first name.
  883. */
  884. public function set_shipping_first_name( $value ) {
  885. $this->set_address_prop( 'first_name', 'shipping', $value );
  886. }
  887. /**
  888. * Set shipping_last_name.
  889. *
  890. * @param string $value Shipping last name.
  891. */
  892. public function set_shipping_last_name( $value ) {
  893. $this->set_address_prop( 'last_name', 'shipping', $value );
  894. }
  895. /**
  896. * Set shipping_company.
  897. *
  898. * @param string $value Shipping company.
  899. */
  900. public function set_shipping_company( $value ) {
  901. $this->set_address_prop( 'company', 'shipping', $value );
  902. }
  903. /**
  904. * Set shipping_address_1.
  905. *
  906. * @param string $value Shipping address line 1.
  907. */
  908. public function set_shipping_address( $value ) {
  909. $this->set_shipping_address_1( $value );
  910. }
  911. /**
  912. * Set shipping_address_1.
  913. *
  914. * @param string $value Shipping address line 1.
  915. */
  916. public function set_shipping_address_1( $value ) {
  917. $this->set_address_prop( 'address_1', 'shipping', $value );
  918. }
  919. /**
  920. * Set shipping_address_2.
  921. *
  922. * @param string $value Shipping address line 2.
  923. */
  924. public function set_shipping_address_2( $value ) {
  925. $this->set_address_prop( 'address_2', 'shipping', $value );
  926. }
  927. /**
  928. * Set shipping_city.
  929. *
  930. * @param string $value Shipping city.
  931. */
  932. public function set_shipping_city( $value ) {
  933. $this->set_address_prop( 'city', 'shipping', $value );
  934. }
  935. /**
  936. * Set shipping_state.
  937. *
  938. * @param string $value Shipping state.
  939. */
  940. public function set_shipping_state( $value ) {
  941. $this->set_address_prop( 'state', 'shipping', $value );
  942. }
  943. /**
  944. * Set shipping_postcode.
  945. *
  946. * @param string $value Shipping postcode.
  947. */
  948. public function set_shipping_postcode( $value ) {
  949. $this->set_address_prop( 'postcode', 'shipping', $value );
  950. }
  951. /**
  952. * Set shipping_country.
  953. *
  954. * @param string $value Shipping country.
  955. */
  956. public function set_shipping_country( $value ) {
  957. $this->set_address_prop( 'country', 'shipping', $value );
  958. }
  959. /**
  960. * Set if the user a paying customer.
  961. *
  962. * @since 3.0.0
  963. * @param bool $is_paying_customer If is a paying customer.
  964. */
  965. public function set_is_paying_customer( $is_paying_customer ) {
  966. $this->set_prop( 'is_paying_customer', (bool) $is_paying_customer );
  967. }
  968. }