/includes/class-wc-checkout.php

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