2011-08-09 15:16:18 +00:00
< ? php
2015-11-06 09:22:19 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2016-11-25 21:46:34 +00:00
exit ;
2015-11-06 09:22:19 +00:00
}
2011-08-09 15:16:18 +00:00
/**
2016-11-25 21:46:34 +00:00
* Main checkout class .
2012-08-10 11:15:32 +00:00
*
2011-08-10 17:11:11 +00:00
* The WooCommerce checkout class handles the checkout process , collecting user data and processing the payment .
2011-08-09 15:16:18 +00:00
*
2016-12-02 16:13:36 +00:00
* @ class WC_Checkout
* @ package WooCommerce / Classes
* @ category Class
* @ author WooThemes
2011-08-09 15:16:18 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Checkout {
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* The single instance of the class .
*
* @ var WC_Checkout | null
*/
protected static $instance = null ;
2012-12-10 12:34:55 +00:00
2016-11-25 21:46:34 +00:00
/**
* Checkout fields are stored here .
*
* @ var array | null
*/
protected $fields = null ;
2012-12-27 19:24:33 +00:00
2017-04-18 20:55:31 +00:00
/**
* Holds posted data for backwards compatibility .
* @ var array
*/
protected $legacy_posted_data = array ();
2016-11-25 21:46:34 +00:00
/**
* Gets the main WC_Checkout Instance .
*
* @ since 2.1
* @ static
* @ return WC_Checkout Main instance
*/
public static function instance () {
if ( is_null ( self :: $instance ) ) {
self :: $instance = new self ();
2012-12-27 19:24:33 +00:00
2016-11-25 21:46:34 +00:00
// Hook in actions once.
add_action ( 'woocommerce_checkout_billing' , array ( self :: $instance , 'checkout_form_billing' ) );
add_action ( 'woocommerce_checkout_shipping' , array ( self :: $instance , 'checkout_form_shipping' ) );
2013-02-15 21:18:46 +00:00
2016-11-25 21:46:34 +00:00
// woocommerce_checkout_init action is ran once when the class is first constructed.
do_action ( 'woocommerce_checkout_init' , self :: $instance );
}
return self :: $instance ;
}
2015-02-03 15:27:40 +00:00
2013-09-12 13:41:02 +00:00
/**
2016-11-25 21:46:34 +00:00
* See if variable is set . Used to support legacy public variables which are no longer defined .
*
* @ param string $key
* @ return bool
2013-09-12 13:41:02 +00:00
*/
2016-11-25 21:46:34 +00:00
public function __isset ( $key ) {
return in_array ( $key , array (
'enable_signup' ,
'enable_guest_checkout' ,
'must_create_account' ,
'checkout_fields' ,
'posted' ,
'shipping_method' ,
'payment_method' ,
'customer_id' ,
'shipping_methods' ,
) );
}
2015-02-03 15:27:40 +00:00
2013-09-12 13:41:02 +00:00
/**
2016-11-25 21:46:34 +00:00
* Sets the legacy public variables for backwards compatibility .
2013-09-12 13:41:02 +00:00
*
2016-11-25 21:46:34 +00:00
* @ param string $key
* @ param mixed $value
*/
public function __set ( $key , $value ) {
switch ( $key ) {
case 'enable_signup' :
$bool_value = wc_string_to_bool ( $value );
if ( $bool_value !== $this -> is_registration_enabled () ) {
remove_filter ( 'woocommerce_checkout_registration_enabled' , '__return_true' , 0 );
remove_filter ( 'woocommerce_checkout_registration_enabled' , '__return_false' , 0 );
add_filter ( 'woocommerce_checkout_registration_enabled' , $bool_value ? '__return_true' : '__return_false' , 0 );
}
break ;
case 'enable_guest_checkout' :
$bool_value = wc_string_to_bool ( $value );
2017-04-10 20:18:49 +00:00
if ( $bool_value === $this -> is_registration_required () ) {
2016-11-25 21:46:34 +00:00
remove_filter ( 'woocommerce_checkout_registration_required' , '__return_true' , 0 );
remove_filter ( 'woocommerce_checkout_registration_required' , '__return_false' , 0 );
add_filter ( 'woocommerce_checkout_registration_required' , $bool_value ? '__return_false' : '__return_true' , 0 );
}
break ;
case 'checkout_fields' :
$this -> fields = $value ;
break ;
case 'shipping_methods' :
WC () -> session -> set ( 'chosen_shipping_methods' , $value );
break ;
2017-04-18 20:55:31 +00:00
case 'posted' :
2016-11-25 21:46:34 +00:00
$this -> legacy_posted_data = $value ;
break ;
}
}
/**
* Gets the legacy public variables for backwards compatibility .
2013-09-12 13:41:02 +00:00
*
2016-11-25 21:46:34 +00:00
* @ param string $key
2017-05-15 11:50:52 +00:00
*
* @ return array | string
2013-09-12 13:41:02 +00:00
*/
2016-11-25 21:46:34 +00:00
public function __get ( $key ) {
2017-04-18 20:55:31 +00:00
if ( in_array ( $key , array ( 'posted' , 'shipping_method' , 'payment_method' ) ) && empty ( $this -> legacy_posted_data ) ) {
2016-12-02 16:13:36 +00:00
$this -> legacy_posted_data = $this -> get_posted_data ();
}
2016-11-25 21:46:34 +00:00
switch ( $key ) {
case 'enable_signup' :
return $this -> is_registration_enabled ();
case 'enable_guest_checkout' :
return ! $this -> is_registration_required ();
case 'must_create_account' :
return $this -> is_registration_required () && ! is_user_logged_in ();
case 'checkout_fields' :
return $this -> get_checkout_fields ();
case 'posted' :
2017-04-18 21:12:42 +00:00
wc_doing_it_wrong ( 'WC_Checkout->posted' , 'Use $_POST directly.' , '3.0.0' );
2016-11-25 21:46:34 +00:00
return $this -> legacy_posted_data ;
case 'shipping_method' :
return $this -> legacy_posted_data [ 'shipping_method' ];
case 'payment_method' :
return $this -> legacy_posted_data [ 'payment_method' ];
case 'customer_id' :
return apply_filters ( 'woocommerce_checkout_customer_id' , get_current_user_id () );
case 'shipping_methods' :
return ( array ) WC () -> session -> get ( 'chosen_shipping_methods' );
}
2013-09-12 13:41:02 +00:00
}
/**
* Cloning is forbidden .
*/
public function __clone () {
2016-11-23 16:15:00 +00:00
wc_doing_it_wrong ( __FUNCTION__ , __ ( 'Cheatin’ huh?' , 'woocommerce' ), '2.1' );
2013-09-12 13:41:02 +00:00
}
/**
* Unserializing instances of this class is forbidden .
*/
public function __wakeup () {
2016-11-23 16:15:00 +00:00
wc_doing_it_wrong ( __FUNCTION__ , __ ( 'Cheatin’ huh?' , 'woocommerce' ), '2.1' );
2013-09-12 13:41:02 +00:00
}
2012-08-14 19:42:38 +00:00
/**
2016-11-25 21:46:34 +00:00
* Is registration required to checkout ?
2012-08-14 19:42:38 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-25 21:46:34 +00:00
* @ return boolean
*/
public function is_registration_required () {
return apply_filters ( 'woocommerce_checkout_registration_required' , 'yes' !== get_option ( 'woocommerce_enable_guest_checkout' ) );
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Is registration enabled on the checkout page ?
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-25 21:46:34 +00:00
* @ return boolean
*/
public function is_registration_enabled () {
return apply_filters ( 'woocommerce_checkout_registration_enabled' , 'yes' === get_option ( 'woocommerce_enable_signup_and_login_from_checkout' ) );
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Get an array of checkout fields .
*
* @ param string $fieldset to get .
* @ return array
*/
public function get_checkout_fields ( $fieldset = '' ) {
if ( is_null ( $this -> fields ) ) {
$this -> fields = array (
'billing' => WC () -> countries -> get_address_fields ( $this -> get_value ( 'billing_country' ), 'billing_' ),
'shipping' => WC () -> countries -> get_address_fields ( $this -> get_value ( 'shipping_country' ), 'shipping_' ),
'account' => array (),
'order' => array (
'order_comments' => array (
'type' => 'textarea' ,
'class' => array ( 'notes' ),
'label' => __ ( 'Order notes' , 'woocommerce' ),
'placeholder' => esc_attr__ ( 'Notes about your order, e.g. special notes for delivery.' , 'woocommerce' ),
),
),
);
if ( 'no' === get_option ( 'woocommerce_registration_generate_username' ) ) {
$this -> fields [ 'account' ][ 'account_username' ] = array (
'type' => 'text' ,
'label' => __ ( 'Account username' , 'woocommerce' ),
'required' => true ,
'placeholder' => esc_attr__ ( 'Username' , 'woocommerce' ),
);
}
2012-12-10 12:34:55 +00:00
2016-11-25 21:46:34 +00:00
if ( 'no' === get_option ( 'woocommerce_registration_generate_password' ) ) {
$this -> fields [ 'account' ][ 'account_password' ] = array (
'type' => 'password' ,
2017-08-18 15:18:49 +00:00
'label' => __ ( 'Create account password' , 'woocommerce' ),
2016-11-25 21:46:34 +00:00
'required' => true ,
'placeholder' => esc_attr__ ( 'Password' , 'woocommerce' ),
);
}
2012-12-10 12:34:55 +00:00
2016-11-25 21:46:34 +00:00
$this -> fields = apply_filters ( 'woocommerce_checkout_fields' , $this -> fields );
}
if ( $fieldset ) {
return $this -> fields [ $fieldset ];
} else {
return $this -> fields ;
}
2011-08-09 15:16:18 +00:00
}
2012-08-10 11:15:32 +00:00
2012-08-14 19:42:38 +00:00
/**
2016-11-25 21:46:34 +00:00
* When we process the checkout , lets ensure cart items are rechecked to prevent checkout .
2012-08-14 19:42:38 +00:00
*/
2013-09-04 10:26:19 +00:00
public function check_cart_items () {
2016-09-02 01:51:31 +00:00
do_action ( 'woocommerce_check_cart_items' );
2012-02-12 11:36:33 +00:00
}
2012-08-10 11:15:32 +00:00
2012-08-14 19:42:38 +00:00
/**
2016-11-25 21:46:34 +00:00
* Output the billing form .
2012-08-14 19:42:38 +00:00
*/
2012-12-14 21:27:29 +00:00
public function checkout_form_billing () {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'checkout/form-billing.php' , array ( 'checkout' => $this ) );
2011-08-09 15:16:18 +00:00
}
2012-08-10 11:15:32 +00:00
2012-08-14 19:42:38 +00:00
/**
2016-11-25 21:46:34 +00:00
* Output the shipping form .
2012-08-14 19:42:38 +00:00
*/
2012-12-14 21:27:29 +00:00
public function checkout_form_shipping () {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'checkout/form-shipping.php' , array ( 'checkout' => $this ) );
2011-08-09 15:16:18 +00:00
}
2012-12-12 21:14:19 +00:00
/**
2016-01-11 10:47:10 +00:00
* Create an order . Error codes :
* 520 - Cannot insert order into the database .
* 521 - Cannot get order after creation .
* 522 - Cannot update order .
* 525 - Cannot create line item .
* 526 - Cannot create fee item .
* 527 - Cannot create shipping item .
* 528 - Cannot create tax item .
* 529 - Cannot create coupon item .
2016-11-25 21:46:34 +00:00
*
2013-10-06 04:44:04 +00:00
* @ throws Exception
2016-11-25 21:46:34 +00:00
* @ param $data Posted data .
2014-06-18 15:03:46 +00:00
* @ return int | WP_ERROR
2012-12-12 21:14:19 +00:00
*/
2016-11-25 21:46:34 +00:00
public function create_order ( $data ) {
// Give plugins the opportunity to create an order themselves.
2014-06-11 14:10:03 +00:00
if ( $order_id = apply_filters ( 'woocommerce_create_order' , null , $this ) ) {
2013-02-13 09:38:54 +00:00
return $order_id ;
2014-06-11 14:10:03 +00:00
}
2013-02-13 09:38:54 +00:00
2014-06-18 15:03:46 +00:00
try {
2017-02-07 14:51:21 +00:00
$order_id = absint ( WC () -> session -> get ( 'order_awaiting_payment' ) );
$cart_hash = md5 ( json_encode ( wc_clean ( WC () -> cart -> get_cart_for_session () ) ) . WC () -> cart -> total );
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
2012-12-12 21:14:19 +00:00
2016-02-05 09:41:25 +00:00
/**
* If there is an order pending payment , we can resume it here so
* long as it has not changed . If the order has changed , i . e .
* different items or cost , create a new order . We use a hash to
* detect changes which is based on cart items + order total .
*/
2016-08-09 13:02:40 +00:00
if ( $order_id && ( $order = wc_get_order ( $order_id ) ) && $order -> has_cart_hash ( $cart_hash ) && $order -> has_status ( array ( 'pending' , 'failed' ) ) ) {
// Action for 3rd parties.
do_action ( 'woocommerce_resume_order' , $order_id );
2012-12-12 21:14:19 +00:00
2016-08-09 13:02:40 +00:00
// Remove all items - we will re-add them later.
$order -> remove_order_items ();
2014-06-11 14:10:03 +00:00
} else {
2016-08-09 13:02:40 +00:00
$order = new WC_Order ();
}
2012-12-12 21:14:19 +00:00
2016-11-25 21:46:34 +00:00
foreach ( $data as $key => $value ) {
if ( is_callable ( array ( $order , " set_ { $key } " ) ) ) {
$order -> { " set_ { $key } " }( $value );
2016-12-19 14:51:56 +00:00
// Store custom fields prefixed with wither shipping_ or billing_. This is for backwards compatibility with 2.6.x.
2017-09-05 17:56:56 +00:00
// TODO: Fix conditional to only include shipping/billing address fields in a smarter way without str(i)pos.
} elseif ( ( 0 === stripos ( $key , 'billing_' ) || 0 === stripos ( $key , 'shipping_' ) )
&& ! in_array ( $key , array ( 'shipping_method' , 'shipping_total' , 'shipping_tax' ) ) ) {
2016-12-19 14:51:56 +00:00
$order -> update_meta_data ( '_' . $key , $value );
2016-11-25 21:46:34 +00:00
}
}
2016-08-09 13:02:40 +00:00
$order -> set_created_via ( 'checkout' );
$order -> set_cart_hash ( $cart_hash );
2016-11-25 21:46:34 +00:00
$order -> set_customer_id ( apply_filters ( 'woocommerce_checkout_customer_id' , get_current_user_id () ) );
2016-08-09 13:02:40 +00:00
$order -> set_currency ( get_woocommerce_currency () );
$order -> set_prices_include_tax ( 'yes' === get_option ( 'woocommerce_prices_include_tax' ) );
$order -> set_customer_ip_address ( WC_Geolocation :: get_ip_address () );
$order -> set_customer_user_agent ( wc_get_user_agent () );
2016-11-25 21:46:34 +00:00
$order -> set_customer_note ( isset ( $data [ 'order_comments' ] ) ? $data [ 'order_comments' ] : '' );
2017-02-07 14:51:21 +00:00
$order -> set_payment_method ( isset ( $available_gateways [ $data [ 'payment_method' ] ] ) ? $available_gateways [ $data [ 'payment_method' ] ] : $data [ 'payment_method' ] );
2017-08-18 14:05:01 +00:00
$order -> set_shipping_total ( WC () -> cart -> get_shipping_total () );
2017-08-18 11:51:45 +00:00
$order -> set_discount_total ( WC () -> cart -> get_discount_total () );
2017-08-18 14:05:01 +00:00
$order -> set_discount_tax ( WC () -> cart -> get_discount_tax () );
$order -> set_cart_tax ( WC () -> cart -> get_cart_contents_tax () + WC () -> cart -> get_fee_tax () );
$order -> set_shipping_tax ( WC () -> cart -> get_shipping_tax () );
$order -> set_total ( WC () -> cart -> get_total ( 'edit' ) );
2017-02-10 00:35:19 +00:00
$this -> create_order_line_items ( $order , WC () -> cart );
$this -> create_order_fee_lines ( $order , WC () -> cart );
$this -> create_order_shipping_lines ( $order , WC () -> session -> get ( 'chosen_shipping_methods' ), WC () -> shipping -> get_packages () );
$this -> create_order_tax_lines ( $order , WC () -> cart );
$this -> create_order_coupon_lines ( $order , WC () -> cart );
2016-12-19 11:58:00 +00:00
/**
2016-12-22 17:41:23 +00:00
* Action hook to adjust order before save .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-12-19 11:58:00 +00:00
*/
do_action ( 'woocommerce_checkout_create_order' , $order , $data );
// Save the order.
2016-11-25 21:46:34 +00:00
$order_id = $order -> save ();
2012-12-12 21:14:19 +00:00
2016-11-25 21:46:34 +00:00
do_action ( 'woocommerce_checkout_update_order_meta' , $order_id , $data );
2012-12-12 21:14:19 +00:00
2016-11-25 21:46:34 +00:00
return $order_id ;
} catch ( Exception $e ) {
return new WP_Error ( 'checkout-error' , $e -> getMessage () );
}
}
2016-08-09 13:02:40 +00:00
2016-11-25 21:46:34 +00:00
/**
* Add line items to the order .
*
* @ param WC_Order $order
2017-05-15 11:50:52 +00:00
* @ param WC_Cart $cart
2016-11-25 21:46:34 +00:00
*/
2017-02-10 00:35:19 +00:00
public function create_order_line_items ( & $order , $cart ) {
foreach ( $cart -> get_cart () as $cart_item_key => $values ) {
2017-05-23 18:16:30 +00:00
/**
2017-07-10 05:56:28 +00:00
* Filter hook to get initial item object .
2017-05-23 18:16:30 +00:00
* @ since 3.1 . 0
*/
$item = apply_filters ( 'woocommerce_checkout_create_order_line_item_object' , new WC_Order_Item_Product (), $cart_item_key , $values , $order );
2016-12-19 11:58:00 +00:00
$product = $values [ 'data' ];
$item -> legacy_values = $values ; // @deprecated For legacy actions.
$item -> legacy_cart_item_key = $cart_item_key ; // @deprecated For legacy actions.
2016-11-25 21:46:34 +00:00
$item -> set_props ( array (
'quantity' => $values [ 'quantity' ],
'variation' => $values [ 'variation' ],
'subtotal' => $values [ 'line_subtotal' ],
'total' => $values [ 'line_total' ],
'subtotal_tax' => $values [ 'line_subtotal_tax' ],
'total_tax' => $values [ 'line_tax' ],
'taxes' => $values [ 'line_tax_data' ],
) );
2017-02-01 01:11:56 +00:00
if ( $product ) {
$item -> set_props ( array (
'name' => $product -> get_name (),
'tax_class' => $product -> get_tax_class (),
'product_id' => $product -> is_type ( 'variation' ) ? $product -> get_parent_id () : $product -> get_id (),
'variation_id' => $product -> is_type ( 'variation' ) ? $product -> get_id () : 0 ,
) );
}
2016-11-25 21:46:34 +00:00
$item -> set_backorder_meta ();
2016-12-19 11:58:00 +00:00
/**
2016-12-22 15:06:10 +00:00
* Action hook to adjust item before save .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-12-19 11:58:00 +00:00
*/
2017-02-21 05:35:00 +00:00
do_action ( 'woocommerce_checkout_create_order_line_item' , $item , $cart_item_key , $values , $order );
2016-12-19 11:58:00 +00:00
// Add item to order and save.
2016-11-25 21:46:34 +00:00
$order -> add_item ( $item );
}
}
/**
* Add fees to the order .
*
* @ param WC_Order $order
2017-05-15 11:50:52 +00:00
* @ param WC_Cart $cart
2016-11-25 21:46:34 +00:00
*/
2017-02-10 00:35:19 +00:00
public function create_order_fee_lines ( & $order , $cart ) {
foreach ( $cart -> get_fees () as $fee_key => $fee ) {
2016-12-19 11:58:00 +00:00
$item = new WC_Order_Item_Fee ();
$item -> legacy_fee = $fee ; // @deprecated For legacy actions.
$item -> legacy_fee_key = $fee_key ; // @deprecated For legacy actions.
2016-11-25 21:46:34 +00:00
$item -> set_props ( array (
'name' => $fee -> name ,
2017-08-23 11:15:06 +00:00
'tax_class' => $fee -> taxable ? $fee -> tax_class : 0 ,
'amount' => $fee -> amount ,
'total' => $fee -> total ,
2016-11-25 21:46:34 +00:00
'total_tax' => $fee -> tax ,
'taxes' => array (
'total' => $fee -> tax_data ,
),
) );
2016-12-19 11:58:00 +00:00
/**
2016-12-22 15:06:10 +00:00
* Action hook to adjust item before save .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-12-19 11:58:00 +00:00
*/
2017-02-21 05:35:00 +00:00
do_action ( 'woocommerce_checkout_create_order_fee_item' , $item , $fee_key , $fee , $order );
2016-12-19 11:58:00 +00:00
// Add item to order and save.
2016-11-25 21:46:34 +00:00
$order -> add_item ( $item );
}
}
2012-12-12 21:14:19 +00:00
2016-11-25 21:46:34 +00:00
/**
* Add shipping lines to the order .
*
* @ param WC_Order $order
2017-05-15 11:50:52 +00:00
* @ param array $chosen_shipping_methods
* @ param array $packages
2016-11-25 21:46:34 +00:00
*/
2017-02-10 00:35:19 +00:00
public function create_order_shipping_lines ( & $order , $chosen_shipping_methods , $packages ) {
foreach ( $packages as $package_key => $package ) {
2016-11-25 21:46:34 +00:00
if ( isset ( $chosen_shipping_methods [ $package_key ], $package [ 'rates' ][ $chosen_shipping_methods [ $package_key ] ] ) ) {
2017-01-18 18:41:32 +00:00
/** @var WC_Shipping_Rate $shipping_rate */
2016-12-19 11:58:00 +00:00
$shipping_rate = $package [ 'rates' ][ $chosen_shipping_methods [ $package_key ] ];
$item = new WC_Order_Item_Shipping ();
$item -> legacy_package_key = $package_key ; // @deprecated For legacy actions.
2016-11-17 21:30:34 +00:00
$item -> set_props ( array (
2016-11-25 21:46:34 +00:00
'method_title' => $shipping_rate -> label ,
'method_id' => $shipping_rate -> id ,
'total' => wc_format_decimal ( $shipping_rate -> cost ),
2016-12-22 18:49:42 +00:00
'taxes' => array (
'total' => $shipping_rate -> taxes ,
),
2016-08-09 13:02:40 +00:00
) );
2016-12-19 11:58:00 +00:00
2017-01-23 11:30:53 +00:00
foreach ( $shipping_rate -> get_meta_data () as $key => $value ) {
2017-01-18 18:41:32 +00:00
$item -> add_meta_data ( $key , $value , true );
}
2016-12-19 11:58:00 +00:00
/**
2016-12-22 15:06:10 +00:00
* Action hook to adjust item before save .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-12-19 11:58:00 +00:00
*/
2017-02-21 05:35:00 +00:00
do_action ( 'woocommerce_checkout_create_order_shipping_item' , $item , $package_key , $package , $order );
2016-12-19 11:58:00 +00:00
// Add item to order and save.
2016-08-09 13:02:40 +00:00
$order -> add_item ( $item );
2013-08-08 11:37:42 +00:00
}
2016-11-25 21:46:34 +00:00
}
}
2013-08-08 11:37:42 +00:00
2016-11-25 21:46:34 +00:00
/**
* Add tax lines to the order .
*
* @ param WC_Order $order
2017-05-15 11:50:52 +00:00
* @ param WC_Cart $cart
2016-11-25 21:46:34 +00:00
*/
2017-02-10 00:35:19 +00:00
public function create_order_tax_lines ( & $order , $cart ) {
2017-11-21 10:49:26 +00:00
foreach ( array_keys ( $cart -> get_cart_contents_taxes () + $cart -> get_shipping_taxes () + $cart -> get_fee_taxes () ) as $tax_rate_id ) {
2016-11-25 21:46:34 +00:00
if ( $tax_rate_id && apply_filters ( 'woocommerce_cart_remove_taxes_zero_rate_id' , 'zero-rated' ) !== $tax_rate_id ) {
$item = new WC_Order_Item_Tax ();
2016-11-17 21:30:34 +00:00
$item -> set_props ( array (
2016-11-25 21:46:34 +00:00
'rate_id' => $tax_rate_id ,
2017-02-10 00:35:19 +00:00
'tax_total' => $cart -> get_tax_amount ( $tax_rate_id ),
'shipping_tax_total' => $cart -> get_shipping_tax_amount ( $tax_rate_id ),
2016-11-25 21:46:34 +00:00
'rate_code' => WC_Tax :: get_rate_code ( $tax_rate_id ),
'label' => WC_Tax :: get_rate_label ( $tax_rate_id ),
'compound' => WC_Tax :: is_compound ( $tax_rate_id ),
2016-08-09 13:02:40 +00:00
) );
2016-12-19 11:58:00 +00:00
/**
2016-12-22 15:06:10 +00:00
* Action hook to adjust item before save .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-12-19 11:58:00 +00:00
*/
2017-02-21 05:35:00 +00:00
do_action ( 'woocommerce_checkout_create_order_tax_item' , $item , $tax_rate_id , $order );
2016-12-19 11:58:00 +00:00
// Add item to order and save.
2016-08-09 13:02:40 +00:00
$order -> add_item ( $item );
2012-12-12 21:14:19 +00:00
}
2014-02-13 15:51:16 +00:00
}
2016-11-25 21:46:34 +00:00
}
2012-12-12 21:14:19 +00:00
2016-11-25 21:46:34 +00:00
/**
* Add coupon lines to the order .
*
2017-08-24 16:07:49 +00:00
* @ param WC_Order $order
* @ param WC_Cart $cart
2016-11-25 21:46:34 +00:00
*/
2017-02-10 00:35:19 +00:00
public function create_order_coupon_lines ( & $order , $cart ) {
foreach ( $cart -> get_coupons () as $code => $coupon ) {
2016-11-25 21:46:34 +00:00
$item = new WC_Order_Item_Coupon ();
$item -> set_props ( array (
'code' => $code ,
2017-02-10 00:35:19 +00:00
'discount' => $cart -> get_coupon_discount_amount ( $code ),
'discount_tax' => $cart -> get_coupon_discount_tax_amount ( $code ),
2016-11-25 21:46:34 +00:00
) );
2017-08-24 16:07:49 +00:00
$item -> add_meta_data ( 'coupon_data' , $coupon -> get_data () );
2016-12-19 11:58:00 +00:00
/**
2016-12-22 15:06:10 +00:00
* Action hook to adjust item before save .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-12-19 11:58:00 +00:00
*/
2017-02-21 05:35:00 +00:00
do_action ( 'woocommerce_checkout_create_order_coupon_item' , $item , $code , $coupon , $order );
2016-12-19 11:58:00 +00:00
// Add item to order and save.
2016-11-25 21:46:34 +00:00
$order -> add_item ( $item );
}
2012-12-12 21:14:19 +00:00
}
2016-08-09 13:02:40 +00:00
/**
2016-11-25 21:46:34 +00:00
* See if a fieldset should be skipped .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-05-12 08:48:46 +00:00
*
2016-11-25 21:46:34 +00:00
* @ param string $fieldset_key
* @ param array $data
2017-05-12 08:48:46 +00:00
*
* @ return bool
2016-08-09 13:02:40 +00:00
*/
2016-11-25 21:46:34 +00:00
protected function maybe_skip_fieldset ( $fieldset_key , $data ) {
if ( 'shipping' === $fieldset_key && ( ! $data [ 'ship_to_different_address' ] || ! WC () -> cart -> needs_shipping_address () ) ) {
return true ;
}
if ( 'account' === $fieldset_key && ( is_user_logged_in () || ( ! $this -> is_registration_required () && empty ( $data [ 'createaccount' ] ) ) ) ) {
return true ;
2016-08-09 13:02:40 +00:00
}
2016-11-25 21:46:34 +00:00
return false ;
2016-08-09 13:02:40 +00:00
}
2011-09-12 15:34:29 +00:00
/**
2016-11-25 21:46:34 +00:00
* Get posted data from the checkout form .
*
2017-05-23 17:48:16 +00:00
* @ since 3.1 . 0
2017-03-29 10:47:33 +00:00
* @ return array of data .
2011-09-12 15:34:29 +00:00
*/
2017-05-12 09:23:10 +00:00
public function get_posted_data () {
2016-12-19 15:42:53 +00:00
$skipped = array ();
$data = array (
2016-11-25 21:46:34 +00:00
'terms' => ( int ) isset ( $_POST [ 'terms' ] ),
'createaccount' => ( int ) ! empty ( $_POST [ 'createaccount' ] ),
'payment_method' => isset ( $_POST [ 'payment_method' ] ) ? wc_clean ( $_POST [ 'payment_method' ] ) : '' ,
'shipping_method' => isset ( $_POST [ 'shipping_method' ] ) ? wc_clean ( $_POST [ 'shipping_method' ] ) : '' ,
'ship_to_different_address' => ! empty ( $_POST [ 'ship_to_different_address' ] ) && ! wc_ship_to_billing_address_only (),
'woocommerce_checkout_update_totals' => isset ( $_POST [ 'woocommerce_checkout_update_totals' ] ),
);
foreach ( $this -> get_checkout_fields () as $fieldset_key => $fieldset ) {
if ( $this -> maybe_skip_fieldset ( $fieldset_key , $data ) ) {
2016-12-19 15:42:53 +00:00
$skipped [] = $fieldset_key ;
2016-11-25 21:46:34 +00:00
continue ;
2014-10-31 15:03:53 +00:00
}
2016-11-25 21:46:34 +00:00
foreach ( $fieldset as $key => $field ) {
$type = sanitize_title ( isset ( $field [ 'type' ] ) ? $field [ 'type' ] : 'text' );
switch ( $type ) {
case 'checkbox' :
2017-07-19 13:58:46 +00:00
$value = isset ( $_POST [ $key ] ) ? 1 : '' ;
2016-11-25 21:46:34 +00:00
break ;
case 'multiselect' :
$value = isset ( $_POST [ $key ] ) ? implode ( ', ' , wc_clean ( $_POST [ $key ] ) ) : '' ;
break ;
case 'textarea' :
$value = isset ( $_POST [ $key ] ) ? wc_sanitize_textarea ( $_POST [ $key ] ) : '' ;
break ;
default :
$value = isset ( $_POST [ $key ] ) ? wc_clean ( $_POST [ $key ] ) : '' ;
break ;
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
$data [ $key ] = apply_filters ( 'woocommerce_process_checkout_' . $type . '_field' , apply_filters ( 'woocommerce_process_checkout_field_' . $key , $value ) );
2014-10-31 15:03:53 +00:00
}
2016-11-25 21:46:34 +00:00
}
2016-12-19 15:42:53 +00:00
2017-04-06 08:50:34 +00:00
if ( in_array ( 'shipping' , $skipped ) && ( WC () -> cart -> needs_shipping_address () || wc_ship_to_billing_address_only () ) ) {
2016-12-19 15:42:53 +00:00
foreach ( $this -> get_checkout_fields ( 'shipping' ) as $key => $field ) {
$data [ $key ] = isset ( $data [ 'billing_' . substr ( $key , 9 ) ] ) ? $data [ 'billing_' . substr ( $key , 9 ) ] : '' ;
}
}
2017-05-30 11:36:20 +00:00
// BW compatibility.
$this -> legacy_posted_data = $data ;
2017-09-08 19:34:55 +00:00
return apply_filters ( 'woocommerce_checkout_posted_data' , $data );
2016-11-25 21:46:34 +00:00
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Validates the posted checkout data based on field properties .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-25 21:46:34 +00:00
* @ param array $data An array of posted data .
* @ param WP_Error $errors
*/
protected function validate_posted_data ( & $data , & $errors ) {
foreach ( $this -> get_checkout_fields () as $fieldset_key => $fieldset ) {
if ( $this -> maybe_skip_fieldset ( $fieldset_key , $data ) ) {
continue ;
2014-05-01 13:40:00 +00:00
}
2016-11-25 21:46:34 +00:00
foreach ( $fieldset as $key => $field ) {
if ( ! isset ( $data [ $key ] ) ) {
2014-10-31 15:03:53 +00:00
continue ;
}
2016-11-25 21:46:34 +00:00
$required = ! empty ( $field [ 'required' ] );
$format = array_filter ( isset ( $field [ 'validate' ] ) ? ( array ) $field [ 'validate' ] : array () );
$field_label = isset ( $field [ 'label' ] ) ? $field [ 'label' ] : '' ;
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
switch ( $fieldset_key ) {
case 'shipping' :
/* translators: %s: field name */
2017-04-26 10:40:59 +00:00
$field_label = sprintf ( __ ( 'Shipping %s' , 'woocommerce' ), $field_label );
2016-11-25 21:46:34 +00:00
break ;
case 'billing' :
/* translators: %s: field name */
2017-04-26 10:40:59 +00:00
$field_label = sprintf ( __ ( 'Billing %s' , 'woocommerce' ), $field_label );
2016-11-25 21:46:34 +00:00
break ;
2014-10-31 15:03:53 +00:00
}
2013-07-19 05:58:45 +00:00
2016-11-25 21:46:34 +00:00
if ( in_array ( 'postcode' , $format ) ) {
$country = isset ( $data [ $fieldset_key . '_country' ] ) ? $data [ $fieldset_key . '_country' ] : WC () -> customer -> { " get_ { $fieldset_key } _country " }();
$data [ $key ] = wc_format_postcode ( $data [ $key ], $country );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( '' !== $data [ $key ] && ! WC_Validation :: is_postcode ( $data [ $key ], $country ) ) {
2017-11-02 18:54:03 +00:00
$errors -> add ( 'validation' , sprintf ( __ ( '%s is not a valid postcode / ZIP.' , 'woocommerce' ), '<strong>' . esc_html ( $field_label ) . '</strong>' ) );
2014-10-31 15:03:53 +00:00
}
2016-11-25 21:46:34 +00:00
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( in_array ( 'phone' , $format ) ) {
$data [ $key ] = wc_format_phone_number ( $data [ $key ] );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( '' !== $data [ $key ] && ! WC_Validation :: is_phone ( $data [ $key ] ) ) {
/* translators: %s: phone number */
2017-04-26 10:40:59 +00:00
$errors -> add ( 'validation' , sprintf ( __ ( '%s is not a valid phone number.' , 'woocommerce' ), '<strong>' . esc_html ( $field_label ) . '</strong>' ) );
2014-10-31 15:03:53 +00:00
}
2016-11-25 21:46:34 +00:00
}
2012-08-10 11:15:32 +00:00
2017-04-20 13:45:04 +00:00
if ( in_array ( 'email' , $format ) && '' !== $data [ $key ] ) {
2016-11-25 21:46:34 +00:00
$data [ $key ] = sanitize_email ( $data [ $key ] );
2017-04-20 13:45:04 +00:00
if ( ! is_email ( $data [ $key ] ) ) {
2016-11-25 21:46:34 +00:00
/* translators: %s: email address */
2017-09-13 07:31:59 +00:00
$errors -> add ( 'validation' , sprintf ( __ ( '%s is not a valid email address.' , 'woocommerce' ), '<strong>' . esc_html ( $field_label ) . '</strong>' ) );
2017-04-20 13:45:04 +00:00
continue ;
2012-07-16 19:21:44 +00:00
}
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( '' !== $data [ $key ] && in_array ( 'state' , $format ) ) {
$country = isset ( $data [ $fieldset_key . '_country' ] ) ? $data [ $fieldset_key . '_country' ] : WC () -> customer -> { " get_ { $fieldset_key } _country " }();
$valid_states = WC () -> countries -> get_states ( $country );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( ! empty ( $valid_states ) && is_array ( $valid_states ) && sizeof ( $valid_states ) > 0 ) {
2017-07-05 15:42:55 +00:00
$valid_state_values = array_map ( 'wc_strtoupper' , array_flip ( array_map ( 'wc_strtoupper' , $valid_states ) ) );
2017-05-25 10:30:01 +00:00
$data [ $key ] = wc_strtoupper ( $data [ $key ] );
2012-08-10 11:15:32 +00:00
2017-05-24 18:10:51 +00:00
if ( isset ( $valid_state_values [ $data [ $key ] ] ) ) {
2017-05-24 12:09:09 +00:00
// With this part we consider state value to be valid as well, convert it to the state key for the valid_states check below.
2017-05-24 18:10:51 +00:00
$data [ $key ] = $valid_state_values [ $data [ $key ] ];
2016-11-25 21:46:34 +00:00
}
2012-08-10 11:15:32 +00:00
2017-07-05 16:13:20 +00:00
if ( ! in_array ( $data [ $key ], $valid_state_values ) ) {
2016-11-25 21:46:34 +00:00
/* translators: 1: state field 2: valid states */
2017-09-13 07:31:59 +00:00
$errors -> add ( 'validation' , sprintf ( __ ( '%1$s is not valid. Please enter one of the following: %2$s' , 'woocommerce' ), '<strong>' . esc_html ( $field_label ) . '</strong>' , implode ( ', ' , $valid_states ) ) );
2016-11-25 21:46:34 +00:00
}
}
2014-10-31 15:03:53 +00:00
}
2016-11-25 21:46:34 +00:00
if ( $required && '' === $data [ $key ] ) {
/* translators: %s: field name */
2017-09-13 07:31:59 +00:00
$errors -> add ( 'required-field' , apply_filters ( 'woocommerce_checkout_required_field_notice' , sprintf ( __ ( '%s is a required field.' , 'woocommerce' ), '<strong>' . esc_html ( $field_label ) . '</strong>' ), $field_label ) );
2014-10-31 15:03:53 +00:00
}
2014-09-22 12:23:28 +00:00
}
2016-11-25 21:46:34 +00:00
}
}
2013-08-02 15:54:28 +00:00
2016-11-25 21:46:34 +00:00
/**
* Validates that the checkout has enough info to proceed .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-25 21:46:34 +00:00
* @ param array $data An array of posted data .
* @ param WP_Error $errors
*/
protected function validate_checkout ( & $data , & $errors ) {
2017-02-09 12:59:13 +00:00
$this -> validate_posted_data ( $data , $errors );
$this -> check_cart_items ();
2016-11-25 21:46:34 +00:00
if ( empty ( $data [ 'woocommerce_checkout_update_totals' ] ) && empty ( $data [ 'terms' ] ) && apply_filters ( 'woocommerce_checkout_show_terms' , wc_get_page_id ( 'terms' ) > 0 ) ) {
$errors -> add ( 'terms' , __ ( 'You must accept our Terms & Conditions.' , 'woocommerce' ) );
}
2016-03-09 20:49:02 +00:00
2016-11-25 21:46:34 +00:00
if ( WC () -> cart -> needs_shipping () ) {
$shipping_country = WC () -> customer -> get_shipping_country ();
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( empty ( $shipping_country ) ) {
$errors -> add ( 'shipping' , __ ( 'Please enter an address to continue.' , 'woocommerce' ) );
} elseif ( ! in_array ( WC () -> customer -> get_shipping_country (), array_keys ( WC () -> countries -> get_shipping_countries () ) ) ) {
$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 () ) );
2017-02-09 12:59:13 +00:00
} else {
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
2012-08-10 11:15:32 +00:00
2017-02-09 12:59:13 +00:00
foreach ( WC () -> shipping -> get_packages () as $i => $package ) {
if ( ! isset ( $chosen_shipping_methods [ $i ], $package [ 'rates' ][ $chosen_shipping_methods [ $i ] ] ) ) {
$errors -> add ( 'shipping' , __ ( 'No shipping method has been selected. Please double check your address, or contact us if you need any help.' , 'woocommerce' ) );
}
2014-10-31 15:03:53 +00:00
}
2012-12-12 21:14:19 +00:00
}
2016-11-25 21:46:34 +00:00
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( WC () -> cart -> needs_payment () ) {
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( ! isset ( $available_gateways [ $data [ 'payment_method' ] ] ) ) {
$errors -> add ( 'payment' , __ ( 'Invalid payment method.' , 'woocommerce' ) );
2015-02-03 15:27:40 +00:00
} else {
2016-11-25 21:46:34 +00:00
$available_gateways [ $data [ 'payment_method' ] ] -> validate_fields ();
2014-10-31 15:03:53 +00:00
}
2016-11-25 21:46:34 +00:00
}
2017-02-09 12:59:13 +00:00
do_action ( 'woocommerce_after_checkout_validation' , $data , $errors );
2016-11-25 21:46:34 +00:00
}
2014-10-31 15:03:53 +00:00
2017-05-12 09:43:14 +00:00
/**
* Set address field for customer .
*
* @ since 3.0 . 7
* @ param $field string to update
* @ param $key
* @ param $data array of data to get the value from
*/
protected function set_customer_address_fields ( $field , $key , $data ) {
if ( isset ( $data [ " billing_ { $field } " ] ) ) {
WC () -> customer -> { " set_billing_ { $field } " }( $data [ " billing_ { $field } " ] );
WC () -> customer -> { " set_shipping_ { $field } " }( $data [ " billing_ { $field } " ] );
}
if ( isset ( $data [ " shipping_ { $field } " ] ) ) {
WC () -> customer -> { " set_shipping_ { $field } " }( $data [ " shipping_ { $field } " ] );
}
}
2016-11-25 21:46:34 +00:00
/**
* Update customer and session data from the posted checkout data .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-25 21:46:34 +00:00
* @ param array $data
*/
protected function update_session ( $data ) {
2017-05-12 09:43:14 +00:00
// Update both shipping and billing to the passed billing address first if set.
$address_fields = array (
'address_1' ,
'address_2' ,
'city' ,
'postcode' ,
'state' ,
'country' ,
);
2017-05-12 11:09:05 +00:00
array_walk ( $address_fields , array ( $this , 'set_customer_address_fields' ), $data );
2016-11-25 21:46:34 +00:00
WC () -> customer -> save ();
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
// Update customer shipping and payment method to posted method
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( is_array ( $data [ 'shipping_method' ] ) ) {
foreach ( $data [ 'shipping_method' ] as $i => $value ) {
$chosen_shipping_methods [ $i ] = $value ;
}
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
WC () -> session -> set ( 'chosen_payment_method' , $data [ 'payment_method' ] );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
// Update cart totals now we have customer address.
WC () -> cart -> calculate_totals ();
}
2012-12-28 18:45:06 +00:00
2013-06-05 11:07:23 +00:00
2016-11-25 21:46:34 +00:00
/**
* Process an order that does require payment .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-25 21:46:34 +00:00
* @ param int $order_id
* @ param string $payment_method
*/
2016-12-02 16:13:36 +00:00
protected function process_order_payment ( $order_id , $payment_method ) {
2016-11-25 21:46:34 +00:00
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
2014-07-19 04:08:02 +00:00
2016-11-25 21:46:34 +00:00
if ( ! isset ( $available_gateways [ $payment_method ] ) ) {
return ;
}
2013-09-02 16:43:53 +00:00
2016-11-25 21:46:34 +00:00
// Store Order ID in session so it can be re-used after payment failure
WC () -> session -> set ( 'order_awaiting_payment' , $order_id );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
// Process Payment
$result = $available_gateways [ $payment_method ] -> process_payment ( $order_id );
2013-09-04 10:26:19 +00:00
2016-11-25 21:46:34 +00:00
// Redirect to success/confirmation/payment page
if ( isset ( $result [ 'result' ] ) && 'success' === $result [ 'result' ] ) {
$result = apply_filters ( 'woocommerce_payment_successful_result' , $result , $order_id );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( is_ajax () ) {
wp_send_json ( $result );
} else {
wp_redirect ( $result [ 'redirect' ] );
exit ;
}
}
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Process an order that doesn ' t require payment .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-25 21:46:34 +00:00
* @ param int $order_id
*/
2016-12-02 16:13:36 +00:00
protected function process_order_without_payment ( $order_id ) {
2016-11-25 21:46:34 +00:00
$order = wc_get_order ( $order_id );
$order -> payment_complete ();
wc_empty_cart ();
2014-06-18 15:03:46 +00:00
2016-11-25 21:46:34 +00:00
if ( is_ajax () ) {
wp_send_json ( array (
2016-12-02 16:13:36 +00:00
'result' => 'success' ,
'redirect' => apply_filters ( 'woocommerce_checkout_no_payment_needed_redirect' , $order -> get_checkout_order_received_url (), $order ),
2016-11-25 21:46:34 +00:00
) );
} else {
wp_safe_redirect (
apply_filters ( 'woocommerce_checkout_no_payment_needed_redirect' , $order -> get_checkout_order_received_url (), $order )
);
exit ;
}
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Create a new customer account if needed .
* @ param array $data
* @ throws Exception
*/
protected function process_customer ( $data ) {
2017-04-24 13:41:23 +00:00
$customer_id = apply_filters ( 'woocommerce_checkout_customer_id' , get_current_user_id () );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
if ( ! is_user_logged_in () && ( $this -> is_registration_required () || ! empty ( $data [ 'createaccount' ] ) ) ) {
$username = ! empty ( $data [ 'account_username' ] ) ? $data [ 'account_username' ] : '' ;
$password = ! empty ( $data [ 'account_password' ] ) ? $data [ 'account_password' ] : '' ;
$customer_id = wc_create_new_customer ( $data [ 'billing_email' ], $username , $password );
2012-01-10 16:43:06 +00:00
2016-11-25 21:46:34 +00:00
if ( is_wp_error ( $customer_id ) ) {
throw new Exception ( $customer_id -> get_error_message () );
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
wp_set_current_user ( $customer_id );
wc_set_customer_auth_cookie ( $customer_id );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
// As we are now logged in, checkout will need to refresh to show logged in data
WC () -> session -> set ( 'reload_checkout' , true );
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
// Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering
WC () -> cart -> calculate_totals ();
}
2012-08-10 11:15:32 +00:00
2017-04-27 12:48:45 +00:00
// On multisite, ensure user exists on current site, if not add them before allowing login.
if ( $customer_id && is_multisite () && is_user_logged_in () && ! is_user_member_of_blog () ) {
add_user_to_blog ( get_current_blog_id (), $customer_id , 'customer' );
}
2016-11-25 21:46:34 +00:00
// Add customer info from other fields.
if ( $customer_id && apply_filters ( 'woocommerce_checkout_update_customer_data' , true , $this ) ) {
$customer = new WC_Customer ( $customer_id );
2017-02-21 20:50:52 +00:00
if ( ! empty ( $data [ 'billing_first_name' ] ) ) {
$customer -> set_first_name ( $data [ 'billing_first_name' ] );
}
if ( ! empty ( $data [ 'billing_last_name' ] ) ) {
$customer -> set_last_name ( $data [ 'billing_last_name' ] );
}
2012-08-10 11:15:32 +00:00
2017-05-23 16:31:38 +00:00
// If the display name is an email, update to the user's full name.
2017-05-24 18:14:29 +00:00
if ( is_email ( $customer -> get_display_name () ) ) {
2017-05-23 16:31:38 +00:00
$customer -> set_display_name ( $data [ 'billing_first_name' ] . ' ' . $data [ 'billing_last_name' ] );
}
2016-11-25 21:46:34 +00:00
foreach ( $data as $key => $value ) {
2016-12-19 14:51:56 +00:00
// Use setters where available.
2016-11-25 21:46:34 +00:00
if ( is_callable ( array ( $customer , " set_ { $key } " ) ) ) {
$customer -> { " set_ { $key } " }( $value );
2016-12-19 14:51:56 +00:00
// Store custom fields prefixed with wither shipping_ or billing_.
} elseif ( 0 === stripos ( $key , 'billing_' ) || 0 === stripos ( $key , 'shipping_' ) ) {
$customer -> update_meta_data ( $key , $value );
2012-09-07 17:26:13 +00:00
}
}
2017-01-10 14:09:50 +00:00
/**
* Action hook to adjust customer before save .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-01-10 14:09:50 +00:00
*/
do_action ( 'woocommerce_checkout_update_customer' , $customer , $data );
2016-11-25 21:46:34 +00:00
$customer -> save ();
2014-10-31 15:03:53 +00:00
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
do_action ( 'woocommerce_checkout_update_user_meta' , $customer_id , $data );
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* If checkout failed during an AJAX call , send failure response .
*/
protected function send_ajax_failure_response () {
if ( is_ajax () ) {
2014-07-04 21:22:58 +00:00
// only print notices if not reloading the checkout, otherwise they're lost in the page reload
if ( ! isset ( WC () -> session -> reload_checkout ) ) {
ob_start ();
wc_print_notices ();
$messages = ob_get_clean ();
}
2015-05-15 12:51:51 +00:00
$response = array (
2016-11-25 21:46:34 +00:00
'result' => 'failure' ,
'messages' => isset ( $messages ) ? $messages : '' ,
'refresh' => isset ( WC () -> session -> refresh_totals ),
'reload' => isset ( WC () -> session -> reload_checkout ),
2015-05-15 12:51:51 +00:00
);
2012-08-10 11:15:32 +00:00
2013-10-24 12:15:42 +00:00
unset ( WC () -> session -> refresh_totals , WC () -> session -> reload_checkout );
2015-05-15 12:51:51 +00:00
wp_send_json ( $response );
2012-09-07 17:26:13 +00:00
}
2011-08-09 15:16:18 +00:00
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Process the checkout after the confirm order button is pressed .
*/
public function process_checkout () {
try {
if ( empty ( $_POST [ '_wpnonce' ] ) || ! wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-process_checkout' ) ) {
WC () -> session -> set ( 'refresh_totals' , true );
throw new Exception ( __ ( 'We were unable to process your order, please try again.' , 'woocommerce' ) );
}
wc_maybe_define_constant ( 'WOOCOMMERCE_CHECKOUT' , true );
wc_set_time_limit ( 0 );
do_action ( 'woocommerce_before_checkout_process' );
if ( WC () -> cart -> is_empty () ) {
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' ) ) ) );
}
do_action ( 'woocommerce_checkout_process' );
$errors = new WP_Error ();
$posted_data = $this -> get_posted_data ();
2017-02-09 12:59:13 +00:00
// Update session for customer and totals.
2016-11-25 21:46:34 +00:00
$this -> update_session ( $posted_data );
2017-02-09 12:59:13 +00:00
// Validate posted data and cart items before proceeding.
$this -> validate_checkout ( $posted_data , $errors );
2016-11-25 21:46:34 +00:00
foreach ( $errors -> get_error_messages () as $message ) {
wc_add_notice ( $message , 'error' );
}
if ( empty ( $posted_data [ 'woocommerce_checkout_update_totals' ] ) && 0 === wc_notice_count ( 'error' ) ) {
$this -> process_customer ( $posted_data );
2017-02-15 17:07:03 +00:00
$order_id = $this -> create_order ( $posted_data );
$order = wc_get_order ( $order_id );
2016-11-25 21:46:34 +00:00
2017-02-15 17:07:03 +00:00
if ( is_wp_error ( $order_id ) ) {
throw new Exception ( $order_id -> get_error_message () );
2016-11-25 21:46:34 +00:00
}
2017-02-15 17:07:03 +00:00
do_action ( 'woocommerce_checkout_order_processed' , $order_id , $posted_data , $order );
2016-11-25 21:46:34 +00:00
if ( WC () -> cart -> needs_payment () ) {
2017-02-15 17:07:03 +00:00
$this -> process_order_payment ( $order_id , $posted_data [ 'payment_method' ] );
2016-11-25 21:46:34 +00:00
} else {
2017-02-15 17:07:03 +00:00
$this -> process_order_without_payment ( $order_id );
2016-11-25 21:46:34 +00:00
}
}
} catch ( Exception $e ) {
wc_add_notice ( $e -> getMessage (), 'error' );
}
$this -> send_ajax_failure_response ();
}
2014-06-11 14:10:03 +00:00
/**
* Get a posted address field after sanitization and validation .
2016-11-25 21:46:34 +00:00
*
2014-06-11 14:10:03 +00:00
* @ param string $key
* @ param string $type billing for shipping
* @ return string
*/
public function get_posted_address_data ( $key , $type = 'billing' ) {
2017-04-18 20:55:31 +00:00
if ( 'billing' === $type || false === $this -> legacy_posted_data [ 'ship_to_different_address' ] ) {
2016-12-19 15:42:53 +00:00
$return = isset ( $this -> legacy_posted_data [ 'billing_' . $key ] ) ? $this -> legacy_posted_data [ 'billing_' . $key ] : '' ;
2014-06-11 14:10:03 +00:00
} else {
2016-12-19 15:42:53 +00:00
$return = isset ( $this -> legacy_posted_data [ 'shipping_' . $key ] ) ? $this -> legacy_posted_data [ 'shipping_' . $key ] : '' ;
2014-06-11 14:10:03 +00:00
}
return $return ;
}
2012-08-14 19:42:38 +00:00
/**
2015-11-03 13:31:20 +00:00
* Gets the value either from the posted data , or from the users meta data .
2012-08-14 19:42:38 +00:00
*
* @ param string $input
2016-12-19 14:51:56 +00:00
* @ return string
2012-08-14 19:42:38 +00:00
*/
2012-12-14 21:27:29 +00:00
public function get_value ( $input ) {
2012-10-19 17:59:17 +00:00
if ( ! empty ( $_POST [ $input ] ) ) {
2013-11-25 13:34:21 +00:00
return wc_clean ( $_POST [ $input ] );
2012-08-10 11:15:32 +00:00
2013-01-07 15:58:19 +00:00
} else {
2017-01-26 22:35:31 +00:00
$value = apply_filters ( 'woocommerce_checkout_get_value' , null , $input );
2017-02-17 18:10:15 +00:00
if ( null !== $value ) {
2017-01-26 22:35:31 +00:00
return $value ;
2014-09-22 12:23:28 +00:00
}
2013-09-10 14:23:26 +00:00
2016-11-25 21:46:34 +00:00
if ( is_callable ( array ( WC () -> customer , " get_ $input " ) ) ) {
2017-03-28 11:30:04 +00:00
$value = WC () -> customer -> { " get_ $input " }() ? WC () -> customer -> { " get_ $input " }() : null ;
} elseif ( WC () -> customer -> meta_exists ( $input ) ) {
2016-12-19 14:51:56 +00:00
$value = WC () -> customer -> get_meta ( $input , true );
2013-01-07 15:58:19 +00:00
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
return apply_filters ( 'default_checkout_' . $input , $value , $input );
2012-10-19 17:59:17 +00:00
}
2011-08-09 15:16:18 +00:00
}
2013-01-22 21:30:14 +00:00
}