WC_Checkout

Main checkout class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-checkout.php  
  1. class WC_Checkout { 
  2.  
  3. /** 
  4. * The single instance of the class. 
  5. * @var WC_Checkout|null 
  6. */ 
  7. protected static $instance = null; 
  8.  
  9. /** 
  10. * Checkout fields are stored here. 
  11. * @var array|null 
  12. */ 
  13. protected $fields = null; 
  14.  
  15. /** 
  16. * Holds posted data for backwards compatibility. 
  17. * @var array 
  18. */ 
  19. protected $legacy_posted_data = array(); 
  20.  
  21. /** 
  22. * Gets the main WC_Checkout Instance. 
  23. * @since 2.1 
  24. * @static 
  25. * @return WC_Checkout Main instance 
  26. */ 
  27. public static function instance() { 
  28. if ( is_null( self::$instance ) ) { 
  29. self::$instance = new self(); 
  30.  
  31. // Hook in actions once. 
  32. add_action( 'woocommerce_checkout_billing', array( self::$instance, 'checkout_form_billing' ) ); 
  33. add_action( 'woocommerce_checkout_shipping', array( self::$instance, 'checkout_form_shipping' ) ); 
  34.  
  35. // woocommerce_checkout_init action is ran once when the class is first constructed. 
  36. do_action( 'woocommerce_checkout_init', self::$instance ); 
  37. return self::$instance; 
  38.  
  39. /** 
  40. * See if variable is set. Used to support legacy public variables which are no longer defined. 
  41. * @param string $key 
  42. * @return bool 
  43. */ 
  44. public function __isset( $key ) { 
  45. return in_array( $key, array( 
  46. 'enable_signup',  
  47. 'enable_guest_checkout',  
  48. 'must_create_account',  
  49. 'checkout_fields',  
  50. 'posted',  
  51. 'shipping_method',  
  52. 'payment_method',  
  53. 'customer_id',  
  54. 'shipping_methods',  
  55. ) ); 
  56.  
  57. /** 
  58. * Sets the legacy public variables for backwards compatibility. 
  59. * @param string $key 
  60. * @param mixed $value 
  61. */ 
  62. public function __set( $key, $value ) { 
  63. switch ( $key ) { 
  64. case 'enable_signup' : 
  65. $bool_value = wc_string_to_bool( $value ); 
  66.  
  67. if ( $bool_value !== $this->is_registration_enabled() ) { 
  68. remove_filter( 'woocommerce_checkout_registration_enabled', '__return_true', 0 ); 
  69. remove_filter( 'woocommerce_checkout_registration_enabled', '__return_false', 0 ); 
  70. add_filter( 'woocommerce_checkout_registration_enabled', $bool_value ? '__return_true' : '__return_false', 0 ); 
  71. break; 
  72. case 'enable_guest_checkout' : 
  73. $bool_value = wc_string_to_bool( $value ); 
  74.  
  75. if ( $bool_value === $this->is_registration_required() ) { 
  76. remove_filter( 'woocommerce_checkout_registration_required', '__return_true', 0 ); 
  77. remove_filter( 'woocommerce_checkout_registration_required', '__return_false', 0 ); 
  78. add_filter( 'woocommerce_checkout_registration_required', $bool_value ? '__return_false' : '__return_true', 0 ); 
  79. break; 
  80. case 'checkout_fields' : 
  81. $this->fields = $value; 
  82. break; 
  83. case 'shipping_methods' : 
  84. WC()->session->set( 'chosen_shipping_methods', $value ); 
  85. break; 
  86. case 'posted' : 
  87. $this->legacy_posted_data = $value; 
  88. break; 
  89.  
  90. /** 
  91. * Gets the legacy public variables for backwards compatibility. 
  92. * @param string $key 
  93. * @return string 
  94. */ 
  95. public function __get( $key ) { 
  96. if ( in_array( $key, array( 'posted', 'shipping_method', 'payment_method' ) ) && empty( $this->legacy_posted_data ) ) { 
  97. $this->legacy_posted_data = $this->get_posted_data(); 
  98. switch ( $key ) { 
  99. case 'enable_signup' : 
  100. return $this->is_registration_enabled(); 
  101. case 'enable_guest_checkout' : 
  102. return ! $this->is_registration_required(); 
  103. case 'must_create_account' : 
  104. return $this->is_registration_required() && ! is_user_logged_in(); 
  105. case 'checkout_fields' : 
  106. return $this->get_checkout_fields(); 
  107. case 'posted' : 
  108. wc_doing_it_wrong( 'WC_Checkout->posted', 'Use $_POST directly.', '3.0.0' ); 
  109. return $this->legacy_posted_data; 
  110. case 'shipping_method' : 
  111. return $this->legacy_posted_data['shipping_method']; 
  112. case 'payment_method' : 
  113. return $this->legacy_posted_data['payment_method']; 
  114. case 'customer_id' : 
  115. return apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() ); 
  116. case 'shipping_methods' : 
  117. return (array) WC()->session->get( 'chosen_shipping_methods' ); 
  118.  
  119. /** 
  120. * Cloning is forbidden. 
  121. */ 
  122. public function __clone() { 
  123. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  124.  
  125. /** 
  126. * Unserializing instances of this class is forbidden. 
  127. */ 
  128. public function __wakeup() { 
  129. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  130.  
  131. /** 
  132. * Is registration required to checkout? 
  133. * @since 3.0.0 
  134. * @return boolean 
  135. */ 
  136. public function is_registration_required() { 
  137. return apply_filters( 'woocommerce_checkout_registration_required', 'yes' !== get_option( 'woocommerce_enable_guest_checkout' ) ); 
  138.  
  139. /** 
  140. * Is registration enabled on the checkout page? 
  141. * @since 3.0.0 
  142. * @return boolean 
  143. */ 
  144. public function is_registration_enabled() { 
  145. return apply_filters( 'woocommerce_checkout_registration_enabled', 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout' ) ); 
  146.  
  147. /** 
  148. * Get an array of checkout fields. 
  149. * @param string $fieldset to get. 
  150. * @return array 
  151. */ 
  152. public function get_checkout_fields( $fieldset = '' ) { 
  153. if ( is_null( $this->fields ) ) { 
  154. $this->fields = array( 
  155. 'billing' => WC()->countries->get_address_fields( $this->get_value( 'billing_country' ), 'billing_' ),  
  156. 'shipping' => WC()->countries->get_address_fields( $this->get_value( 'shipping_country' ), 'shipping_' ),  
  157. 'account' => array(),  
  158. 'order' => array( 
  159. 'order_comments' => array( 
  160. 'type' => 'textarea',  
  161. 'class' => array( 'notes' ),  
  162. 'label' => __( 'Order notes', 'woocommerce' ),  
  163. 'placeholder' => esc_attr__( 'Notes about your order, e.g. special notes for delivery.', 'woocommerce' ),  
  164. ),  
  165. ),  
  166. ); 
  167. if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) { 
  168. $this->fields['account']['account_username'] = array( 
  169. 'type' => 'text',  
  170. 'label' => __( 'Account username', 'woocommerce' ),  
  171. 'required' => true,  
  172. 'placeholder' => esc_attr__( 'Username', 'woocommerce' ),  
  173. ); 
  174.  
  175. if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) { 
  176. $this->fields['account']['account_password'] = array( 
  177. 'type' => 'password',  
  178. 'label' => __( 'Account password', 'woocommerce' ),  
  179. 'required' => true,  
  180. 'placeholder' => esc_attr__( 'Password', 'woocommerce' ),  
  181. ); 
  182.  
  183. $this->fields = apply_filters( 'woocommerce_checkout_fields', $this->fields ); 
  184. if ( $fieldset ) { 
  185. return $this->fields[ $fieldset ]; 
  186. } else { 
  187. return $this->fields; 
  188.  
  189. /** 
  190. * When we process the checkout, lets ensure cart items are rechecked to prevent checkout. 
  191. */ 
  192. public function check_cart_items() { 
  193. do_action( 'woocommerce_check_cart_items' ); 
  194.  
  195. /** 
  196. * Output the billing form. 
  197. */ 
  198. public function checkout_form_billing() { 
  199. wc_get_template( 'checkout/form-billing.php', array( 'checkout' => $this ) ); 
  200.  
  201. /** 
  202. * Output the shipping form. 
  203. */ 
  204. public function checkout_form_shipping() { 
  205. wc_get_template( 'checkout/form-shipping.php', array( 'checkout' => $this ) ); 
  206.  
  207. /** 
  208. * Create an order. Error codes: 
  209. * 520 - Cannot insert order into the database. 
  210. * 521 - Cannot get order after creation. 
  211. * 522 - Cannot update order. 
  212. * 525 - Cannot create line item. 
  213. * 526 - Cannot create fee item. 
  214. * 527 - Cannot create shipping item. 
  215. * 528 - Cannot create tax item. 
  216. * 529 - Cannot create coupon item. 
  217. * @throws Exception 
  218. * @param $data Posted data. 
  219. * @return int|WP_ERROR 
  220. */ 
  221. public function create_order( $data ) { 
  222. // Give plugins the opportunity to create an order themselves. 
  223. if ( $order_id = apply_filters( 'woocommerce_create_order', null, $this ) ) { 
  224. return $order_id; 
  225.  
  226. try { 
  227. $order_id = absint( WC()->session->get( 'order_awaiting_payment' ) ); 
  228. $cart_hash = md5( json_encode( wc_clean( WC()->cart->get_cart_for_session() ) ) . WC()->cart->total ); 
  229. $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  230.  
  231. /** 
  232. * If there is an order pending payment, we can resume it here so 
  233. * long as it has not changed. If the order has changed, i.e. 
  234. * different items or cost, create a new order. We use a hash to 
  235. * detect changes which is based on cart items + order total. 
  236. */ 
  237. if ( $order_id && ( $order = wc_get_order( $order_id ) ) && $order->has_cart_hash( $cart_hash ) && $order->has_status( array( 'pending', 'failed' ) ) ) { 
  238. // Action for 3rd parties. 
  239. do_action( 'woocommerce_resume_order', $order_id ); 
  240.  
  241. // Remove all items - we will re-add them later. 
  242. $order->remove_order_items(); 
  243. } else { 
  244. $order = new WC_Order(); 
  245.  
  246. foreach ( $data as $key => $value ) { 
  247. if ( is_callable( array( $order, "set_{$key}" ) ) ) { 
  248. $order->{"set_{$key}"}( $value ); 
  249.  
  250. // Store custom fields prefixed with wither shipping_ or billing_. This is for backwards compatibility with 2.6.x. 
  251. } elseif ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) ) { 
  252. $order->update_meta_data( '_' . $key, $value ); 
  253.  
  254. $order->set_created_via( 'checkout' ); 
  255. $order->set_cart_hash( $cart_hash ); 
  256. $order->set_customer_id( apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() ) ); 
  257. $order->set_currency( get_woocommerce_currency() ); 
  258. $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); 
  259. $order->set_customer_ip_address( WC_Geolocation::get_ip_address() ); 
  260. $order->set_customer_user_agent( wc_get_user_agent() ); 
  261. $order->set_customer_note( isset( $data['order_comments'] ) ? $data['order_comments'] : '' ); 
  262. $order->set_payment_method( isset( $available_gateways[ $data['payment_method'] ] ) ? $available_gateways[ $data['payment_method'] ] : $data['payment_method'] ); 
  263. $order->set_shipping_total( WC()->cart->shipping_total ); 
  264. $order->set_discount_total( WC()->cart->get_cart_discount_total() ); 
  265. $order->set_discount_tax( WC()->cart->get_cart_discount_tax_total() ); 
  266. $order->set_cart_tax( WC()->cart->tax_total ); 
  267. $order->set_shipping_tax( WC()->cart->shipping_tax_total ); 
  268. $order->set_total( WC()->cart->total ); 
  269. $this->create_order_line_items( $order, WC()->cart ); 
  270. $this->create_order_fee_lines( $order, WC()->cart ); 
  271. $this->create_order_shipping_lines( $order, WC()->session->get( 'chosen_shipping_methods' ), WC()->shipping->get_packages() ); 
  272. $this->create_order_tax_lines( $order, WC()->cart ); 
  273. $this->create_order_coupon_lines( $order, WC()->cart ); 
  274.  
  275. /** 
  276. * Action hook to adjust order before save. 
  277. * @since 3.0.0 
  278. */ 
  279. do_action( 'woocommerce_checkout_create_order', $order, $data ); 
  280.  
  281. // Save the order. 
  282. $order_id = $order->save(); 
  283.  
  284. do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data ); 
  285.  
  286. return $order_id; 
  287. } catch ( Exception $e ) { 
  288. return new WP_Error( 'checkout-error', $e->getMessage() ); 
  289.  
  290. /** 
  291. * Add line items to the order. 
  292. * @param WC_Order $order 
  293. */ 
  294. public function create_order_line_items( &$order, $cart ) { 
  295. foreach ( $cart->get_cart() as $cart_item_key => $values ) { 
  296. $product = $values['data']; 
  297. $item = new WC_Order_Item_Product(); 
  298. $item->legacy_values = $values; // @deprecated For legacy actions. 
  299. $item->legacy_cart_item_key = $cart_item_key; // @deprecated For legacy actions. 
  300. $item->set_props( array( 
  301. 'quantity' => $values['quantity'],  
  302. 'variation' => $values['variation'],  
  303. 'subtotal' => $values['line_subtotal'],  
  304. 'total' => $values['line_total'],  
  305. 'subtotal_tax' => $values['line_subtotal_tax'],  
  306. 'total_tax' => $values['line_tax'],  
  307. 'taxes' => $values['line_tax_data'],  
  308. ) ); 
  309. if ( $product ) { 
  310. $item->set_props( array( 
  311. 'name' => $product->get_name(),  
  312. 'tax_class' => $product->get_tax_class(),  
  313. 'product_id' => $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id(),  
  314. 'variation_id' => $product->is_type( 'variation' ) ? $product->get_id() : 0,  
  315. ) ); 
  316. $item->set_backorder_meta(); 
  317.  
  318. /** 
  319. * Action hook to adjust item before save. 
  320. * @since 3.0.0 
  321. */ 
  322. do_action( 'woocommerce_checkout_create_order_line_item', $item, $cart_item_key, $values, $order ); 
  323.  
  324. // Add item to order and save. 
  325. $order->add_item( $item ); 
  326.  
  327. /** 
  328. * Add fees to the order. 
  329. * @param WC_Order $order 
  330. */ 
  331. public function create_order_fee_lines( &$order, $cart ) { 
  332. foreach ( $cart->get_fees() as $fee_key => $fee ) { 
  333. $item = new WC_Order_Item_Fee(); 
  334. $item->legacy_fee = $fee; // @deprecated For legacy actions. 
  335. $item->legacy_fee_key = $fee_key; // @deprecated For legacy actions. 
  336. $item->set_props( array( 
  337. 'name' => $fee->name,  
  338. 'tax_class' => $fee->taxable ? $fee->tax_class : 0,  
  339. 'total' => $fee->amount,  
  340. 'total_tax' => $fee->tax,  
  341. 'taxes' => array( 
  342. 'total' => $fee->tax_data,  
  343. ),  
  344. ) ); 
  345.  
  346. /** 
  347. * Action hook to adjust item before save. 
  348. * @since 3.0.0 
  349. */ 
  350. do_action( 'woocommerce_checkout_create_order_fee_item', $item, $fee_key, $fee, $order ); 
  351.  
  352. // Add item to order and save. 
  353. $order->add_item( $item ); 
  354.  
  355. /** 
  356. * Add shipping lines to the order. 
  357. * @param WC_Order $order 
  358. */ 
  359. public function create_order_shipping_lines( &$order, $chosen_shipping_methods, $packages ) { 
  360. foreach ( $packages as $package_key => $package ) { 
  361. if ( isset( $chosen_shipping_methods[ $package_key ], $package['rates'][ $chosen_shipping_methods[ $package_key ] ] ) ) { 
  362. /** @var WC_Shipping_Rate $shipping_rate */ 
  363. $shipping_rate = $package['rates'][ $chosen_shipping_methods[ $package_key ] ]; 
  364. $item = new WC_Order_Item_Shipping(); 
  365. $item->legacy_package_key = $package_key; // @deprecated For legacy actions. 
  366. $item->set_props( array( 
  367. 'method_title' => $shipping_rate->label,  
  368. 'method_id' => $shipping_rate->id,  
  369. 'total' => wc_format_decimal( $shipping_rate->cost ),  
  370. 'taxes' => array( 
  371. 'total' => $shipping_rate->taxes,  
  372. ),  
  373. ) ); 
  374.  
  375. foreach ( $shipping_rate->get_meta_data() as $key => $value ) { 
  376. $item->add_meta_data( $key, $value, true ); 
  377.  
  378. /** 
  379. * Action hook to adjust item before save. 
  380. * @since 3.0.0 
  381. */ 
  382. do_action( 'woocommerce_checkout_create_order_shipping_item', $item, $package_key, $package, $order ); 
  383.  
  384. // Add item to order and save. 
  385. $order->add_item( $item ); 
  386.  
  387. /** 
  388. * Add tax lines to the order. 
  389. * @param WC_Order $order 
  390. */ 
  391. public function create_order_tax_lines( &$order, $cart ) { 
  392. foreach ( array_keys( $cart->taxes + $cart->shipping_taxes ) as $tax_rate_id ) { 
  393. if ( $tax_rate_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) { 
  394. $item = new WC_Order_Item_Tax(); 
  395. $item->set_props( array( 
  396. 'rate_id' => $tax_rate_id,  
  397. 'tax_total' => $cart->get_tax_amount( $tax_rate_id ),  
  398. 'shipping_tax_total' => $cart->get_shipping_tax_amount( $tax_rate_id ),  
  399. 'rate_code' => WC_Tax::get_rate_code( $tax_rate_id ),  
  400. 'label' => WC_Tax::get_rate_label( $tax_rate_id ),  
  401. 'compound' => WC_Tax::is_compound( $tax_rate_id ),  
  402. ) ); 
  403.  
  404. /** 
  405. * Action hook to adjust item before save. 
  406. * @since 3.0.0 
  407. */ 
  408. do_action( 'woocommerce_checkout_create_order_tax_item', $item, $tax_rate_id, $order ); 
  409.  
  410. // Add item to order and save. 
  411. $order->add_item( $item ); 
  412.  
  413. /** 
  414. * Add coupon lines to the order. 
  415. * @param WC_Order $order 
  416. */ 
  417. public function create_order_coupon_lines( &$order, $cart ) { 
  418. foreach ( $cart->get_coupons() as $code => $coupon ) { 
  419. $item = new WC_Order_Item_Coupon(); 
  420. $item->set_props( array( 
  421. 'code' => $code,  
  422. 'discount' => $cart->get_coupon_discount_amount( $code ),  
  423. 'discount_tax' => $cart->get_coupon_discount_tax_amount( $code ),  
  424. ) ); 
  425.  
  426. /** 
  427. * Action hook to adjust item before save. 
  428. * @since 3.0.0 
  429. */ 
  430. do_action( 'woocommerce_checkout_create_order_coupon_item', $item, $code, $coupon, $order ); 
  431.  
  432. // Add item to order and save. 
  433. $order->add_item( $item ); 
  434.  
  435. /** 
  436. * See if a fieldset should be skipped. 
  437. * @since 3.0.0 
  438. * @param string $fieldset_key 
  439. * @param array $data 
  440. */ 
  441. protected function maybe_skip_fieldset( $fieldset_key, $data ) { 
  442. if ( 'shipping' === $fieldset_key && ( ! $data['ship_to_different_address'] || ! WC()->cart->needs_shipping_address() ) ) { 
  443. return true; 
  444. if ( 'account' === $fieldset_key && ( is_user_logged_in() || ( ! $this->is_registration_required() && empty( $data['createaccount'] ) ) ) ) { 
  445. return true; 
  446. return false; 
  447.  
  448. /** 
  449. * Get posted data from the checkout form. 
  450. * @since 3.0.0 
  451. * @return array of data. 
  452. */ 
  453. protected function get_posted_data() { 
  454. $skipped = array(); 
  455. $data = array( 
  456. 'terms' => (int) isset( $_POST['terms'] ),  
  457. 'createaccount' => (int) ! empty( $_POST['createaccount'] ),  
  458. 'payment_method' => isset( $_POST['payment_method'] ) ? wc_clean( $_POST['payment_method'] ) : '',  
  459. 'shipping_method' => isset( $_POST['shipping_method'] ) ? wc_clean( $_POST['shipping_method'] ) : '',  
  460. 'ship_to_different_address' => ! empty( $_POST['ship_to_different_address'] ) && ! wc_ship_to_billing_address_only(),  
  461. 'woocommerce_checkout_update_totals' => isset( $_POST['woocommerce_checkout_update_totals'] ),  
  462. ); 
  463. foreach ( $this->get_checkout_fields() as $fieldset_key => $fieldset ) { 
  464. if ( $this->maybe_skip_fieldset( $fieldset_key, $data ) ) { 
  465. $skipped[] = $fieldset_key; 
  466. continue; 
  467. foreach ( $fieldset as $key => $field ) { 
  468. $type = sanitize_title( isset( $field['type'] ) ? $field['type'] : 'text' ); 
  469.  
  470. switch ( $type ) { 
  471. case 'checkbox' : 
  472. $value = (int) isset( $_POST[ $key ] ); 
  473. break; 
  474. case 'multiselect' : 
  475. $value = isset( $_POST[ $key ] ) ? implode( ', ', wc_clean( $_POST[ $key ] ) ) : ''; 
  476. break; 
  477. case 'textarea' : 
  478. $value = isset( $_POST[ $key ] ) ? wc_sanitize_textarea( $_POST[ $key ] ) : ''; 
  479. break; 
  480. default : 
  481. $value = isset( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : ''; 
  482. break; 
  483.  
  484. $data[ $key ] = apply_filters( 'woocommerce_process_checkout_' . $type . '_field', apply_filters( 'woocommerce_process_checkout_field_' . $key, $value ) ); 
  485.  
  486. // BW compatibility. 
  487. $this->legacy_posted_data[ $key ] = $data[ $key ]; 
  488.  
  489. if ( in_array( 'shipping', $skipped ) && ( WC()->cart->needs_shipping_address() || wc_ship_to_billing_address_only() ) ) { 
  490. foreach ( $this->get_checkout_fields( 'shipping' ) as $key => $field ) { 
  491. $data[ $key ] = isset( $data[ 'billing_' . substr( $key, 9 ) ] ) ? $data[ 'billing_' . substr( $key, 9 ) ] : ''; 
  492.  
  493. return $data; 
  494.  
  495. /** 
  496. * Validates the posted checkout data based on field properties. 
  497. * @since 3.0.0 
  498. * @param array $data An array of posted data. 
  499. * @param WP_Error $errors 
  500. */ 
  501. protected function validate_posted_data( &$data, &$errors ) { 
  502. foreach ( $this->get_checkout_fields() as $fieldset_key => $fieldset ) { 
  503. if ( $this->maybe_skip_fieldset( $fieldset_key, $data ) ) { 
  504. continue; 
  505. foreach ( $fieldset as $key => $field ) { 
  506. if ( ! isset( $data[ $key ] ) ) { 
  507. continue; 
  508. $required = ! empty( $field['required'] ); 
  509. $format = array_filter( isset( $field['validate'] ) ? (array) $field['validate'] : array() ); 
  510. $field_label = isset( $field['label'] ) ? $field['label'] : ''; 
  511.  
  512. switch ( $fieldset_key ) { 
  513. case 'shipping' : 
  514. /** translators: %s: field name */ 
  515. $field_label = sprintf( __( 'Shipping %s', 'woocommerce' ), $field_label ); 
  516. break; 
  517. case 'billing' : 
  518. /** translators: %s: field name */ 
  519. $field_label = sprintf( __( 'Billing %s', 'woocommerce' ), $field_label ); 
  520. break; 
  521.  
  522. if ( in_array( 'postcode', $format ) ) { 
  523. $country = isset( $data[ $fieldset_key . '_country' ] ) ? $data[ $fieldset_key . '_country' ] : WC()->customer->{"get_{$fieldset_key}_country"}(); 
  524. $data[ $key ] = wc_format_postcode( $data[ $key ], $country ); 
  525.  
  526. if ( '' !== $data[ $key ] && ! WC_Validation::is_postcode( $data[ $key ], $country ) ) { 
  527. $errors->add( 'validation', __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ) ); 
  528.  
  529. if ( in_array( 'phone', $format ) ) { 
  530. $data[ $key ] = wc_format_phone_number( $data[ $key ] ); 
  531.  
  532. if ( '' !== $data[ $key ] && ! WC_Validation::is_phone( $data[ $key ] ) ) { 
  533. /** translators: %s: phone number */ 
  534. $errors->add( 'validation', sprintf( __( '%s is not a valid phone number.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>' ) ); 
  535.  
  536. if ( in_array( 'email', $format ) && '' !== $data[ $key ] ) { 
  537. $data[ $key ] = sanitize_email( $data[ $key ] ); 
  538.  
  539. if ( ! is_email( $data[ $key ] ) ) { 
  540. /** translators: %s: email address */ 
  541. $errors->add( 'validation', sprintf( __( '%s is not a valid email address.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ) ); 
  542. continue; 
  543.  
  544. if ( '' !== $data[ $key ] && in_array( 'state', $format ) ) { 
  545. $country = isset( $data[ $fieldset_key . '_country' ] ) ? $data[ $fieldset_key . '_country' ] : WC()->customer->{"get_{$fieldset_key}_country"}(); 
  546. $valid_states = WC()->countries->get_states( $country ); 
  547.  
  548. if ( ! empty( $valid_states ) && is_array( $valid_states ) && sizeof( $valid_states ) > 0 ) { 
  549. $valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) ); 
  550.  
  551. // Convert value to key if set 
  552. if ( isset( $valid_state_values[ strtolower( $data[ $key ] ) ] ) ) { 
  553. $data[ $key ] = $valid_state_values[ strtolower( $data[ $key ] ) ]; 
  554.  
  555. if ( ! in_array( $data[ $key ], array_keys( $valid_states ) ) ) { 
  556. /** translators: 1: state field 2: valid states */ 
  557. $errors->add( 'validation', sprintf( __( '%1$s is not valid. Please enter one of the following: %2$s', 'woocommerce' ), '<strong>' . $field_label . '</strong>', implode( ', ', $valid_states ) ) ); 
  558.  
  559. if ( $required && '' === $data[ $key ] ) { 
  560. /** translators: %s: field name */ 
  561. $errors->add( 'required-field', apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) ); 
  562.  
  563. /** 
  564. * Validates that the checkout has enough info to proceed. 
  565. * @since 3.0.0 
  566. * @param array $data An array of posted data. 
  567. * @param WP_Error $errors 
  568. */ 
  569. protected function validate_checkout( &$data, &$errors ) { 
  570. $this->validate_posted_data( $data, $errors ); 
  571. $this->check_cart_items(); 
  572.  
  573. if ( empty( $data['woocommerce_checkout_update_totals'] ) && empty( $data['terms'] ) && apply_filters( 'woocommerce_checkout_show_terms', wc_get_page_id( 'terms' ) > 0 ) ) { 
  574. $errors->add( 'terms', __( 'You must accept our Terms & Conditions.', 'woocommerce' ) ); 
  575.  
  576. if ( WC()->cart->needs_shipping() ) { 
  577. $shipping_country = WC()->customer->get_shipping_country(); 
  578.  
  579. if ( empty( $shipping_country ) ) { 
  580. $errors->add( 'shipping', __( 'Please enter an address to continue.', 'woocommerce' ) ); 
  581. } elseif ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) ) { 
  582. $errors->add( 'shipping', sprintf( __( 'Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce' ), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country() ) ); 
  583. } else { 
  584. $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); 
  585.  
  586. foreach ( WC()->shipping->get_packages() as $i => $package ) { 
  587. if ( ! isset( $chosen_shipping_methods[ $i ], $package['rates'][ $chosen_shipping_methods[ $i ] ] ) ) { 
  588. $errors->add( 'shipping', __( 'No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce' ) ); 
  589.  
  590. if ( WC()->cart->needs_payment() ) { 
  591. $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  592.  
  593. if ( ! isset( $available_gateways[ $data['payment_method'] ] ) ) { 
  594. $errors->add( 'payment', __( 'Invalid payment method.', 'woocommerce' ) ); 
  595. } else { 
  596. $available_gateways[ $data['payment_method'] ]->validate_fields(); 
  597.  
  598. do_action( 'woocommerce_after_checkout_validation', $data, $errors ); 
  599.  
  600. /** 
  601. * Update customer and session data from the posted checkout data. 
  602. * @since 3.0.0 
  603. * @param array $data 
  604. */ 
  605. protected function update_session( $data ) { 
  606. if ( isset( $data['billing_country'] ) ) { 
  607. WC()->customer->set_billing_country( $data['billing_country'] ); 
  608. WC()->customer->set_shipping_country( $data['billing_country'] ); 
  609. if ( isset( $data['billing_state'] ) ) { 
  610. WC()->customer->set_billing_state( $data['billing_state'] ); 
  611. WC()->customer->set_shipping_state( $data['billing_state'] ); 
  612. if ( isset( $data['billing_postcode'] ) ) { 
  613. WC()->customer->set_billing_postcode( $data['billing_postcode'] ); 
  614. WC()->customer->set_shipping_postcode( $data['billing_postcode'] ); 
  615. if ( isset( $data['shipping_country'] ) ) { 
  616. WC()->customer->set_shipping_country( $data['shipping_country'] ); 
  617. if ( isset( $data['shipping_state'] ) ) { 
  618. WC()->customer->set_shipping_state( $data['shipping_state'] ); 
  619. if ( isset( $data['shipping_postcode'] ) ) { 
  620. WC()->customer->set_shipping_postcode( $data['shipping_postcode'] ); 
  621. WC()->customer->save(); 
  622.  
  623. // Update customer shipping and payment method to posted method 
  624. $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); 
  625.  
  626. if ( is_array( $data['shipping_method'] ) ) { 
  627. foreach ( $data['shipping_method'] as $i => $value ) { 
  628. $chosen_shipping_methods[ $i ] = $value; 
  629.  
  630. WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); 
  631. WC()->session->set( 'chosen_payment_method', $data['payment_method'] ); 
  632.  
  633. // Update cart totals now we have customer address. 
  634. WC()->cart->calculate_totals(); 
  635.  
  636.  
  637. /** 
  638. * Process an order that does require payment. 
  639. * @since 3.0.0 
  640. * @param int $order_id 
  641. * @param string $payment_method 
  642. */ 
  643. protected function process_order_payment( $order_id, $payment_method ) { 
  644. $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  645.  
  646. if ( ! isset( $available_gateways[ $payment_method ] ) ) { 
  647. return; 
  648.  
  649. // Store Order ID in session so it can be re-used after payment failure 
  650. WC()->session->set( 'order_awaiting_payment', $order_id ); 
  651.  
  652. // Process Payment 
  653. $result = $available_gateways[ $payment_method ]->process_payment( $order_id ); 
  654.  
  655. // Redirect to success/confirmation/payment page 
  656. if ( isset( $result['result'] ) && 'success' === $result['result'] ) { 
  657. $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); 
  658.  
  659. if ( is_ajax() ) { 
  660. wp_send_json( $result ); 
  661. } else { 
  662. wp_redirect( $result['redirect'] ); 
  663. exit; 
  664.  
  665. /** 
  666. * Process an order that doesn't require payment. 
  667. * @since 3.0.0 
  668. * @param int $order_id 
  669. */ 
  670. protected function process_order_without_payment( $order_id ) { 
  671. $order = wc_get_order( $order_id ); 
  672. $order->payment_complete(); 
  673. wc_empty_cart(); 
  674.  
  675. if ( is_ajax() ) { 
  676. wp_send_json( array( 
  677. 'result' => 'success',  
  678. 'redirect' => apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $order->get_checkout_order_received_url(), $order ),  
  679. ) ); 
  680. } else { 
  681. wp_safe_redirect( 
  682. apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $order->get_checkout_order_received_url(), $order ) 
  683. ); 
  684. exit; 
  685.  
  686. /** 
  687. * Create a new customer account if needed. 
  688. * @param array $data 
  689. * @throws Exception 
  690. */ 
  691. protected function process_customer( $data ) { 
  692. $customer_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() ); 
  693.  
  694. if ( ! is_user_logged_in() && ( $this->is_registration_required() || ! empty( $data['createaccount'] ) ) ) { 
  695. $username = ! empty( $data['account_username'] ) ? $data['account_username'] : ''; 
  696. $password = ! empty( $data['account_password'] ) ? $data['account_password'] : ''; 
  697. $customer_id = wc_create_new_customer( $data['billing_email'], $username, $password ); 
  698.  
  699. if ( is_wp_error( $customer_id ) ) { 
  700. throw new Exception( $customer_id->get_error_message() ); 
  701.  
  702. wp_set_current_user( $customer_id ); 
  703. wc_set_customer_auth_cookie( $customer_id ); 
  704.  
  705. // As we are now logged in, checkout will need to refresh to show logged in data 
  706. WC()->session->set( 'reload_checkout', true ); 
  707.  
  708. // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering 
  709. WC()->cart->calculate_totals(); 
  710.  
  711. // On multisite, ensure user exists on current site, if not add them before allowing login. 
  712. if ( $customer_id && is_multisite() && is_user_logged_in() && ! is_user_member_of_blog() ) { 
  713. add_user_to_blog( get_current_blog_id(), $customer_id, 'customer' ); 
  714.  
  715. // Add customer info from other fields. 
  716. if ( $customer_id && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) { 
  717. $customer = new WC_Customer( $customer_id ); 
  718.  
  719. if ( ! empty( $data['billing_first_name'] ) ) { 
  720. $customer->set_first_name( $data['billing_first_name'] ); 
  721.  
  722. if ( ! empty( $data['billing_last_name'] ) ) { 
  723. $customer->set_last_name( $data['billing_last_name'] ); 
  724.  
  725. foreach ( $data as $key => $value ) { 
  726. // Use setters where available. 
  727. if ( is_callable( array( $customer, "set_{$key}" ) ) ) { 
  728. $customer->{"set_{$key}"}( $value ); 
  729.  
  730. // Store custom fields prefixed with wither shipping_ or billing_. 
  731. } elseif ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) ) { 
  732. $customer->update_meta_data( $key, $value ); 
  733.  
  734. /** 
  735. * Action hook to adjust customer before save. 
  736. * @since 3.0.0 
  737. */ 
  738. do_action( 'woocommerce_checkout_update_customer', $customer, $data ); 
  739.  
  740. $customer->save(); 
  741.  
  742. do_action( 'woocommerce_checkout_update_user_meta', $customer_id, $data ); 
  743.  
  744. /** 
  745. * If checkout failed during an AJAX call, send failure response. 
  746. */ 
  747. protected function send_ajax_failure_response() { 
  748. if ( is_ajax() ) { 
  749. // only print notices if not reloading the checkout, otherwise they're lost in the page reload 
  750. if ( ! isset( WC()->session->reload_checkout ) ) { 
  751. ob_start(); 
  752. wc_print_notices(); 
  753. $messages = ob_get_clean(); 
  754.  
  755. $response = array( 
  756. 'result' => 'failure',  
  757. 'messages' => isset( $messages ) ? $messages : '',  
  758. 'refresh' => isset( WC()->session->refresh_totals ),  
  759. 'reload' => isset( WC()->session->reload_checkout ),  
  760. ); 
  761.  
  762. unset( WC()->session->refresh_totals, WC()->session->reload_checkout ); 
  763.  
  764. wp_send_json( $response ); 
  765.  
  766. /** 
  767. * Process the checkout after the confirm order button is pressed. 
  768. */ 
  769. public function process_checkout() { 
  770. try { 
  771. if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' ) ) { 
  772. WC()->session->set( 'refresh_totals', true ); 
  773. throw new Exception( __( 'We were unable to process your order, please try again.', 'woocommerce' ) ); 
  774.  
  775. wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true ); 
  776. wc_set_time_limit( 0 ); 
  777.  
  778. do_action( 'woocommerce_before_checkout_process' ); 
  779.  
  780. if ( WC()->cart->is_empty() ) { 
  781. throw new Exception( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'shop' ) ) ) ); 
  782.  
  783. do_action( 'woocommerce_checkout_process' ); 
  784.  
  785. $errors = new WP_Error(); 
  786. $posted_data = $this->get_posted_data(); 
  787.  
  788. // Update session for customer and totals. 
  789. $this->update_session( $posted_data ); 
  790.  
  791. // Validate posted data and cart items before proceeding. 
  792. $this->validate_checkout( $posted_data, $errors ); 
  793.  
  794. foreach ( $errors->get_error_messages() as $message ) { 
  795. wc_add_notice( $message, 'error' ); 
  796.  
  797. if ( empty( $posted_data['woocommerce_checkout_update_totals'] ) && 0 === wc_notice_count( 'error' ) ) { 
  798. $this->process_customer( $posted_data ); 
  799. $order_id = $this->create_order( $posted_data ); 
  800. $order = wc_get_order( $order_id ); 
  801.  
  802. if ( is_wp_error( $order_id ) ) { 
  803. throw new Exception( $order_id->get_error_message() ); 
  804.  
  805. do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order ); 
  806.  
  807. if ( WC()->cart->needs_payment() ) { 
  808. $this->process_order_payment( $order_id, $posted_data['payment_method'] ); 
  809. } else { 
  810. $this->process_order_without_payment( $order_id ); 
  811. } catch ( Exception $e ) { 
  812. wc_add_notice( $e->getMessage(), 'error' ); 
  813. $this->send_ajax_failure_response(); 
  814.  
  815. /** 
  816. * Get a posted address field after sanitization and validation. 
  817. * @param string $key 
  818. * @param string $type billing for shipping 
  819. * @return string 
  820. */ 
  821. public function get_posted_address_data( $key, $type = 'billing' ) { 
  822. if ( 'billing' === $type || false === $this->legacy_posted_data['ship_to_different_address'] ) { 
  823. $return = isset( $this->legacy_posted_data[ 'billing_' . $key ] ) ? $this->legacy_posted_data[ 'billing_' . $key ] : ''; 
  824. } else { 
  825. $return = isset( $this->legacy_posted_data[ 'shipping_' . $key ] ) ? $this->legacy_posted_data[ 'shipping_' . $key ] : ''; 
  826. return $return; 
  827.  
  828. /** 
  829. * Gets the value either from the posted data, or from the users meta data. 
  830. * @param string $input 
  831. * @return string 
  832. */ 
  833. public function get_value( $input ) { 
  834. if ( ! empty( $_POST[ $input ] ) ) { 
  835. return wc_clean( $_POST[ $input ] ); 
  836.  
  837. } else { 
  838.  
  839. $value = apply_filters( 'woocommerce_checkout_get_value', null, $input ); 
  840.  
  841. if ( null !== $value ) { 
  842. return $value; 
  843.  
  844. if ( is_callable( array( WC()->customer, "get_$input" ) ) ) { 
  845. $value = WC()->customer->{"get_$input"}() ? WC()->customer->{"get_$input"}() : null; 
  846. } elseif ( WC()->customer->meta_exists( $input ) ) { 
  847. $value = WC()->customer->get_meta( $input, true ); 
  848.  
  849. return apply_filters( 'default_checkout_' . $input, $value, $input );