2011-08-09 15:16:18 +00:00
< ? php
/**
* Checkout
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
*
2012-08-14 19:42:38 +00:00
* @ class WC_Cart
2013-09-12 13:41:02 +00:00
* @ version 2.1 . 0
2012-08-14 22:43:48 +00:00
* @ package WooCommerce / Classes
2013-02-20 17:14:46 +00:00
* @ category Class
2012-08-14 19:42:38 +00:00
* @ 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
2012-08-15 17:08:42 +00:00
/** @var array Array of posted form data. */
2012-12-14 21:27:29 +00:00
public $posted ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var array Array of fields to display on the checkout. */
2012-12-14 21:27:29 +00:00
public $checkout_fields ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var bool Whether or not the user must create an account to checkout. */
2012-12-14 21:27:29 +00:00
public $must_create_account ;
2012-08-14 19:42:38 +00:00
2012-12-10 12:34:55 +00:00
/** @var bool Whether or not signups are allowed. */
2012-12-14 21:27:29 +00:00
public $enable_signup ;
2012-12-10 12:34:55 +00:00
2012-12-27 19:24:33 +00:00
/** @var object The shipping method being used. */
private $shipping_method ;
2013-10-06 04:44:04 +00:00
/** @var WC_Payment_Gateway The payment gateway being used. */
2012-12-27 19:24:33 +00:00
private $payment_method ;
2013-02-15 21:18:46 +00:00
/** @var int ID of customer. */
private $customer_id ;
2013-09-12 13:41:02 +00:00
/**
* @ var WooCommerce The single instance of the class
* @ since 2.1
*/
protected static $_instance = null ;
/**
* Main WooCommerce Instance
*
* Ensures only one instance of WooCommerce is loaded or can be loaded .
*
* @ since 2.1
* @ static
* @ see WC ()
* @ return Main WooCommerce instance
*/
public static function instance () {
if ( is_null ( self :: $_instance ) )
self :: $_instance = new self ();
return self :: $_instance ;
}
/**
* Cloning is forbidden .
*
* @ since 2.1
*/
public function __clone () {
_doing_it_wrong ( __FUNCTION__ , __ ( 'Cheatin’ huh?' ), '2.1' );
}
/**
* Unserializing instances of this class is forbidden .
*
* @ since 2.1
*/
public function __wakeup () {
_doing_it_wrong ( __FUNCTION__ , __ ( 'Cheatin’ huh?' ), '2.1' );
}
2012-08-14 19:42:38 +00:00
/**
2013-03-03 17:07:31 +00:00
* Constructor for the checkout class . Hooks in methods and defines checkout fields .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
*/
2012-12-14 21:27:29 +00:00
public function __construct () {
2012-12-15 11:53:32 +00:00
add_action ( 'woocommerce_checkout_billing' , array ( $this , 'checkout_form_billing' ) );
add_action ( 'woocommerce_checkout_shipping' , array ( $this , 'checkout_form_shipping' ) );
2012-08-10 11:15:32 +00:00
2012-12-10 12:34:55 +00:00
$this -> enable_signup = get_option ( 'woocommerce_enable_signup_and_login_from_checkout' ) == 'yes' ? true : false ;
$this -> enable_guest_checkout = get_option ( 'woocommerce_enable_guest_checkout' ) == 'yes' ? true : false ;
$this -> must_create_account = $this -> enable_guest_checkout || is_user_logged_in () ? false : true ;
2011-12-21 16:03:45 +00:00
// Define all Checkout fields
2013-10-24 12:15:42 +00:00
$this -> checkout_fields [ 'billing' ] = WC () -> countries -> get_address_fields ( $this -> get_value ( 'billing_country' ), 'billing_' );
$this -> checkout_fields [ 'shipping' ] = WC () -> countries -> get_address_fields ( $this -> get_value ( 'shipping_country' ), 'shipping_' );
2012-08-10 11:15:32 +00:00
2013-06-03 15:09:04 +00:00
if ( get_option ( 'woocommerce_registration_generate_username' ) == 'no' ) {
2012-08-10 11:15:32 +00:00
$this -> checkout_fields [ 'account' ][ 'account_username' ] = array (
'type' => 'text' ,
'label' => __ ( 'Account username' , 'woocommerce' ),
2013-07-19 05:58:45 +00:00
'required' => true ,
2012-08-10 11:15:32 +00:00
'placeholder' => _x ( 'Username' , 'placeholder' , 'woocommerce' )
2011-12-21 16:03:45 +00:00
);
2012-08-10 11:15:32 +00:00
}
2013-06-03 15:09:04 +00:00
if ( get_option ( 'woocommerce_registration_generate_password' ) == 'no' ) {
$this -> checkout_fields [ 'account' ][ 'account_password' ] = array (
'type' => 'password' ,
'label' => __ ( 'Account password' , 'woocommerce' ),
2013-07-19 05:58:45 +00:00
'required' => true ,
2013-06-03 15:09:04 +00:00
'placeholder' => _x ( 'Password' , 'placeholder' , 'woocommerce' )
);
}
2012-08-10 11:15:32 +00:00
2011-12-21 16:03:45 +00:00
$this -> checkout_fields [ 'order' ] = array (
2012-08-10 11:15:32 +00:00
'order_comments' => array (
'type' => 'textarea' ,
'class' => array ( 'notes' ),
2012-10-16 09:45:33 +00:00
'label' => __ ( 'Order Notes' , 'woocommerce' ),
2012-08-10 11:15:32 +00:00
'placeholder' => _x ( 'Notes about your order, e.g. special notes for delivery.' , 'placeholder' , 'woocommerce' )
2011-12-21 16:03:45 +00:00
)
);
2012-12-10 12:34:55 +00:00
$this -> checkout_fields = apply_filters ( 'woocommerce_checkout_fields' , $this -> checkout_fields );
do_action ( 'woocommerce_checkout_init' , $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
/**
* Checkout process
*/
2013-09-04 10:26:19 +00:00
public function check_cart_items () {
2012-02-12 11:36:33 +00:00
// When we process the checkout, lets ensure cart items are rechecked to prevent checkout
do_action ( 'woocommerce_check_cart_items' );
}
2012-08-10 11:15:32 +00:00
2012-08-14 19:42:38 +00:00
/**
* Output the billing information form
*
* @ access public
* @ return void
*/
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
/**
* Output the shipping information form
*
* @ access public
* @ return void
*/
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-08-14 19:42:38 +00:00
2012-12-12 21:14:19 +00:00
/**
* create_order function .
* @ access public
2013-10-06 04:44:04 +00:00
* @ throws Exception
* @ return int
2012-12-12 21:14:19 +00:00
*/
2012-12-14 21:27:29 +00:00
public function create_order () {
2013-10-24 12:15:42 +00:00
global $wpdb ;
2012-12-12 21:14:19 +00:00
2013-02-13 09:38:54 +00:00
// Give plugins the opportunity to create an order themselves
$order_id = apply_filters ( 'woocommerce_create_order' , null , $this );
if ( is_numeric ( $order_id ) )
return $order_id ;
// Create Order (send cart variable so we can record items and reduce inventory). Only create if this is a new order, not if the payment was rejected.
2012-12-12 21:14:19 +00:00
$order_data = apply_filters ( 'woocommerce_new_order_data' , array (
'post_type' => 'shop_order' ,
'post_title' => sprintf ( __ ( 'Order – %s' , 'woocommerce' ), strftime ( _x ( '%b %d, %Y @ %I:%M %p' , 'Order date parsed by strftime' , 'woocommerce' ) ) ),
'post_status' => 'publish' ,
'ping_status' => 'closed' ,
'post_excerpt' => isset ( $this -> posted [ 'order_comments' ] ) ? $this -> posted [ 'order_comments' ] : '' ,
'post_author' => 1 ,
'post_password' => uniqid ( 'order_' ) // Protects the post just in case
) );
// Insert or update the post data
$create_new_order = true ;
2013-10-24 12:15:42 +00:00
if ( WC () -> session -> order_awaiting_payment > 0 ) {
2012-12-12 21:14:19 +00:00
2013-10-24 12:15:42 +00:00
$order_id = absint ( WC () -> session -> order_awaiting_payment );
2012-12-12 21:14:19 +00:00
/* Check order is unpaid by getting its status */
$terms = wp_get_object_terms ( $order_id , 'shop_order_status' , array ( 'fields' => 'slugs' ) );
$order_status = isset ( $terms [ 0 ] ) ? $terms [ 0 ] : 'pending' ;
// Resume the unpaid order if its pending
2013-02-14 15:37:05 +00:00
if ( $order_status == 'pending' || $order_status == 'failed' ) {
2012-12-12 21:14:19 +00:00
// Update the existing order as we are resuming it
$create_new_order = false ;
$order_data [ 'ID' ] = $order_id ;
wp_update_post ( $order_data );
// Clear the old line items - we'll add these again in case they changed
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM { $wpdb -> prefix } woocommerce_order_items WHERE order_id = %d ) " , $order_id ) );
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_items WHERE order_id = %d " , $order_id ) );
// Trigger an action for the resumed order
do_action ( 'woocommerce_resume_order' , $order_id );
}
}
if ( $create_new_order ) {
2013-09-24 21:08:32 +00:00
$order_id = wp_insert_post ( $order_data , true );
2012-12-12 21:14:19 +00:00
if ( is_wp_error ( $order_id ) )
2013-05-09 12:39:41 +00:00
throw new Exception ( 'Error: Unable to create order. Please try again.' );
2012-12-12 21:14:19 +00:00
else
do_action ( 'woocommerce_new_order' , $order_id );
}
// Store user data
2013-06-05 11:07:23 +00:00
if ( $this -> checkout_fields [ 'billing' ] )
2013-08-08 11:37:42 +00:00
foreach ( $this -> checkout_fields [ 'billing' ] as $key => $field ) {
2012-12-12 21:14:19 +00:00
update_post_meta ( $order_id , '_' . $key , $this -> posted [ $key ] );
2013-10-04 14:58:00 +00:00
if ( $this -> customer_id && apply_filters ( 'woocommerce_checkout_update_customer_data' , true , $this ) )
2013-08-08 11:37:42 +00:00
update_user_meta ( $this -> customer_id , $key , $this -> posted [ $key ] );
}
2013-10-24 12:15:42 +00:00
if ( $this -> checkout_fields [ 'shipping' ] && WC () -> cart -> needs_shipping () ) {
2012-12-12 21:14:19 +00:00
foreach ( $this -> checkout_fields [ 'shipping' ] as $key => $field ) {
2013-03-04 11:17:48 +00:00
$postvalue = false ;
2012-12-12 21:14:19 +00:00
2013-05-28 13:19:08 +00:00
if ( $this -> posted [ 'ship_to_different_address' ] == false ) {
2013-03-04 11:17:48 +00:00
if ( isset ( $this -> posted [ str_replace ( 'shipping_' , 'billing_' , $key ) ] ) ) {
$postvalue = $this -> posted [ str_replace ( 'shipping_' , 'billing_' , $key ) ];
update_post_meta ( $order_id , '_' . $key , $postvalue );
}
2012-12-12 21:14:19 +00:00
} else {
2013-03-04 11:17:48 +00:00
$postvalue = $this -> posted [ $key ];
update_post_meta ( $order_id , '_' . $key , $postvalue );
2012-12-12 21:14:19 +00:00
}
2013-03-04 11:17:48 +00:00
// User
2013-10-04 14:58:00 +00:00
if ( $postvalue && $this -> customer_id && apply_filters ( 'woocommerce_checkout_update_customer_data' , true , $this ) )
2013-03-04 11:17:48 +00:00
update_user_meta ( $this -> customer_id , $key , $postvalue );
2012-12-12 21:14:19 +00:00
}
}
// Save any other user meta
if ( $this -> customer_id )
do_action ( 'woocommerce_checkout_update_user_meta' , $this -> customer_id , $this -> posted );
// Store the line items to the new/resumed order
2013-10-24 12:15:42 +00:00
foreach ( WC () -> cart -> get_cart () as $cart_item_key => $values ) {
2012-12-12 21:14:19 +00:00
$_product = $values [ 'data' ];
// Add line item
2013-11-25 13:54:52 +00:00
$item_id = wc_add_order_item ( $order_id , array (
2012-12-12 21:14:19 +00:00
'order_item_name' => $_product -> get_title (),
'order_item_type' => 'line_item'
) );
// Add line item meta
if ( $item_id ) {
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , '_qty' , apply_filters ( 'woocommerce_stock_amount' , $values [ 'quantity' ] ) );
wc_add_order_item_meta ( $item_id , '_tax_class' , $_product -> get_tax_class () );
wc_add_order_item_meta ( $item_id , '_product_id' , $values [ 'product_id' ] );
wc_add_order_item_meta ( $item_id , '_variation_id' , $values [ 'variation_id' ] );
wc_add_order_item_meta ( $item_id , '_line_subtotal' , wc_format_decimal ( $values [ 'line_subtotal' ] ) );
wc_add_order_item_meta ( $item_id , '_line_total' , wc_format_decimal ( $values [ 'line_total' ] ) );
wc_add_order_item_meta ( $item_id , '_line_tax' , wc_format_decimal ( $values [ 'line_tax' ] ) );
wc_add_order_item_meta ( $item_id , '_line_subtotal_tax' , wc_format_decimal ( $values [ 'line_subtotal_tax' ] ) );
2012-12-12 21:14:19 +00:00
// Store variation data in meta so admin can view it
if ( $values [ 'variation' ] && is_array ( $values [ 'variation' ] ) )
foreach ( $values [ 'variation' ] as $key => $value )
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , esc_attr ( str_replace ( 'attribute_' , '' , $key ) ), $value );
2012-12-12 21:14:19 +00:00
// Add line item meta for backorder status
if ( $_product -> backorders_require_notification () && $_product -> is_on_backorder ( $values [ 'quantity' ] ) )
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , apply_filters ( 'woocommerce_backordered_item_meta_name' , __ ( 'Backordered' , 'woocommerce' ), $cart_item_key , $order_id ), $values [ 'quantity' ] - max ( 0 , $_product -> get_total_stock () ) );
2012-12-12 21:14:19 +00:00
2013-04-16 14:39:07 +00:00
// Allow plugins to add order item meta
2013-05-15 13:19:06 +00:00
do_action ( 'woocommerce_add_order_item_meta' , $item_id , $values , $cart_item_key );
2012-12-12 21:14:19 +00:00
}
}
// Store fees
2013-10-24 12:15:42 +00:00
foreach ( WC () -> cart -> get_fees () as $fee ) {
2013-11-25 13:54:52 +00:00
$item_id = wc_add_order_item ( $order_id , array (
2012-12-12 21:14:19 +00:00
'order_item_name' => $fee -> name ,
'order_item_type' => 'fee'
) );
if ( $fee -> taxable )
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , '_tax_class' , $fee -> tax_class );
2012-12-12 21:14:19 +00:00
else
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , '_tax_class' , '0' );
2012-12-12 21:14:19 +00:00
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , '_line_total' , wc_format_decimal ( $fee -> amount ) );
wc_add_order_item_meta ( $item_id , '_line_tax' , wc_format_decimal ( $fee -> tax ) );
2012-12-12 21:14:19 +00:00
}
2013-08-16 15:43:26 +00:00
// Store shipping for all packages
$packages = WC () -> shipping -> get_packages ();
foreach ( $packages as $i => $package ) {
if ( isset ( $package [ 'rates' ][ $this -> shipping_methods [ $i ] ] ) ) {
$method = $package [ 'rates' ][ $this -> shipping_methods [ $i ] ];
2013-11-25 13:54:52 +00:00
$item_id = wc_add_order_item ( $order_id , array (
2013-08-16 15:43:26 +00:00
'order_item_name' => $method -> label ,
'order_item_type' => 'shipping'
) );
if ( $item_id ) {
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , 'method_id' , $method -> id );
wc_add_order_item_meta ( $item_id , 'cost' , wc_format_decimal ( $method -> cost ) );
2013-08-16 15:43:26 +00:00
}
}
}
2012-12-12 21:14:19 +00:00
// Store tax rows
2013-10-24 12:15:42 +00:00
foreach ( array_keys ( WC () -> cart -> taxes + WC () -> cart -> shipping_taxes ) as $key ) {
2012-12-12 21:14:19 +00:00
2013-11-25 13:54:52 +00:00
$item_id = wc_add_order_item ( $order_id , array (
2013-10-24 12:15:42 +00:00
'order_item_name' => WC () -> cart -> tax -> get_rate_code ( $key ),
2012-12-12 21:14:19 +00:00
'order_item_type' => 'tax'
) );
// Add line item meta
if ( $item_id ) {
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , 'rate_id' , $key );
wc_add_order_item_meta ( $item_id , 'label' , WC () -> cart -> tax -> get_rate_label ( $key ) );
wc_add_order_item_meta ( $item_id , 'compound' , absint ( WC () -> cart -> tax -> is_compound ( $key ) ? 1 : 0 ) );
wc_add_order_item_meta ( $item_id , 'tax_amount' , wc_format_decimal ( isset ( WC () -> cart -> taxes [ $key ] ) ? WC () -> cart -> taxes [ $key ] : 0 ) );
wc_add_order_item_meta ( $item_id , 'shipping_tax_amount' , wc_format_decimal ( isset ( WC () -> cart -> shipping_taxes [ $key ] ) ? WC () -> cart -> shipping_taxes [ $key ] : 0 ) );
2012-12-12 21:14:19 +00:00
}
}
// Store coupons
2013-10-24 12:15:42 +00:00
if ( $applied_coupons = WC () -> cart -> get_coupons () ) {
2013-10-18 17:10:55 +00:00
foreach ( $applied_coupons as $code => $coupon ) {
2012-12-12 21:14:19 +00:00
2013-11-25 13:54:52 +00:00
$item_id = wc_add_order_item ( $order_id , array (
2012-12-12 21:14:19 +00:00
'order_item_name' => $code ,
'order_item_type' => 'coupon'
) );
// Add line item meta
if ( $item_id ) {
2013-11-25 13:54:52 +00:00
wc_add_order_item_meta ( $item_id , 'discount_amount' , isset ( WC () -> cart -> coupon_discount_amounts [ $code ] ) ? WC () -> cart -> coupon_discount_amounts [ $code ] : 0 );
2012-12-12 21:14:19 +00:00
}
}
}
2012-12-27 19:24:33 +00:00
if ( $this -> payment_method ) {
update_post_meta ( $order_id , '_payment_method' , $this -> payment_method -> id );
update_post_meta ( $order_id , '_payment_method_title' , $this -> payment_method -> get_title () );
2012-12-12 21:14:19 +00:00
}
2013-11-25 13:34:21 +00:00
update_post_meta ( $order_id , '_order_shipping' , wc_format_decimal ( WC () -> cart -> shipping_total ) );
update_post_meta ( $order_id , '_order_discount' , wc_format_decimal ( WC () -> cart -> get_order_discount_total () ) );
update_post_meta ( $order_id , '_cart_discount' , wc_format_decimal ( WC () -> cart -> get_cart_discount_total () ) );
update_post_meta ( $order_id , '_order_tax' , wc_format_decimal ( wc_round_tax_total ( WC () -> cart -> tax_total ) ) );
update_post_meta ( $order_id , '_order_shipping_tax' , wc_format_decimal ( wc_round_tax_total ( WC () -> cart -> shipping_tax_total ) ) );
update_post_meta ( $order_id , '_order_total' , wc_format_decimal ( WC () -> cart -> total , get_option ( 'woocommerce_price_num_decimals' ) ) );
2013-10-01 16:54:29 +00:00
2013-11-20 08:03:49 +00:00
update_post_meta ( $order_id , '_order_key' , 'wc_' . apply_filters ( 'woocommerce_generate_order_key' , uniqid ( 'order_' ) ) );
2012-12-12 21:14:19 +00:00
update_post_meta ( $order_id , '_customer_user' , absint ( $this -> customer_id ) );
update_post_meta ( $order_id , '_order_currency' , get_woocommerce_currency () );
update_post_meta ( $order_id , '_prices_include_tax' , get_option ( 'woocommerce_prices_include_tax' ) );
update_post_meta ( $order_id , '_customer_ip_address' , isset ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ] ) ? $_SERVER [ 'HTTP_X_FORWARDED_FOR' ] : $_SERVER [ 'REMOTE_ADDR' ] );
update_post_meta ( $order_id , '_customer_user_agent' , isset ( $_SERVER [ 'HTTP_USER_AGENT' ] ) ? $_SERVER [ 'HTTP_USER_AGENT' ] : '' );
// Let plugins add meta
do_action ( 'woocommerce_checkout_update_order_meta' , $order_id , $this -> posted );
// Order status
wp_set_object_terms ( $order_id , 'pending' , 'shop_order_status' );
return $order_id ;
}
2011-09-12 15:34:29 +00:00
/**
* Process the checkout after the confirm order button is pressed
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
2011-09-12 15:34:29 +00:00
*/
2012-12-14 21:27:29 +00:00
public function process_checkout () {
2013-10-24 12:15:42 +00:00
global $wpdb , $current_user ;
2011-09-14 14:55:03 +00:00
2013-06-11 16:55:55 +00:00
wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-process_checkout' );
2012-12-12 21:14:19 +00:00
2012-11-27 16:22:47 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' ) )
2012-09-07 17:26:13 +00:00
define ( 'WOOCOMMERCE_CHECKOUT' , true );
2012-08-10 11:15:32 +00:00
2012-12-12 21:14:19 +00:00
// Prevent timeout
@ set_time_limit ( 0 );
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
do_action ( 'woocommerce_before_checkout_process' );
2012-01-12 00:43:30 +00:00
2013-10-24 12:15:42 +00:00
if ( sizeof ( WC () -> cart -> get_cart () ) == 0 )
2013-11-13 04:29:03 +00:00
wc_add_notice ( sprintf ( __ ( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to homepage</a>' , 'woocommerce' ), home_url () ), 'error' );
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
do_action ( 'woocommerce_checkout_process' );
2012-01-12 00:43:30 +00:00
2012-01-12 00:54:45 +00:00
// Checkout fields (not defined in checkout_fields)
2013-05-28 13:19:08 +00:00
$this -> posted [ 'terms' ] = isset ( $_POST [ 'terms' ] ) ? 1 : 0 ;
$this -> posted [ 'createaccount' ] = isset ( $_POST [ 'createaccount' ] ) ? 1 : 0 ;
2013-05-31 12:05:11 +00:00
$this -> posted [ 'payment_method' ] = isset ( $_POST [ 'payment_method' ] ) ? stripslashes ( $_POST [ 'payment_method' ] ) : '' ;
2013-09-04 10:26:19 +00:00
$this -> posted [ 'shipping_method' ] = isset ( $_POST [ 'shipping_method' ] ) ? $_POST [ 'shipping_method' ] : '' ;
2013-05-28 13:19:08 +00:00
$this -> posted [ 'ship_to_different_address' ] = isset ( $_POST [ 'ship_to_different_address' ] ) ? true : false ;
if ( isset ( $_POST [ 'shiptobilling' ] ) ) {
_deprecated_argument ( 'WC_Checkout::process_checkout()' , '2.1' , 'The "shiptobilling" field is deprecated. THe template files are out of date' );
$this -> posted [ 'ship_to_different_address' ] = $_POST [ 'shiptobilling' ] ? false : true ;
}
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Ship to billing only option
2013-10-24 12:15:42 +00:00
if ( WC () -> cart -> ship_to_billing_address_only () )
2013-05-28 13:19:08 +00:00
$this -> posted [ 'ship_to_different_address' ] = false ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Update customer shipping and payment method to posted method
2013-08-14 20:00:34 +00:00
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
if ( isset ( $this -> posted [ 'shipping_method' ] ) && is_array ( $this -> posted [ 'shipping_method' ] ) )
foreach ( $this -> posted [ 'shipping_method' ] as $i => $value )
2013-11-25 13:34:21 +00:00
$chosen_shipping_methods [ $i ] = wc_clean ( $value );
2013-08-14 20:00:34 +00:00
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
WC () -> session -> set ( 'chosen_payment_method' , $this -> posted [ 'payment_method' ] );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Note if we skip shipping
$skipped_shipping = false ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Get posted checkout_fields and do validation
2012-08-10 11:15:32 +00:00
foreach ( $this -> checkout_fields as $fieldset_key => $fieldset ) {
2013-05-28 13:19:08 +00:00
// Skip shipping if not needed
2013-10-24 12:15:42 +00:00
if ( $fieldset_key == 'shipping' && ( $this -> posted [ 'ship_to_different_address' ] == false || ! WC () -> cart -> needs_shipping () ) ) {
2012-01-12 00:54:45 +00:00
$skipped_shipping = true ;
continue ;
2012-04-21 18:30:37 +00:00
}
2012-08-10 11:15:32 +00:00
2013-07-19 05:58:45 +00:00
// Ship account if not needed
if ( $fieldset_key == 'account' && ( is_user_logged_in () || ( $this -> must_create_account == false && empty ( $this -> posted [ 'createaccount' ] ) ) ) ) {
continue ;
}
2012-07-16 19:21:44 +00:00
foreach ( $fieldset as $key => $field ) {
2012-08-10 11:15:32 +00:00
2013-05-23 08:40:55 +00:00
if ( ! isset ( $field [ 'type' ] ) )
$field [ 'type' ] = 'text' ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Get Value
2012-07-16 19:21:44 +00:00
switch ( $field [ 'type' ] ) {
2012-01-12 00:54:45 +00:00
case " checkbox " :
2013-05-23 08:40:55 +00:00
$this -> posted [ $key ] = isset ( $_POST [ $key ] ) ? 1 : 0 ;
break ;
case " multiselect " :
2013-11-25 13:34:21 +00:00
$this -> posted [ $key ] = isset ( $_POST [ $key ] ) ? implode ( ', ' , array_map ( 'wc_clean' , $_POST [ $key ] ) ) : '' ;
2012-01-12 00:54:45 +00:00
break ;
2013-10-04 12:31:43 +00:00
case " textarea " :
$this -> posted [ $key ] = isset ( $_POST [ $key ] ) ? wp_strip_all_tags ( wp_check_invalid_utf8 ( stripslashes ( $_POST [ $key ] ) ) ) : '' ;
break ;
2012-01-12 00:54:45 +00:00
default :
2013-11-25 13:34:21 +00:00
$this -> posted [ $key ] = isset ( $_POST [ $key ] ) ? wc_clean ( $_POST [ $key ] ) : '' ;
2012-01-12 00:54:45 +00:00
break ;
2012-07-16 19:21:44 +00:00
}
2012-08-10 11:15:32 +00:00
2013-05-23 08:40:55 +00:00
// Hooks to allow modification of value
$this -> posted [ $key ] = apply_filters ( 'woocommerce_process_checkout_' . sanitize_title ( $field [ 'type' ] ) . '_field' , $this -> posted [ $key ] );
$this -> posted [ $key ] = apply_filters ( 'woocommerce_process_checkout_field_' . $key , $this -> posted [ $key ] );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Validation: Required fields
2013-06-11 14:59:54 +00:00
if ( isset ( $field [ 'required' ] ) && $field [ 'required' ] && empty ( $this -> posted [ $key ] ) )
2013-11-13 04:29:03 +00:00
wc_add_notice ( '<strong>' . $field [ 'label' ] . '</strong> ' . __ ( 'is a required field.' , 'woocommerce' ), 'error' );
2012-08-10 11:15:32 +00:00
2012-07-16 19:21:44 +00:00
if ( ! empty ( $this -> posted [ $key ] ) ) {
2012-08-10 11:15:32 +00:00
2013-09-19 13:39:49 +00:00
// Validation rules
if ( ! empty ( $field [ 'validate' ] ) && is_array ( $field [ 'validate' ] ) ) {
foreach ( $field [ 'validate' ] as $rule ) {
switch ( $rule ) {
case 'postcode' :
$this -> posted [ $key ] = strtoupper ( str_replace ( ' ' , '' , $this -> posted [ $key ] ) );
if ( ! WC_Validation :: is_postcode ( $this -> posted [ $key ], $_POST [ $fieldset_key . '_country' ] ) ) :
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please enter a valid postcode/ZIP.' , 'woocommerce' ), 'error' );
2013-09-19 13:39:49 +00:00
else :
$this -> posted [ $key ] = wc_format_postcode ( $this -> posted [ $key ], $_POST [ $fieldset_key . '_country' ] );
endif ;
break ;
case 'phone' :
$this -> posted [ $key ] = wc_format_phone_number ( $this -> posted [ $key ] );
if ( ! WC_Validation :: is_phone ( $this -> posted [ $key ] ) )
2013-11-13 04:29:03 +00:00
wc_add_notice ( '<strong>' . $field [ 'label' ] . '</strong> ' . __ ( 'is not a valid phone number.' , 'woocommerce' ), 'error' );
2013-09-19 13:39:49 +00:00
break ;
case 'email' :
$this -> posted [ $key ] = strtolower ( $this -> posted [ $key ] );
if ( ! is_email ( $this -> posted [ $key ] ) )
2013-11-13 04:29:03 +00:00
wc_add_notice ( '<strong>' . $field [ 'label' ] . '</strong> ' . __ ( 'is not a valid email address.' , 'woocommerce' ), 'error' );
2013-09-19 13:39:49 +00:00
break ;
case 'state' :
// Get valid states
2013-10-24 12:15:42 +00:00
$valid_states = WC () -> countries -> get_states ( $_POST [ $fieldset_key . '_country' ] );
2013-09-19 13:39:49 +00:00
if ( $valid_states )
$valid_state_values = array_flip ( array_map ( 'strtolower' , $valid_states ) );
// Convert value to key if set
if ( isset ( $valid_state_values [ strtolower ( $this -> posted [ $key ] ) ] ) )
$this -> posted [ $key ] = $valid_state_values [ strtolower ( $this -> posted [ $key ] ) ];
// Only validate if the country has specific state options
if ( $valid_states && sizeof ( $valid_states ) > 0 )
if ( ! in_array ( $this -> posted [ $key ], array_keys ( $valid_states ) ) )
2013-11-13 04:29:03 +00:00
wc_add_notice ( '<strong>' . $field [ 'label' ] . '</strong> ' . __ ( 'is not valid. Please enter one of the following:' , 'woocommerce' ) . ' ' . implode ( ', ' , $valid_states ), 'error' );
2013-09-19 13:39:49 +00:00
break ;
}
}
2012-07-16 19:21:44 +00:00
}
}
}
}
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Update customer location to posted location so we can correctly check available shipping methods
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'billing_country' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_country ( $this -> posted [ 'billing_country' ] );
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'billing_state' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_state ( $this -> posted [ 'billing_state' ] );
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'billing_postcode' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_postcode ( $this -> posted [ 'billing_postcode' ] );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Shipping Information
2012-03-26 19:28:19 +00:00
if ( ! $skipped_shipping ) {
2012-08-10 11:15:32 +00:00
2011-12-21 16:03:45 +00:00
// Update customer location to posted location so we can correctly check available shipping methods
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'shipping_country' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_shipping_country ( $this -> posted [ 'shipping_country' ] );
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'shipping_state' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_shipping_state ( $this -> posted [ 'shipping_state' ] );
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'shipping_postcode' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_shipping_postcode ( $this -> posted [ 'shipping_postcode' ] );
2012-08-10 11:15:32 +00:00
2012-03-26 19:28:19 +00:00
} else {
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Update customer location to posted location so we can correctly check available shipping methods
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'billing_country' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_shipping_country ( $this -> posted [ 'billing_country' ] );
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'billing_state' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_shipping_state ( $this -> posted [ 'billing_state' ] );
2012-08-10 11:15:32 +00:00
if ( isset ( $this -> posted [ 'billing_postcode' ] ) )
2013-10-24 12:15:42 +00:00
WC () -> customer -> set_shipping_postcode ( $this -> posted [ 'billing_postcode' ] );
2012-08-10 11:15:32 +00:00
2012-03-26 19:28:19 +00:00
}
2012-08-10 11:15:32 +00:00
2012-07-16 19:21:44 +00:00
// Update cart totals now we have customer address
2013-10-24 12:15:42 +00:00
WC () -> cart -> calculate_totals ();
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Terms
2013-11-25 14:07:22 +00:00
if ( ! isset ( $_POST [ 'woocommerce_checkout_update_totals' ] ) && empty ( $this -> posted [ 'terms' ] ) && wc_get_page_id ( 'terms' ) > 0 )
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'You must accept our Terms & Conditions.' , 'woocommerce' ), 'error' );
2012-08-10 11:15:32 +00:00
2013-10-24 12:15:42 +00:00
if ( WC () -> cart -> needs_shipping () ) {
2012-08-10 11:15:32 +00:00
2013-10-24 12:15:42 +00:00
if ( ! in_array ( WC () -> customer -> get_shipping_country (), array_keys ( WC () -> countries -> get_shipping_countries () ) ) )
2013-11-13 04:29:03 +00:00
wc_add_notice ( sprintf ( __ ( 'Unfortunately <strong>we do not ship to %s</strong>. Please enter an alternative shipping address.' , 'woocommerce' ), WC () -> countries -> shipping_to_prefix () . ' ' . WC () -> customer -> get_shipping_country () ), 'error' );
2013-08-02 15:54:28 +00:00
2013-08-14 20:00:34 +00:00
// Validate Shipping Methods
$packages = WC () -> shipping -> get_packages ();
$this -> shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
2012-08-10 11:15:32 +00:00
2013-08-14 20:00:34 +00:00
foreach ( $packages as $i => $package ) {
if ( ! isset ( $package [ 'rates' ][ $this -> shipping_methods [ $i ] ] ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Invalid shipping method.' , 'woocommerce' ), 'error' );
2013-08-14 20:00:34 +00:00
$this -> shipping_methods [ $i ] = '' ;
}
2012-12-12 21:14:19 +00:00
}
2012-08-10 11:15:32 +00:00
}
2013-10-24 12:15:42 +00:00
if ( WC () -> cart -> needs_payment () ) {
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Payment Method
2013-10-24 12:15:42 +00:00
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
2012-08-10 11:15:32 +00:00
2012-12-12 21:14:19 +00:00
if ( ! isset ( $available_gateways [ $this -> posted [ 'payment_method' ] ] ) ) {
2012-12-27 19:24:33 +00:00
$this -> payment_method = '' ;
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Invalid payment method.' , 'woocommerce' ), 'error' );
2012-12-12 21:14:19 +00:00
} else {
2012-12-27 19:24:33 +00:00
$this -> payment_method = $available_gateways [ $this -> posted [ 'payment_method' ] ];
$this -> payment_method -> validate_fields ();
2012-12-12 21:14:19 +00:00
}
2012-07-16 19:21:44 +00:00
}
2012-08-10 11:15:32 +00:00
2012-07-16 19:21:44 +00:00
// Action after validation
2012-01-12 00:54:45 +00:00
do_action ( 'woocommerce_after_checkout_validation' , $this -> posted );
2012-08-10 11:15:32 +00:00
2013-11-27 16:15:53 +00:00
if ( ! isset ( $_POST [ 'woocommerce_checkout_update_totals' ] ) && wc_notice_count ( 'error' ) == 0 ) {
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
try {
2012-08-10 11:15:32 +00:00
2013-06-04 15:33:05 +00:00
// Customer accounts
2013-07-30 13:50:53 +00:00
$this -> customer_id = apply_filters ( 'woocommerce_checkout_customer_id' , get_current_user_id () );
2012-08-10 11:15:32 +00:00
2013-06-04 15:33:05 +00:00
if ( ! is_user_logged_in () && ( $this -> must_create_account || ! empty ( $this -> posted [ 'createaccount' ] ) ) ) {
2012-08-10 11:15:32 +00:00
2013-06-04 15:33:05 +00:00
$username = ! empty ( $this -> posted [ 'account_username' ] ) ? $this -> posted [ 'account_username' ] : '' ;
$password = ! empty ( $this -> posted [ 'account_password' ] ) ? $this -> posted [ 'account_password' ] : '' ;
2013-11-25 12:52:53 +00:00
$new_customer = wc_create_new_customer ( $this -> posted [ 'billing_email' ], $username , $password );
2012-08-10 11:15:32 +00:00
2013-06-04 15:33:05 +00:00
if ( is_wp_error ( $new_customer ) )
throw new Exception ( $new_customer -> get_error_message () );
2012-12-28 18:45:06 +00:00
2013-06-04 15:33:05 +00:00
$this -> customer_id = $new_customer ;
2012-08-10 11:15:32 +00:00
2013-11-25 12:52:53 +00:00
wc_set_customer_auth_cookie ( $this -> customer_id );
2013-06-05 11:07:23 +00:00
2013-09-04 10:26:19 +00:00
// As we are now logged in, checkout will need to refresh to show logged in data
WC () -> session -> set ( 'reload_checkout' , true );
2013-09-02 16:43:53 +00:00
2013-06-05 11:07:23 +00:00
// Add customer info from other billing fields
2013-12-03 12:27:53 +00:00
if ( $this -> posted [ 'billing_first_name' ] && apply_filters ( 'woocommerce_checkout_update_customer_data' , true , $this ) ) {
$userdata = array (
'ID' => $this -> customer_id ,
'first_name' => $this -> posted [ 'billing_first_name' ] ? $this -> posted [ 'billing_first_name' ] : '' ,
'last_name' => $this -> posted [ 'billing_last_name' ] ? $this -> posted [ 'billing_last_name' ] : '' ,
'display_name' => $this -> posted [ 'billing_first_name' ] ? $this -> posted [ 'billing_first_name' ] : ''
);
wp_update_user ( apply_filters ( 'woocommerce_checkout_customer_userdata' , $userdata , $this ) );
}
2012-09-07 17:26:13 +00:00
}
2012-08-10 11:15:32 +00:00
2013-09-04 10:26:19 +00:00
// Do a final stock check at this point
$this -> check_cart_items ();
2012-12-12 21:14:19 +00:00
// Abort if errors are present
2013-11-27 16:15:53 +00:00
if ( wc_notice_count ( 'error' ) > 0 )
2013-05-09 12:39:41 +00:00
throw new Exception ();
2012-08-10 11:15:32 +00:00
2012-12-12 21:14:19 +00:00
$order_id = $this -> create_order ();
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
do_action ( 'woocommerce_checkout_order_processed' , $order_id , $this -> posted );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Process payment
2013-10-24 12:15:42 +00:00
if ( WC () -> cart -> needs_payment () ) {
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Store Order ID in session so it can be re-used after payment failure
2013-10-24 12:15:42 +00:00
WC () -> session -> order_awaiting_payment = $order_id ;
2012-01-10 16:43:06 +00:00
2012-01-12 00:54:45 +00:00
// Process Payment
2012-09-07 17:26:13 +00:00
$result = $available_gateways [ $this -> posted [ 'payment_method' ] ] -> process_payment ( $order_id );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Redirect to success/confirmation/payment page
2012-09-07 17:26:13 +00:00
if ( $result [ 'result' ] == 'success' ) {
2012-08-10 11:15:32 +00:00
2013-12-23 15:47:56 +00:00
$result = apply_filters ( 'woocommerce_payment_successful_result' , $result , $order_id );
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
if ( is_ajax () ) {
2013-03-08 12:24:22 +00:00
echo '<!--WC_START-->' . json_encode ( $result ) . '<!--WC_END-->' ;
2011-08-09 15:16:18 +00:00
exit ;
2012-09-07 17:26:13 +00:00
} else {
2012-02-24 20:44:58 +00:00
wp_redirect ( $result [ 'redirect' ] );
2011-08-09 15:16:18 +00:00
exit ;
2012-09-07 17:26:13 +00:00
}
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
}
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
} else {
2012-08-10 11:15:32 +00:00
2012-08-12 14:12:52 +00:00
if ( empty ( $order ) )
$order = new WC_Order ( $order_id );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// No payment was required for order
$order -> payment_complete ();
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Empty the Cart
2013-10-24 12:15:42 +00:00
WC () -> cart -> empty_cart ();
2012-08-10 11:15:32 +00:00
2012-03-05 13:25:02 +00:00
// Get redirect
2013-05-31 15:13:14 +00:00
$return_url = $order -> get_checkout_order_received_url ();
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Redirect to success/confirmation/payment page
2012-09-07 17:26:13 +00:00
if ( is_ajax () ) {
2013-03-08 12:24:22 +00:00
echo '<!--WC_START-->' . json_encode (
2012-01-18 12:32:39 +00:00
array (
2012-03-16 15:09:12 +00:00
'result' => 'success' ,
2013-09-02 16:43:53 +00:00
'redirect' => apply_filters ( 'woocommerce_checkout_no_payment_needed_redirect' , $return_url , $order )
2012-08-10 11:15:32 +00:00
)
2012-12-13 14:57:31 +00:00
) . '<!--WC_END-->' ;
2012-01-12 00:54:45 +00:00
exit ;
2012-09-07 17:26:13 +00:00
} else {
2012-08-10 11:15:32 +00:00
wp_safe_redirect (
2013-05-31 15:13:14 +00:00
apply_filters ( 'woocommerce_checkout_no_payment_needed_redirect' , $return_url , $order )
2012-01-18 12:32:39 +00:00
);
2012-01-12 00:54:45 +00:00
exit ;
2012-09-07 17:26:13 +00:00
}
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
}
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
} catch ( Exception $e ) {
2012-11-27 16:22:47 +00:00
2012-09-07 17:26:13 +00:00
if ( ! empty ( $e ) )
2013-11-13 04:29:03 +00:00
wc_add_notice ( $e -> getMessage (), 'error' );
2012-11-27 16:22:47 +00:00
2012-09-07 17:26:13 +00:00
}
2012-01-12 00:54:45 +00:00
2012-09-07 17:26:13 +00:00
} // endif
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// If we reached this point then there were errors
2012-09-07 17:26:13 +00:00
if ( is_ajax () ) {
2012-08-10 11:15:32 +00:00
2012-03-14 11:16:26 +00:00
ob_start ();
2013-11-13 04:34:55 +00:00
wc_print_notices ();
2012-03-14 11:16:26 +00:00
$messages = ob_get_clean ();
2012-08-10 11:15:32 +00:00
2013-03-08 12:24:22 +00:00
echo '<!--WC_START-->' . json_encode (
2012-03-14 11:16:26 +00:00
array (
'result' => 'failure' ,
'messages' => $messages ,
2013-10-24 12:15:42 +00:00
'refresh' => isset ( WC () -> session -> refresh_totals ) ? 'true' : 'false' ,
'reload' => isset ( WC () -> session -> reload_checkout ) ? 'true' : 'false'
2012-08-10 11:15:32 +00:00
)
2012-12-13 14:57:31 +00:00
) . '<!--WC_END-->' ;
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 );
2012-01-12 00:54:45 +00:00
exit ;
2012-09-07 17:26:13 +00:00
}
2011-08-09 15:16:18 +00:00
}
2012-08-10 11:15:32 +00:00
2012-08-14 19:42:38 +00:00
/**
* Gets the value either from the posted data , or from the users meta data
*
* @ access public
* @ param string $input
* @ return string
*/
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 ] ) ) {
2012-08-10 11:15:32 +00:00
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 {
2012-08-10 11:15:32 +00:00
2013-09-10 14:23:26 +00:00
$value = apply_filters ( 'woocommerce_checkout_get_value' , null , $input );
2013-09-19 14:02:14 +00:00
if ( $value )
return $value ;
2013-09-10 14:23:26 +00:00
2013-01-07 15:58:19 +00:00
if ( is_user_logged_in () ) {
2012-08-10 11:15:32 +00:00
2013-01-07 15:58:19 +00:00
$current_user = wp_get_current_user ();
2012-08-10 11:15:32 +00:00
2013-01-07 17:26:02 +00:00
if ( $meta = get_user_meta ( $current_user -> ID , $input , true ) )
2013-01-07 15:58:19 +00:00
return $meta ;
2012-08-10 11:15:32 +00:00
2013-01-07 15:58:19 +00:00
if ( $input == " billing_email " )
return $current_user -> user_email ;
}
2012-08-10 11:15:32 +00:00
2013-09-19 14:02:14 +00:00
switch ( $input ) {
case " billing_country " :
2013-10-24 12:15:42 +00:00
return apply_filters ( 'default_checkout_country' , WC () -> customer -> get_country () ? WC () -> customer -> get_country () : WC () -> countries -> get_base_country (), 'billing' );
2013-09-19 14:02:14 +00:00
case " billing_state " :
2013-10-24 12:15:42 +00:00
return apply_filters ( 'default_checkout_state' , WC () -> customer -> has_calculated_shipping () ? WC () -> customer -> get_state () : '' , 'billing' );
2013-09-19 14:02:14 +00:00
case " billing_postcode " :
2013-10-24 12:15:42 +00:00
return apply_filters ( 'default_checkout_postcode' , WC () -> customer -> get_postcode () ? WC () -> customer -> get_postcode () : '' , 'billing' );
2013-09-19 14:02:14 +00:00
case " shipping_country " :
2013-10-24 12:15:42 +00:00
return apply_filters ( 'default_checkout_country' , WC () -> customer -> get_shipping_country () ? WC () -> customer -> get_shipping_country () : WC () -> countries -> get_base_country (), 'shipping' );
2013-09-19 14:02:14 +00:00
case " shipping_state " :
2013-10-24 12:15:42 +00:00
return apply_filters ( 'default_checkout_state' , WC () -> customer -> has_calculated_shipping () ? WC () -> customer -> get_shipping_state () : '' , 'shipping' );
2013-09-19 14:02:14 +00:00
case " shipping_postcode " :
2013-10-24 12:15:42 +00:00
return apply_filters ( 'default_checkout_postcode' , WC () -> customer -> get_shipping_postcode () ? WC () -> customer -> get_shipping_postcode () : '' , 'shipping' );
2013-09-19 14:02:14 +00:00
default :
return apply_filters ( 'default_checkout_' . $input , '' , $input );
2012-04-19 11:09:52 +00:00
}
2012-10-19 17:59:17 +00:00
}
2011-08-09 15:16:18 +00:00
}
2013-01-22 21:30:14 +00:00
}