2011-08-09 15:16:18 +00:00
< ? php
/**
2018-03-16 19:15:08 +00:00
* Checkout functionality
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
*
2018-03-16 19:15:08 +00:00
* @ package WooCommerce / Classes
* @ version 3.4 . 0
*/
defined ( 'ABSPATH' ) || exit ;
/**
* Checkout class .
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 .
2018-03-16 19:15:08 +00:00
*
2017-04-18 20:55:31 +00:00
* @ 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 .
*
2018-03-16 19:15:08 +00:00
* @ param string $key Key .
2016-11-25 21:46:34 +00:00
* @ return bool
2013-09-12 13:41:02 +00:00
*/
2016-11-25 21:46:34 +00:00
public function __isset ( $key ) {
2018-03-16 19:15:08 +00:00
return in_array (
$key ,
array (
'enable_signup' ,
'enable_guest_checkout' ,
'must_create_account' ,
'checkout_fields' ,
'posted' ,
'shipping_method' ,
'payment_method' ,
'customer_id' ,
'shipping_methods' ,
),
true
);
2016-11-25 21:46:34 +00:00
}
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
*
2018-03-16 19:15:08 +00:00
* @ param string $key Key .
* @ param mixed $value Value .
2016-11-25 21:46:34 +00:00
*/
public function __set ( $key , $value ) {
switch ( $key ) {
2018-03-16 19:15:08 +00:00
case 'enable_signup' :
2016-11-25 21:46:34 +00:00
$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 ;
2018-03-16 19:15:08 +00:00
case 'enable_guest_checkout' :
2016-11-25 21:46:34 +00:00
$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 ;
2018-03-16 19:15:08 +00:00
case 'checkout_fields' :
2016-11-25 21:46:34 +00:00
$this -> fields = $value ;
break ;
2018-03-16 19:15:08 +00:00
case 'shipping_methods' :
2016-11-25 21:46:34 +00:00
WC () -> session -> set ( 'chosen_shipping_methods' , $value );
break ;
2018-03-16 19:15:08 +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
*
2018-03-16 19:15:08 +00:00
* @ param string $key 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 ) {
2018-03-16 19:15:08 +00:00
if ( in_array ( $key , array ( 'posted' , 'shipping_method' , 'payment_method' ), true ) && empty ( $this -> legacy_posted_data ) ) {
2016-12-02 16:13:36 +00:00
$this -> legacy_posted_data = $this -> get_posted_data ();
}
2018-04-26 03:11:36 +00:00
2016-11-25 21:46:34 +00:00
switch ( $key ) {
2018-03-16 19:15:08 +00:00
case 'enable_signup' :
2016-11-25 21:46:34 +00:00
return $this -> is_registration_enabled ();
2018-03-16 19:15:08 +00:00
case 'enable_guest_checkout' :
2016-11-25 21:46:34 +00:00
return ! $this -> is_registration_required ();
2018-03-16 19:15:08 +00:00
case 'must_create_account' :
2016-11-25 21:46:34 +00:00
return $this -> is_registration_required () && ! is_user_logged_in ();
2018-03-16 19:15:08 +00:00
case 'checkout_fields' :
2016-11-25 21:46:34 +00:00
return $this -> get_checkout_fields ();
2018-03-16 19:15:08 +00:00
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 ;
2018-03-16 19:15:08 +00:00
case 'shipping_method' :
2016-11-25 21:46:34 +00:00
return $this -> legacy_posted_data [ 'shipping_method' ];
2018-03-16 19:15:08 +00:00
case 'payment_method' :
2016-11-25 21:46:34 +00:00
return $this -> legacy_posted_data [ 'payment_method' ];
2018-03-16 19:15:08 +00:00
case 'customer_id' :
2016-11-25 21:46:34 +00:00
return apply_filters ( 'woocommerce_checkout_customer_id' , get_current_user_id () );
2018-03-16 19:15:08 +00:00
case 'shipping_methods' :
2016-11-25 21:46:34 +00:00
return ( array ) WC () -> session -> get ( 'chosen_shipping_methods' );
}
2013-09-12 13:41:02 +00:00
}
/**
* Cloning is forbidden .
*/
public function __clone () {
2018-02-07 22:01:12 +00:00
wc_doing_it_wrong ( __FUNCTION__ , __ ( 'Cloning is forbidden.' , 'woocommerce' ), '2.1' );
2013-09-12 13:41:02 +00:00
}
/**
* Unserializing instances of this class is forbidden .
*/
public function __wakeup () {
2018-02-07 22:01:12 +00:00
wc_doing_it_wrong ( __FUNCTION__ , __ ( 'Unserializing instances of this class is forbidden.' , '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 = '' ) {
2018-04-26 03:03:51 +00:00
if ( ! is_null ( $this -> fields ) ) {
return $fieldset ? $this -> fields [ $fieldset ] : $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'
2016-11-25 21:46:34 +00:00
),
),
2018-04-26 03:03:51 +00:00
),
);
2012-12-10 12:34:55 +00:00
2018-04-26 03:03:51 +00:00
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' ),
);
2016-11-25 21:46:34 +00:00
}
2018-04-26 03:03:51 +00:00
if ( 'no' === get_option ( 'woocommerce_registration_generate_password' ) ) {
$this -> fields [ 'account' ][ 'account_password' ] = array (
'type' => 'password' ,
'label' => __ ( 'Create account password' , 'woocommerce' ),
'required' => true ,
'placeholder' => esc_attr__ ( 'Password' , 'woocommerce' ),
);
2016-11-25 21:46:34 +00:00
}
2018-04-26 03:03:51 +00:00
$this -> fields = apply_filters ( 'woocommerce_checkout_fields' , $this -> fields );
return $fieldset ? $this -> fields [ $fieldset ] : $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 :
2018-03-16 19:15:08 +00:00
* 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
*
2018-03-16 19:15:08 +00:00
* @ throws Exception When checkout validation fails .
* @ param array $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.
2018-03-16 19:15:08 +00:00
$order_id = apply_filters ( 'woocommerce_create_order' , null , $this );
if ( $order_id ) {
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' ) );
2018-03-16 19:15:08 +00:00
$cart_hash = md5 ( wp_json_encode ( wc_clean ( WC () -> cart -> get_cart_for_session () ) ) . WC () -> cart -> total );
2017-02-07 14:51:21 +00:00
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
2018-03-16 19:15:08 +00:00
$order = $order_id ? wc_get_order ( $order_id ) : null ;
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 .
*/
2018-03-16 19:15:08 +00:00
if ( $order && $order -> has_cart_hash ( $cart_hash ) && $order -> has_status ( array ( 'pending' , 'failed' ) ) ) {
2016-08-09 13:02:40 +00:00
// 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
2018-03-16 19:15:08 +00:00
// Store custom fields prefixed with wither shipping_ or billing_. This is for backwards compatibility with 2.6.x.
// TODO: Fix conditional to only include shipping/billing address fields in a smarter way without str(i)pos.
2017-09-05 17:56:56 +00:00
} elseif ( ( 0 === stripos ( $key , 'billing_' ) || 0 === stripos ( $key , 'shipping_' ) )
2018-03-16 19:15:08 +00:00
&& ! in_array ( $key , array ( 'shipping_method' , 'shipping_total' , 'shipping_tax' ), true ) ) {
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' ] : '' );
2018-03-16 19:15:08 +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 .
2018-03-16 19:15:08 +00:00
*
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 .
*
2018-03-16 19:15:08 +00:00
* @ param WC_Order $order Order instance .
* @ param WC_Cart $cart Cart instance .
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 .
2018-03-16 19:15:08 +00:00
*
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.
2018-03-16 19:15:08 +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' ],
)
);
2018-04-26 03:11:36 +00:00
2017-02-01 01:11:56 +00:00
if ( $product ) {
2018-03-16 19:15:08 +00:00
$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 ,
)
);
2017-02-01 01:11:56 +00:00
}
2018-04-26 03:11:36 +00:00
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 .
2018-03-16 19:15:08 +00:00
*
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 .
*
2018-03-16 19:15:08 +00:00
* @ param WC_Order $order Order instance .
* @ param WC_Cart $cart Cart instance .
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.
2018-03-16 19:15:08 +00:00
$item -> set_props (
array (
'name' => $fee -> name ,
'tax_class' => $fee -> taxable ? $fee -> tax_class : 0 ,
'amount' => $fee -> amount ,
'total' => $fee -> total ,
'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 .
2018-03-16 19:15:08 +00:00
*
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 .
*
2018-03-16 19:15:08 +00:00
* @ param WC_Order $order Order Instance .
* @ param array $chosen_shipping_methods Chosen shipping methods .
* @ param array $packages 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 ] ] ) ) {
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.
2018-03-16 19:15:08 +00:00
$item -> set_props (
array (
'method_title' => $shipping_rate -> label ,
'method_id' => $shipping_rate -> method_id ,
'instance_id' => $shipping_rate -> instance_id ,
'total' => wc_format_decimal ( $shipping_rate -> cost ),
'taxes' => array (
'total' => $shipping_rate -> taxes ,
),
)
);
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 .
2018-03-16 19:15:08 +00:00
*
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 .
*
2018-03-16 19:15:08 +00:00
* @ param WC_Order $order Order instance .
* @ param WC_Cart $cart Cart instance .
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 ();
2018-03-16 19:15:08 +00:00
$item -> set_props (
array (
'rate_id' => $tax_rate_id ,
'tax_total' => $cart -> get_tax_amount ( $tax_rate_id ),
'shipping_tax_total' => $cart -> get_shipping_tax_amount ( $tax_rate_id ),
'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-12-19 11:58:00 +00:00
/**
2016-12-22 15:06:10 +00:00
* Action hook to adjust item before save .
2018-03-16 19:15:08 +00:00
*
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 .
*
2018-03-16 19:15:08 +00:00
* @ param WC_Order $order Order instance .
* @ param WC_Cart $cart Cart instance .
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 ();
2018-03-16 19:15:08 +00:00
$item -> set_props (
array (
'code' => $code ,
'discount' => $cart -> get_coupon_discount_amount ( $code ),
'discount_tax' => $cart -> get_coupon_discount_tax_amount ( $code ),
)
);
2018-04-10 11:36:24 +00:00
// Avoid storing used_by - it's not needed and can get large.
$coupon_data = $coupon -> get_data ();
unset ( $coupon_data [ 'used_by' ] );
$item -> add_meta_data ( 'coupon_data' , $coupon_data );
2016-12-19 11:58:00 +00:00
/**
2016-12-22 15:06:10 +00:00
* Action hook to adjust item before save .
2018-03-16 19:15:08 +00:00
*
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
2018-03-16 19:15:08 +00:00
* @ param string $fieldset_key Fieldset key .
* @ param array $data Posted 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 ;
}
2018-04-26 03:11:36 +00:00
2016-11-25 21:46:34 +00:00
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
}
2018-04-26 03:11:36 +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 (
2018-03-16 19:15:08 +00:00
'terms' => ( int ) isset ( $_POST [ 'terms' ] ), // WPCS: input var ok, CSRF ok.
'createaccount' => ( int ) ! empty ( $_POST [ 'createaccount' ] ), // WPCS: input var ok, CSRF ok.
'payment_method' => isset ( $_POST [ 'payment_method' ] ) ? wc_clean ( wp_unslash ( $_POST [ 'payment_method' ] ) ) : '' , // WPCS: input var ok, CSRF ok.
'shipping_method' => isset ( $_POST [ 'shipping_method' ] ) ? wc_clean ( wp_unslash ( $_POST [ 'shipping_method' ] ) ) : '' , // WPCS: input var ok, CSRF ok.
'ship_to_different_address' => ! empty ( $_POST [ 'ship_to_different_address' ] ) && ! wc_ship_to_billing_address_only (), // WPCS: input var ok, CSRF ok.
'woocommerce_checkout_update_totals' => isset ( $_POST [ 'woocommerce_checkout_update_totals' ] ), // WPCS: input var ok, CSRF ok.
2016-11-25 21:46:34 +00:00
);
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
}
2018-04-26 03:11:36 +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 ) {
2018-03-16 19:15:08 +00:00
case 'checkbox' :
$value = isset ( $_POST [ $key ] ) ? 1 : '' ; // WPCS: input var ok, CSRF ok.
2016-11-25 21:46:34 +00:00
break ;
2018-03-16 19:15:08 +00:00
case 'multiselect' :
$value = isset ( $_POST [ $key ] ) ? implode ( ', ' , wc_clean ( wp_unslash ( $_POST [ $key ] ) ) ) : '' ; // WPCS: input var ok, CSRF ok.
2016-11-25 21:46:34 +00:00
break ;
2018-03-16 19:15:08 +00:00
case 'textarea' :
$value = isset ( $_POST [ $key ] ) ? wc_sanitize_textarea ( wp_unslash ( $_POST [ $key ] ) ) : '' ; // WPCS: input var ok, CSRF ok.
2016-11-25 21:46:34 +00:00
break ;
2018-03-16 19:15:08 +00:00
default :
$value = isset ( $_POST [ $key ] ) ? wc_clean ( wp_unslash ( $_POST [ $key ] ) ) : '' ; // WPCS: input var ok, CSRF ok.
2016-11-25 21:46:34 +00:00
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
2018-03-16 19:15:08 +00:00
if ( in_array ( 'shipping' , $skipped , true ) && ( 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
2018-03-16 19:15:08 +00:00
* @ param array $data An array of posted data .
* @ param WP_Error $errors Validation error .
2016-11-25 21:46:34 +00:00
*/
protected function validate_posted_data ( & $data , & $errors ) {
foreach ( $this -> get_checkout_fields () as $fieldset_key => $fieldset ) {
2018-06-01 15:28:18 +00:00
$validate_fieldset = true ;
2016-11-25 21:46:34 +00:00
if ( $this -> maybe_skip_fieldset ( $fieldset_key , $data ) ) {
2018-06-01 15:28:18 +00:00
$validate_fieldset = false ;
2014-05-01 13:40:00 +00:00
}
2018-04-26 03:11:36 +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 ) {
2018-03-16 19:15:08 +00:00
case 'shipping' :
2016-11-25 21:46:34 +00:00
/* translators: %s: field name */
2017-04-26 10:40:59 +00:00
$field_label = sprintf ( __ ( 'Shipping %s' , 'woocommerce' ), $field_label );
2018-03-16 19:15:08 +00:00
break ;
case 'billing' :
2016-11-25 21:46:34 +00:00
/* translators: %s: field name */
2017-04-26 10:40:59 +00:00
$field_label = sprintf ( __ ( 'Billing %s' , 'woocommerce' ), $field_label );
2018-03-16 19:15:08 +00:00
break ;
2014-10-31 15:03:53 +00:00
}
2013-07-19 05:58:45 +00:00
2018-03-16 19:15:08 +00:00
if ( in_array ( 'postcode' , $format , true ) ) {
2016-11-25 21:46:34 +00:00
$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
2018-06-01 15:28:18 +00:00
if ( $validate_fieldset && '' !== $data [ $key ] && ! WC_Validation :: is_postcode ( $data [ $key ], $country ) ) {
2018-05-03 10:26:01 +00:00
switch ( $country ) {
case 'IE' :
/* translators: %1$s: field name, %2$s finder.eircode.ie URL */
2018-05-31 19:40:58 +00:00
$postcode_validation_notice = sprintf ( __ ( '%1$s is not valid. You can look up the correct Eircode <a target="_blank" href="%2$s">here</a>.' , 'woocommerce' ), '<strong>' . esc_html ( $field_label ) . '</strong>' , 'https://finder.eircode.ie' );
2018-05-03 10:26:01 +00:00
break ;
default :
/* translators: %s: field name */
$postcode_validation_notice = sprintf ( __ ( '%s is not a valid postcode / ZIP.' , 'woocommerce' ), '<strong>' . esc_html ( $field_label ) . '</strong>' );
}
$errors -> add ( 'validation' , apply_filters ( 'woocommerce_checkout_postcode_validation_notice' , $postcode_validation_notice , $country , $data [ $key ] ) );
2014-10-31 15:03:53 +00:00
}
2016-11-25 21:46:34 +00:00
}
2012-08-10 11:15:32 +00:00
2018-03-16 19:15:08 +00:00
if ( in_array ( 'phone' , $format , true ) ) {
2016-11-25 21:46:34 +00:00
$data [ $key ] = wc_format_phone_number ( $data [ $key ] );
2012-08-10 11:15:32 +00:00
2018-06-01 15:28:18 +00:00
if ( $validate_fieldset && '' !== $data [ $key ] && ! WC_Validation :: is_phone ( $data [ $key ] ) ) {
2016-11-25 21:46:34 +00:00
/* 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
2018-03-16 19:15:08 +00:00
if ( in_array ( 'email' , $format , true ) && '' !== $data [ $key ] ) {
2016-11-25 21:46:34 +00:00
$data [ $key ] = sanitize_email ( $data [ $key ] );
2018-06-01 15:28:18 +00:00
if ( $validate_fieldset && ! 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
2018-03-16 19:15:08 +00:00
if ( '' !== $data [ $key ] && in_array ( 'state' , $format , true ) ) {
2016-11-25 21:46:34 +00:00
$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
2018-03-16 19:15:08 +00:00
if ( ! empty ( $valid_states ) && is_array ( $valid_states ) && count ( $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
2018-06-01 15:28:18 +00:00
if ( $validate_fieldset && ! in_array ( $data [ $key ], $valid_state_values , true ) ) {
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
2018-06-01 15:28:18 +00:00
if ( $validate_fieldset && $required && '' === $data [ $key ] ) {
2016-11-25 21:46:34 +00:00
/* 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
2018-03-16 19:15:08 +00:00
* @ param array $data An array of posted data .
* @ param WP_Error $errors Validation errors .
2016-11-25 21:46:34 +00:00
*/
protected function validate_checkout ( & $data , & $errors ) {
2017-02-09 12:59:13 +00:00
$this -> validate_posted_data ( $data , $errors );
$this -> check_cart_items ();
2018-05-22 13:10:09 +00:00
if ( empty ( $data [ 'woocommerce_checkout_update_totals' ] ) && empty ( $data [ 'terms' ] ) && ! empty ( $_POST [ 'terms-field' ] ) ) { // WPCS: input var ok, CSRF ok.
2018-04-06 10:43:30 +00:00
$errors -> add ( 'terms' , __ ( 'Please read and accept the terms and conditions to proceed with your order.' , 'woocommerce' ) );
2016-11-25 21:46:34 +00:00
}
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' ) );
2018-03-16 19:15:08 +00:00
} elseif ( ! in_array ( WC () -> customer -> get_shipping_country (), array_keys ( WC () -> countries -> get_shipping_countries () ), true ) ) {
/* translators: %s: shipping location */
2016-11-25 21:46:34 +00:00
$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
2018-03-16 19:15:08 +00:00
* @ param string $field String to update .
* @ param string $key Field key .
* @ param array $data Array of data to get the value from .
2017-05-12 09:43:14 +00:00
*/
protected function set_customer_address_fields ( $field , $key , $data ) {
2018-06-29 11:42:10 +00:00
$billing_value = null ;
$shipping_value = null ;
if ( isset ( $data [ " billing_ { $field } " ] ) && is_callable ( array ( WC () -> customer , " set_billing_ { $field } " ) ) ) {
$billing_value = $data [ " billing_ { $field } " ];
$shipping_value = $data [ " billing_ { $field } " ];
}
if ( isset ( $data [ " shipping_ { $field } " ] ) && is_callable ( array ( WC () -> customer , " set_shipping_ { $field } " ) ) ) {
$shipping_value = $data [ " shipping_ { $field } " ];
2017-05-12 09:43:14 +00:00
}
2018-04-26 03:11:36 +00:00
2018-06-29 11:42:10 +00:00
if ( ! is_null ( $billing_value ) && is_callable ( array ( WC () -> customer , " set_billing_ { $field } " ) ) ) {
WC () -> customer -> { " set_billing_ { $field } " }( $billing_value );
}
if ( ! is_null ( $shipping_value ) && is_callable ( array ( WC () -> customer , " set_shipping_ { $field } " ) ) ) {
WC () -> customer -> { " set_shipping_ { $field } " }( $shipping_value );
2017-05-12 09:43:14 +00:00
}
}
2016-11-25 21:46:34 +00:00
/**
* Update customer and session data from the posted checkout data .
*
2018-03-16 19:15:08 +00:00
* @ since 3.0 . 0
* @ param array $data Posted data .
2016-11-25 21:46:34 +00:00
*/
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 (
2018-06-29 11:42:10 +00:00
'first_name' ,
'last_name' ,
'company' ,
'email' ,
'phone' ,
2017-05-12 09:43:14 +00:00
'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
2018-03-16 19:15:08 +00:00
// Update customer shipping and payment method to posted method.
2016-11-25 21:46:34 +00:00
$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 .
*
2018-03-16 19:15:08 +00:00
* @ since 3.0 . 0
* @ param int $order_id Order ID .
* @ param string $payment_method Payment method .
2016-11-25 21:46:34 +00:00
*/
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
2018-03-16 19:15:08 +00:00
// Store Order ID in session so it can be re-used after payment failure.
2016-11-25 21:46:34 +00:00
WC () -> session -> set ( 'order_awaiting_payment' , $order_id );
2012-08-10 11:15:32 +00:00
2018-03-16 19:15:08 +00:00
// Process Payment.
2016-11-25 21:46:34 +00:00
$result = $available_gateways [ $payment_method ] -> process_payment ( $order_id );
2013-09-04 10:26:19 +00:00
2018-03-16 19:15:08 +00:00
// Redirect to success/confirmation/payment page.
2016-11-25 21:46:34 +00:00
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
2018-04-26 04:24:31 +00:00
if ( ! is_ajax () ) {
2016-11-25 21:46:34 +00:00
wp_redirect ( $result [ 'redirect' ] );
exit ;
}
2018-04-26 04:24:31 +00:00
wp_send_json ( $result );
2016-11-25 21:46:34 +00:00
}
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Process an order that doesn ' t require payment .
*
2018-03-16 19:15:08 +00:00
* @ since 3.0 . 0
* @ param int $order_id Order ID .
2016-11-25 21:46:34 +00:00
*/
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
2018-04-26 04:18:47 +00:00
if ( ! is_ajax () ) {
2016-11-25 21:46:34 +00:00
wp_safe_redirect (
apply_filters ( 'woocommerce_checkout_no_payment_needed_redirect' , $order -> get_checkout_order_received_url (), $order )
);
exit ;
}
2018-04-26 04:18:47 +00:00
wp_send_json (
array (
'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
}
2012-08-10 11:15:32 +00:00
2016-11-25 21:46:34 +00:00
/**
* Create a new customer account if needed .
2018-03-16 19:15:08 +00:00
*
* @ throws Exception When not able to create customer .
* @ param array $data Posted data .
2016-11-25 21:46:34 +00:00
*/
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
2018-03-16 19:15:08 +00:00
// As we are now logged in, checkout will need to refresh to show logged in data.
2016-11-25 21:46:34 +00:00
WC () -> session -> set ( 'reload_checkout' , true );
2012-08-10 11:15:32 +00:00
2018-03-16 19:15:08 +00:00
// Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering.
2016-11-25 21:46:34 +00:00
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
2018-03-16 19:15:08 +00:00
// Store custom fields prefixed with wither shipping_ or billing_.
2016-12-19 14:51:56 +00:00
} 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 .
2018-03-16 19:15:08 +00:00
*
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 () ) {
2018-03-16 19:15:08 +00:00
// Only print notices if not reloading the checkout, otherwise they're lost in the page reload.
2014-07-04 21:22:58 +00:00
if ( ! isset ( WC () -> session -> reload_checkout ) ) {
2018-04-19 17:27:29 +00:00
$messages = wc_print_notices ( true );
2014-07-04 21:22:58 +00:00
}
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 .
2018-03-16 19:15:08 +00:00
*
* @ throws Exception When validation fails .
2016-11-25 21:46:34 +00:00
*/
public function process_checkout () {
try {
2018-03-13 14:16:56 +00:00
$nonce_value = wc_get_var ( $_REQUEST [ 'woocommerce-process-checkout-nonce' ], wc_get_var ( $_REQUEST [ '_wpnonce' ], '' ) ); // @codingStandardsIgnoreLine.
if ( empty ( $nonce_value ) || ! wp_verify_nonce ( $nonce_value , 'woocommerce-process_checkout' ) ) {
2016-11-25 21:46:34 +00:00
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 () ) {
2018-03-16 19:15:08 +00:00
/* translators: %s: shop cart url */
2016-11-25 21:46:34 +00:00
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
}
2018-04-20 10:54:36 +00:00
if ( ! $order ) {
throw new Exception ( __ ( 'Unable to create order.' , 'woocommerce' ) );
}
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
*
2018-03-16 19:15:08 +00:00
* @ param string $key Field key .
* @ param string $type Type of address . Available options : 'billing' or 'shipping' .
2014-06-11 14:10:03 +00:00
* @ 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
*
2018-03-16 19:15:08 +00:00
* @ param string $input Input key .
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 ) {
2018-03-16 19:15:08 +00:00
if ( ! empty ( $_POST [ $input ] ) ) { // WPCS: input var ok, CSRF OK.
return wc_clean ( wp_unslash ( $_POST [ $input ] ) ); // WPCS: input var ok, CSRF OK.
2018-04-26 03:40:58 +00:00
}
2012-08-10 11:15:32 +00:00
2018-04-26 03:40:58 +00:00
$value = apply_filters ( 'woocommerce_checkout_get_value' , null , $input );
2013-09-10 14:23:26 +00:00
2018-04-26 03:40:58 +00:00
if ( null !== $value ) {
return $value ;
}
2012-08-10 11:15:32 +00:00
2018-04-26 03:40:58 +00:00
if ( is_callable ( array ( WC () -> customer , " get_ $input " ) ) ) {
2018-06-29 11:42:10 +00:00
$value = WC () -> customer -> { " get_ $input " }();
2018-04-26 03:40:58 +00:00
} elseif ( WC () -> customer -> meta_exists ( $input ) ) {
$value = WC () -> customer -> get_meta ( $input , true );
2012-10-19 17:59:17 +00:00
}
2018-04-26 03:40:58 +00:00
2018-06-29 11:42:10 +00:00
$value = $value ? $value : null ; // Empty value should return null.
2018-04-26 03:40:58 +00:00
return apply_filters ( 'default_checkout_' . $input , $value , $input );
2011-08-09 15:16:18 +00:00
}
2013-01-22 21:30:14 +00:00
}