2011-08-09 15:16:18 +00:00
< ? php
/**
* Checkout
*
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
*
2011-08-10 17:11:11 +00:00
* @ class woocommerce_checkout
* @ package WooCommerce
* @ category Class
* @ author WooThemes
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
class woocommerce_checkout {
2011-08-09 15:16:18 +00:00
var $posted ;
var $billing_fields ;
var $shipping_fields ;
var $must_create_account ;
var $creating_account ;
/** constructor */
2011-09-06 11:18:22 +00:00
function __construct () {
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
add_action ( 'woocommerce_checkout_billing' , array ( & $this , 'checkout_form_billing' ));
add_action ( 'woocommerce_checkout_shipping' , array ( & $this , 'checkout_form_shipping' ));
2011-08-09 15:16:18 +00:00
$this -> must_create_account = true ;
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_enable_guest_checkout' ) == 'yes' || is_user_logged_in ()) $this -> must_create_account = false ;
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Define billing fields in an array. This can be hooked into and filtered if you wish to change/add anything.
$this -> billing_fields = apply_filters ( 'woocommerce_billing_fields' , array (
2011-09-14 14:55:03 +00:00
'billing_first_name' => array (
'name' => 'billing_first_name' ,
2011-09-12 15:34:29 +00:00
'label' => __ ( 'First Name' , 'woothemes' ),
'placeholder' => __ ( 'First Name' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' )
),
2011-09-14 14:55:03 +00:00
'billing_last_name' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Last Name' , 'woothemes' ),
'placeholder' => __ ( 'Last Name' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-last' )
),
2011-09-14 14:55:03 +00:00
'billing_company' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Company' , 'woothemes' ),
'placeholder' => __ ( 'Company' , 'woothemes' )
),
2011-09-14 14:55:03 +00:00
'billing_address' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Address' , 'woothemes' ),
'placeholder' => __ ( 'Address 1' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' )
),
2011-09-14 14:55:03 +00:00
'billing_address-2' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Address 2' , 'woothemes' ),
'placeholder' => __ ( 'Address 2' , 'woothemes' ),
'class' => array ( 'form-row-last' ),
'label_class' => array ( 'hidden' )
),
2011-09-14 14:55:03 +00:00
'billing_city' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'City' , 'woothemes' ),
'placeholder' => __ ( 'City' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' )
),
2011-09-14 14:55:03 +00:00
'billing_postcode' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Postcode' , 'woothemes' ),
'placeholder' => __ ( 'Postcode' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-last' )
),
2011-09-14 14:55:03 +00:00
'billing_country' => array (
2011-09-12 15:34:29 +00:00
'type' => 'country' ,
'label' => __ ( 'Country' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' ),
2011-09-14 14:55:03 +00:00
'rel' => 'billing_state'
2011-09-12 15:34:29 +00:00
),
2011-09-14 14:55:03 +00:00
'billing_state' => array (
2011-09-12 15:34:29 +00:00
'type' => 'state' ,
2011-09-14 14:55:03 +00:00
'name' => 'billing_state' ,
2011-09-12 15:34:29 +00:00
'label' => __ ( 'State/County' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-last' ),
2011-09-14 14:55:03 +00:00
'rel' => 'billing_country'
2011-09-12 15:34:29 +00:00
),
2011-09-14 14:55:03 +00:00
'billing_email' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Email Address' , 'woothemes' ),
'placeholder' => __ ( 'you@yourdomain.com' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' )
),
2011-09-14 14:55:03 +00:00
'billing_phone' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Phone' , 'woothemes' ),
'placeholder' => __ ( 'Phone number' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-last' )
)
));
// Define shipping fields in an array. This can be hooked into and filtered if you wish to change/add anything.
$this -> shipping_fields = apply_filters ( 'woocommerce_shipping_fields' , array (
2011-09-14 14:55:03 +00:00
'shipping_first_name' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'First Name' , 'woothemes' ),
'placeholder' => __ ( 'First Name' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' )
),
2011-09-14 14:55:03 +00:00
'shipping_last_name' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Last Name' , 'woothemes' ),
'placeholder' => __ ( 'Last Name' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-last' )
),
2011-09-14 14:55:03 +00:00
'shipping_company' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Company' , 'woothemes' ),
'placeholder' => __ ( 'Company' , 'woothemes' )
),
2011-09-14 14:55:03 +00:00
'shipping_address' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Address' , 'woothemes' ),
'placeholder' => __ ( 'Address 1' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' )
),
2011-09-14 14:55:03 +00:00
'shipping_address-2' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Address 2' , 'woothemes' ),
'placeholder' => __ ( 'Address 2' , 'woothemes' ),
'class' => array ( 'form-row-last' ),
'label_class' => array ( 'hidden' )
),
2011-09-14 14:55:03 +00:00
'shipping_city' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'City' , 'woothemes' ),
'placeholder' => __ ( 'City' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' )
),
2011-09-14 14:55:03 +00:00
'shipping_postcode' => array (
2011-09-12 15:34:29 +00:00
'label' => __ ( 'Postcode' , 'woothemes' ),
'placeholder' => __ ( 'Postcode' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-last' )
),
2011-09-14 14:55:03 +00:00
'shipping_country' => array (
2011-09-12 15:34:29 +00:00
'type' => 'country' ,
'label' => __ ( 'Country' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-first' ),
2011-09-14 14:55:03 +00:00
'rel' => 'shipping_state'
2011-09-12 15:34:29 +00:00
),
2011-09-14 14:55:03 +00:00
'shipping_state' => array (
2011-09-12 15:34:29 +00:00
'type' => 'state' ,
'label' => __ ( 'State/County' , 'woothemes' ),
'required' => true ,
'class' => array ( 'form-row-last' ),
2011-09-14 14:55:03 +00:00
'rel' => 'shipping_country'
2011-09-12 15:34:29 +00:00
)
));
2011-08-09 15:16:18 +00:00
}
/** Output the billing information form */
function checkout_form_billing () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> ship_to_billing_address_only ()) :
2011-08-10 17:11:11 +00:00
echo '<h3>' . __ ( 'Billing & Shipping' , 'woothemes' ) . '</h3>' ;
2011-08-09 15:16:18 +00:00
else :
2011-08-10 17:11:11 +00:00
echo '<h3>' . __ ( 'Billing Address' , 'woothemes' ) . '</h3>' ;
2011-08-09 15:16:18 +00:00
endif ;
2011-09-12 15:34:29 +00:00
// Output billing form fields
2011-09-13 16:17:52 +00:00
do_action ( 'woocommerce_before_checkout_billing_form' , $this );
2011-09-12 15:34:29 +00:00
foreach ( $this -> billing_fields as $key => $field ) :
$this -> checkout_form_field ( $key , $field );
2011-08-09 15:16:18 +00:00
endforeach ;
2011-09-13 16:17:52 +00:00
do_action ( 'woocommerce_after_checkout_billing_form' , $this );
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Registration Form Fields
2011-08-09 15:16:18 +00:00
if ( ! is_user_logged_in ()) :
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_enable_guest_checkout' ) == 'yes' ) :
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
echo '<p class="form-row"><input class="input-checkbox" id="createaccount" ' . checked ( $this -> get_value ( 'createaccount' ), true ) . ' type="checkbox" name="createaccount" value="1" /> <label for="createaccount" class="checkbox">' . __ ( 'Create an account?' , 'woothemes' ) . '</label></p>' ;
2011-08-09 15:16:18 +00:00
endif ;
echo '<div class="create-account">' ;
2011-08-27 11:53:46 +00:00
echo '<p>' . __ ( 'Create an account by entering the information below. If you are a returning customer please login with your username at the top of the page.' , 'woothemes' ) . '</p>' ;
2011-09-14 14:55:03 +00:00
$this -> checkout_form_field ( 'account_username' , array (
2011-09-12 15:34:29 +00:00
'type' => 'text' ,
'label' => __ ( 'Account username' , 'woothemes' ),
'placeholder' => __ ( 'Username' , 'woothemes' )
));
2011-09-14 14:55:03 +00:00
$this -> checkout_form_field ( 'account_password' , array (
2011-09-12 15:34:29 +00:00
'type' => 'password' ,
'label' => __ ( 'Account password' , 'woothemes' ),
'placeholder' => __ ( 'Password' , 'woothemes' ),
'class' => array ( 'form-row-first' )
));
2011-09-14 14:55:03 +00:00
$this -> checkout_form_field ( 'account_password-2' , array (
2011-09-12 15:34:29 +00:00
'type' => 'password' ,
'label' => __ ( 'Account password' , 'woothemes' ),
'placeholder' => __ ( 'Password' , 'woothemes' ),
'class' => array ( 'form-row-last' ),
'label_class' => array ( 'hidden' )
));
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
echo '</div>' ;
2011-08-09 15:16:18 +00:00
endif ;
}
/** Output the shipping information form */
function checkout_form_shipping () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-09 15:16:18 +00:00
// Shipping Details
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> needs_shipping () && ! $woocommerce -> cart -> ship_to_billing_address_only ()) :
2011-08-09 15:16:18 +00:00
2011-09-21 16:58:05 +00:00
if ( ! isset ( $_POST ) || ! $_POST ) $shiptobilling = apply_filters ( 'woocommerce_shiptobilling_default' , 1 ); else $shiptobilling = $this -> get_value ( 'shiptobilling' );
2011-09-12 15:34:29 +00:00
echo '<p class="form-row" id="shiptobilling"><input class="input-checkbox" ' . checked ( $shiptobilling , 1 , false ) . ' type="checkbox" name="shiptobilling" value="1" /> <label for="shiptobilling" class="checkbox">' . __ ( 'Ship to same address?' , 'woothemes' ) . '</label></p>' ;
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
echo '<h3>' . __ ( 'Shipping Address' , 'woothemes' ) . '</h3>' ;
2011-08-09 15:16:18 +00:00
2011-09-14 14:55:03 +00:00
echo '<div class="shipping_address">' ;
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Output shipping form fields
2011-09-13 16:17:52 +00:00
do_action ( 'woocommerce_before_checkout_shipping_form' , $this );
2011-09-12 15:34:29 +00:00
foreach ( $this -> shipping_fields as $key => $field ) :
$this -> checkout_form_field ( $key , $field );
2011-08-09 15:16:18 +00:00
endforeach ;
2011-09-13 16:17:52 +00:00
do_action ( 'woocommerce_after_checkout_shipping_form' , $this );
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
echo '</div>' ;
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
elseif ( $woocommerce -> cart -> ship_to_billing_address_only ()) :
2011-08-10 17:11:11 +00:00
echo '<h3>' . __ ( 'Notes/Comments' , 'woothemes' ) . '</h3>' ;
2011-08-09 15:16:18 +00:00
endif ;
2011-09-12 15:34:29 +00:00
$this -> checkout_form_field ( 'order_comments' , array (
'type' => 'textarea' ,
'class' => array ( 'notes' ),
'label' => __ ( 'Order Notes' , 'woothemes' ),
'placeholder' => __ ( 'Notes about your order, e.g. special notes for delivery.' , 'woothemes' )
));
2011-10-08 10:45:41 +00:00
do_action ( 'woocommerce_after_order_notes' , $this );
2011-08-09 15:16:18 +00:00
}
/**
2011-09-12 15:34:29 +00:00
* Outputs a checkout form field
2011-08-09 15:16:18 +00:00
*/
2011-09-12 15:34:29 +00:00
function checkout_form_field ( $key , $args ) {
2011-09-06 11:18:22 +00:00
global $woocommerce ;
2011-08-09 15:16:18 +00:00
$defaults = array (
'type' => 'input' ,
'label' => '' ,
'placeholder' => '' ,
'required' => false ,
'class' => array (),
'label_class' => array (),
'rel' => '' ,
'return' => false
);
$args = wp_parse_args ( $args , $defaults );
if ( $args [ 'required' ]) $required = ' <span class="required">*</span>' ; else $required = '' ;
if ( in_array ( 'form-row-last' , $args [ 'class' ])) $after = '<div class="clear"></div>' ; else $after = '' ;
$field = '' ;
switch ( $args [ 'type' ]) :
case " country " :
$field = '<p class="form-row ' . implode ( ' ' , $args [ 'class' ]) . ' " >
2011-09-12 15:34:29 +00:00
< label for = " '. $key .' " class = " '.implode(' ', $args['label_class'] ).' " > '.$args[' label '].$required.' </ label >
< select name = " '. $key .' " id = " '. $key .' " class = " country_to_state " rel = " '. $args['rel'] .' " >
2011-08-10 17:11:11 +00:00
< option value = " " > '.__(' Select a country & hellip ; ', ' woothemes ').' </ option > ' ;
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
foreach ( $woocommerce -> countries -> get_allowed_countries () as $ckey => $value ) :
$field .= '<option value="' . $ckey . '" ' . selected ( $this -> get_value ( $key ), $ckey , false ) . '>' . __ ( $value , 'woothemes' ) . '</option>' ;
2011-08-09 15:16:18 +00:00
endforeach ;
$field .= '</select></p>' . $after ;
break ;
case " state " :
$field = '<p class="form-row ' . implode ( ' ' , $args [ 'class' ]) . ' " >
2011-09-12 15:34:29 +00:00
< label for = " '. $key .' " class = " '.implode(' ', $args['label_class'] ).' " > '.$args[' label '].$required.' </ label > ' ;
2011-08-09 15:16:18 +00:00
$current_cc = $this -> get_value ( $args [ 'rel' ]);
2011-09-06 11:11:22 +00:00
if ( ! $current_cc ) $current_cc = $woocommerce -> customer -> get_country ();
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
$current_r = $this -> get_value ( $key );
2011-09-06 11:11:22 +00:00
if ( ! $current_r ) $current_r = $woocommerce -> customer -> get_state ();
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$states = $woocommerce -> countries -> states ;
2011-08-09 15:16:18 +00:00
if ( isset ( $states [ $current_cc ][ $current_r ] )) :
// Dropdown
2011-09-12 15:34:29 +00:00
$field .= '<select name="' . $key . '" id="' . $key . '"><option value="">' . __ ( 'Select a state…' , 'woothemes' ) . '</option>' ;
foreach ( $states [ $current_cc ] as $ckey => $value ) :
$field .= '<option value="' . $ckey . '" ' . selected ( $current_r , $ckey , false ) . '>' . __ ( $value , 'woothemes' ) . '</option>' ;
2011-08-09 15:16:18 +00:00
endforeach ;
$field .= '</select>' ;
else :
// Input
2011-09-12 15:34:29 +00:00
$field .= '<input type="text" class="input-text" value="' . $current_r . '" placeholder="' . __ ( 'State/County' , 'woothemes' ) . '" name="' . $key . '" id="' . $key . '" />' ;
2011-08-09 15:16:18 +00:00
endif ;
$field .= '</p>' . $after ;
break ;
case " textarea " :
$field = '<p class="form-row ' . implode ( ' ' , $args [ 'class' ]) . ' " >
2011-09-12 15:34:29 +00:00
< label for = " '. $key .' " class = " '.implode(' ', $args['label_class'] ).' " > '.$args[' label '].$required.' </ label >
2011-09-21 06:59:21 +00:00
< textarea name = " '. $key .' " class = " input-text " id = " '. $key .' " placeholder = " '. $args['placeholder'] .' " cols = " 5 " rows = " 2 " > '. esc_textarea( $this->get_value( $key ) ).' </ textarea >
2011-08-09 15:16:18 +00:00
</ p > ' . $after ;
2011-10-05 20:08:31 +00:00
break ;
case " checkbox " :
$field = '<p class="form-row ' . implode ( ' ' , $args [ 'class' ]) . ' " >
< input type = " '. $args['type'] .' " class = " input-checkbox " name = " '. $key .' " id = " '. $key .' " value = " 1 " '.checked($this->get_value( $key ), 1, false).' />
< label for = " '. $key .' " class = " checkbox '.implode(' ', $args['label_class'] ).' " > '.$args[' label '].$required.' </ label >
</ p > ' . $after ;
2011-08-09 15:16:18 +00:00
break ;
default :
$field = '<p class="form-row ' . implode ( ' ' , $args [ 'class' ]) . ' " >
2011-09-12 15:34:29 +00:00
< label for = " '. $key .' " class = " '.implode(' ', $args['label_class'] ).' " > '.$args[' label '].$required.' </ label >
< input type = " '. $args['type'] .' " class = " input-text " name = " '. $key .' " id = " '. $key .' " placeholder = " '. $args['placeholder'] .' " value = " '. $this->get_value ( $key ).' " />
2011-08-09 15:16:18 +00:00
</ p > ' . $after ;
break ;
endswitch ;
if ( $args [ 'return' ]) return $field ; else echo $field ;
}
2011-09-12 15:34:29 +00:00
/**
* Process the checkout after the confirm order button is pressed
*/
2011-08-09 15:16:18 +00:00
function process_checkout () {
2011-09-06 11:11:22 +00:00
global $wpdb , $woocommerce ;
$validation = & new woocommerce_validation ();
2011-08-09 15:16:18 +00:00
2011-08-11 22:39:02 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' )) define ( 'WOOCOMMERCE_CHECKOUT' , true );
2011-08-10 17:11:11 +00:00
do_action ( 'woocommerce_before_checkout_process' );
2011-08-09 15:16:18 +00:00
if ( isset ( $_POST ) && $_POST && ! isset ( $_POST [ 'login' ])) :
2011-09-06 11:11:22 +00:00
$woocommerce -> verify_nonce ( 'process_checkout' );
2011-09-14 14:55:03 +00:00
2011-09-06 11:11:22 +00:00
if ( sizeof ( $woocommerce -> cart -> cart_contents ) == 0 ) :
$woocommerce -> add_error ( sprintf ( __ ( 'Sorry, your session has expired. <a href="%s">Return to homepage →</a>' , 'woothemes' ), home_url ()) );
2011-08-09 15:16:18 +00:00
endif ;
2011-09-14 14:55:03 +00:00
do_action ( 'woocommerce_checkout_process' );
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Checkout fields (non-shipping/billing)
$this -> posted [ 'shiptobilling' ] = isset ( $_POST [ 'shiptobilling' ]) ? 1 : 0 ;
$this -> posted [ 'terms' ] = isset ( $_POST [ 'terms' ]) ? 1 : 0 ;
$this -> posted [ 'createaccount' ] = isset ( $_POST [ 'createaccount' ]) ? 1 : 0 ;
$this -> posted [ 'payment_method' ] = isset ( $_POST [ 'payment_method' ]) ? woocommerce_clean ( $_POST [ 'payment_method' ]) : '' ;
$this -> posted [ 'shipping_method' ] = isset ( $_POST [ 'shipping_method' ]) ? woocommerce_clean ( $_POST [ 'shipping_method' ]) : '' ;
$this -> posted [ 'order_comments' ] = isset ( $_POST [ 'order_comments' ]) ? woocommerce_clean ( $_POST [ 'order_comments' ]) : '' ;
2011-09-14 14:55:03 +00:00
$this -> posted [ 'account_username' ] = isset ( $_POST [ 'account_username' ]) ? woocommerce_clean ( $_POST [ 'account_username' ]) : '' ;
$this -> posted [ 'account_password' ] = isset ( $_POST [ 'account_password' ]) ? woocommerce_clean ( $_POST [ 'account_password' ]) : '' ;
$this -> posted [ 'account_password-2' ] = isset ( $_POST [ 'account_password-2' ]) ? woocommerce_clean ( $_POST [ 'account_password-2' ]) : '' ;
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
if ( $woocommerce -> cart -> ship_to_billing_address_only ()) $this -> posted [ 'shiptobilling' ] = 1 ;
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Update shipping method to posted method
2011-09-02 14:42:04 +00:00
$_SESSION [ '_chosen_shipping_method' ] = $this -> posted [ 'shipping_method' ];
2011-09-14 14:55:03 +00:00
// Update cart totals
$woocommerce -> cart -> calculate_totals ();
2011-08-09 15:16:18 +00:00
// Billing Information
2011-09-12 15:34:29 +00:00
foreach ( $this -> billing_fields as $key => $field ) :
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
$this -> posted [ $key ] = isset ( $_POST [ $key ]) ? woocommerce_clean ( $_POST [ $key ]) : '' ;
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Hook
$this -> posted [ $key ] = apply_filters ( 'woocommerce_process_checkout_field_' . $key , $this -> posted [ $key ]);
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Special handling for validation and formatting
switch ( $key ) :
2011-09-14 14:55:03 +00:00
case " billing_postcode " :
2011-09-12 15:34:29 +00:00
$this -> posted [ $key ] = strtolower ( str_replace ( ' ' , '' , $this -> posted [ $key ]));
2011-09-14 14:55:03 +00:00
if ( ! $validation -> is_postcode ( $this -> posted [ $key ], $_POST [ 'billing_country' ] )) : $woocommerce -> add_error ( $field [ 'label' ] . __ ( ' (billing) is not a valid postcode/ZIP.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
else :
2011-09-14 14:55:03 +00:00
$this -> posted [ $key ] = $validation -> format_postcode ( $this -> posted [ $key ], $_POST [ 'billing_country' ] );
2011-08-09 15:16:18 +00:00
endif ;
break ;
2011-09-14 14:55:03 +00:00
case " billing_phone " :
2011-09-12 15:34:29 +00:00
if ( ! $validation -> is_phone ( $this -> posted [ $key ] )) : $woocommerce -> add_error ( $field [ 'label' ] . __ ( ' (billing) is not a valid number.' , 'woothemes' ) ); endif ;
break ;
2011-09-14 14:55:03 +00:00
case " billing_email " :
2011-09-12 15:34:29 +00:00
if ( ! $validation -> is_email ( $this -> posted [ $key ] )) : $woocommerce -> add_error ( $field [ 'label' ] . __ ( ' (billing) is not a valid email address.' , 'woothemes' ) ); endif ;
break ;
2011-08-09 15:16:18 +00:00
endswitch ;
2011-09-12 15:34:29 +00:00
// Required
if ( isset ( $field [ 'required' ]) && $field [ 'required' ] && empty ( $this -> posted [ $key ]) ) $woocommerce -> add_error ( $field [ 'label' ] . __ ( ' (billing) is a required field.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
endforeach ;
// Shipping Information
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> needs_shipping () && ! $woocommerce -> cart -> ship_to_billing_address_only () && empty ( $this -> posted [ 'shiptobilling' ])) :
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
foreach ( $this -> shipping_fields as $key => $field ) :
if ( isset ( $_POST [ $key ] )) $this -> posted [ $key ] = woocommerce_clean ( $_POST [ $key ]); else $this -> posted [ $key ] = '' ;
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Hook
$this -> posted [ $key ] = apply_filters ( 'woocommerce_process_checkout_field_' . $key , $this -> posted [ $key ]);
2011-08-09 15:16:18 +00:00
2011-09-12 15:34:29 +00:00
// Special handling for validation and formatting
switch ( $key ) :
2011-09-14 14:55:03 +00:00
case " shipping_postcode " :
2011-09-12 15:34:29 +00:00
$this -> posted [ $key ] = strtolower ( str_replace ( ' ' , '' , $this -> posted [ $key ]));
2011-09-14 14:55:03 +00:00
if ( ! $validation -> is_postcode ( $this -> posted [ $key ], $this -> posted [ 'shipping_country' ] )) : $woocommerce -> add_error ( $field [ 'label' ] . __ ( ' (shipping) is not a valid postcode/ZIP.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
else :
2011-09-14 14:55:03 +00:00
$this -> posted [ $key ] = $validation -> format_postcode ( $this -> posted [ $key ], $this -> posted [ 'shipping_country' ] );
2011-08-09 15:16:18 +00:00
endif ;
break ;
endswitch ;
2011-09-12 15:34:29 +00:00
// Required
if ( isset ( $field [ 'required' ]) && $field [ 'required' ] && empty ( $this -> posted [ $key ]) ) $woocommerce -> add_error ( $field [ 'label' ] . __ ( ' (shipping) is a required field.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
endforeach ;
endif ;
if ( is_user_logged_in ()) :
$this -> creating_account = false ;
elseif ( isset ( $this -> posted [ 'createaccount' ]) && $this -> posted [ 'createaccount' ]) :
$this -> creating_account = true ;
elseif ( $this -> must_create_account ) :
$this -> creating_account = true ;
else :
$this -> creating_account = false ;
endif ;
2011-09-12 15:34:29 +00:00
if ( $this -> creating_account ) :
2011-08-09 15:16:18 +00:00
2011-09-14 14:55:03 +00:00
if ( empty ( $this -> posted [ 'account_username' ]) ) $woocommerce -> add_error ( __ ( 'Please enter an account username.' , 'woothemes' ) );
if ( empty ( $this -> posted [ 'account_password' ]) ) $woocommerce -> add_error ( __ ( 'Please enter an account password.' , 'woothemes' ) );
if ( $this -> posted [ 'account_password-2' ] !== $this -> posted [ 'account_password' ] ) $woocommerce -> add_error ( __ ( 'Passwords do not match.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
// Check the username
2011-09-14 14:55:03 +00:00
if ( ! validate_username ( $this -> posted [ 'account_username' ] ) ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'Invalid email/username.' , 'woothemes' ) );
2011-09-14 14:55:03 +00:00
elseif ( username_exists ( $this -> posted [ 'account_username' ] ) ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'An account is already registered with that username. Please choose another.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
endif ;
// Check the e-mail address
2011-09-14 14:55:03 +00:00
if ( email_exists ( $this -> posted [ 'billing_email' ] ) ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'An account is already registered with your email address. Please login.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
endif ;
endif ;
// Terms
2011-09-06 11:11:22 +00:00
if ( ! isset ( $_POST [ 'update_totals' ]) && empty ( $this -> posted [ 'terms' ]) && get_option ( 'woocommerce_terms_page_id' ) > 0 ) $woocommerce -> add_error ( __ ( 'You must accept our Terms & Conditions.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> needs_shipping ()) :
2011-08-09 15:16:18 +00:00
// Shipping Method
2011-09-06 11:11:22 +00:00
$available_methods = $woocommerce -> shipping -> get_available_shipping_methods ();
2011-08-09 15:16:18 +00:00
if ( ! isset ( $available_methods [ $this -> posted [ 'shipping_method' ]])) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'Invalid shipping method.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
endif ;
endif ;
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> needs_payment ()) :
2011-08-09 15:16:18 +00:00
// Payment Method
2011-09-06 11:11:22 +00:00
$available_gateways = $woocommerce -> payment_gateways -> get_available_payment_gateways ();
2011-08-09 15:16:18 +00:00
if ( ! isset ( $available_gateways [ $this -> posted [ 'payment_method' ]])) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'Invalid payment method.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
else :
// Payment Method Field Validation
$available_gateways [ $this -> posted [ 'payment_method' ]] -> validate_fields ();
endif ;
endif ;
2011-09-06 11:11:22 +00:00
if ( ! isset ( $_POST [ 'update_totals' ]) && $woocommerce -> error_count () == 0 ) :
2011-08-09 15:16:18 +00:00
$user_id = get_current_user_id ();
while ( 1 ) :
// Create customer account and log them in
if ( $this -> creating_account && ! $user_id ) :
$reg_errors = new WP_Error ();
2011-09-14 14:55:03 +00:00
do_action ( 'register_post' , $this -> posted [ 'billing_email' ], $this -> posted [ 'billing_email' ], $reg_errors );
$errors = apply_filters ( 'registration_errors' , $reg_errors , $this -> posted [ 'billing_email' ], $this -> posted [ 'billing_email' ] );
2011-08-09 15:16:18 +00:00
// if there are no errors, let's create the user account
if ( ! $reg_errors -> get_error_code () ) :
2011-09-14 14:55:03 +00:00
$user_pass = $this -> posted [ 'account_password' ];
$user_id = wp_create_user ( $this -> posted [ 'account_username' ], $user_pass , $this -> posted [ 'billing_email' ] );
2011-08-09 15:16:18 +00:00
if ( ! $user_id ) {
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( sprintf ( __ ( '<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !' , 'woothemes' ), get_option ( 'admin_email' )));
2011-08-09 15:16:18 +00:00
break ;
}
// Change role
wp_update_user ( array ( 'ID' => $user_id , 'role' => 'customer' ) ) ;
// send the user a confirmation and their login details
wp_new_user_notification ( $user_id , $user_pass );
// set the WP login cookie
$secure_cookie = is_ssl () ? true : false ;
wp_set_auth_cookie ( $user_id , true , $secure_cookie );
else :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( $reg_errors -> get_error_message () );
2011-08-09 15:16:18 +00:00
break ;
endif ;
endif ;
// Get shipping/billing
if ( ! empty ( $this -> posted [ 'shiptobilling' ]) ) :
2011-09-14 14:55:03 +00:00
$shipping_first_name = $this -> posted [ 'billing_first_name' ];
$shipping_last_name = $this -> posted [ 'billing_last_name' ];
$shipping_company = $this -> posted [ 'billing_company' ];
$shipping_address_1 = $this -> posted [ 'billing_address' ];
$shipping_address_2 = $this -> posted [ 'billing_address-2' ];
$shipping_city = $this -> posted [ 'billing_city' ];
$shipping_state = $this -> posted [ 'billing_state' ];
$shipping_postcode = $this -> posted [ 'billing_postcode' ];
$shipping_country = $this -> posted [ 'billing_country' ];
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
elseif ( $woocommerce -> cart -> needs_shipping () ) :
2011-08-09 15:16:18 +00:00
2011-09-14 14:55:03 +00:00
$shipping_first_name = $this -> posted [ 'shipping_first_name' ];
$shipping_last_name = $this -> posted [ 'shipping_last_name' ];
$shipping_company = $this -> posted [ 'shipping_company' ];
$shipping_address_1 = $this -> posted [ 'shipping_address' ];
$shipping_address_2 = $this -> posted [ 'shipping_address-2' ];
$shipping_city = $this -> posted [ 'shipping_city' ];
$shipping_state = $this -> posted [ 'shipping_state' ];
$shipping_postcode = $this -> posted [ 'shipping_postcode' ];
$shipping_country = $this -> posted [ 'shipping_country' ];
2011-08-09 15:16:18 +00:00
endif ;
// Save billing/shipping to user meta fields
if ( $user_id > 0 ) :
2011-08-29 16:09:42 +00:00
2011-09-14 14:55:03 +00:00
update_user_meta ( $user_id , 'billing_first_name' , $this -> posted [ 'billing_first_name' ] );
update_user_meta ( $user_id , 'billing_last_name' , $this -> posted [ 'billing_last_name' ] );
update_user_meta ( $user_id , 'billing_company' , $this -> posted [ 'billing_company' ] );
update_user_meta ( $user_id , 'billing_email' , $this -> posted [ 'billing_email' ] );
update_user_meta ( $user_id , 'billing_address' , $this -> posted [ 'billing_address' ] );
update_user_meta ( $user_id , 'billing_address-2' , $this -> posted [ 'billing_address-2' ] );
update_user_meta ( $user_id , 'billing_city' , $this -> posted [ 'billing_city' ] );
update_user_meta ( $user_id , 'billing_postcode' , $this -> posted [ 'billing_postcode' ] );
update_user_meta ( $user_id , 'billing_country' , $this -> posted [ 'billing_country' ] );
update_user_meta ( $user_id , 'billing_state' , $this -> posted [ 'billing_state' ] );
update_user_meta ( $user_id , 'billing_phone' , $this -> posted [ 'billing_phone' ] );
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
if ( empty ( $this -> posted [ 'shiptobilling' ]) && $woocommerce -> cart -> needs_shipping () ) :
2011-09-14 14:55:03 +00:00
update_user_meta ( $user_id , 'shipping_first_name' , $this -> posted [ 'shipping_first_name' ] );
update_user_meta ( $user_id , 'shipping_last_name' , $this -> posted [ 'shipping_last_name' ] );
update_user_meta ( $user_id , 'shipping_company' , $this -> posted [ 'shipping_company' ] );
update_user_meta ( $user_id , 'shipping_address' , $this -> posted [ 'shipping_address' ] );
update_user_meta ( $user_id , 'shipping_address-2' , $this -> posted [ 'shipping_address-2' ] );
update_user_meta ( $user_id , 'shipping_city' , $this -> posted [ 'shipping_city' ] );
update_user_meta ( $user_id , 'shipping_postcode' , $this -> posted [ 'shipping_postcode' ] );
update_user_meta ( $user_id , 'shipping_country' , $this -> posted [ 'shipping_country' ] );
update_user_meta ( $user_id , 'shipping_state' , $this -> posted [ 'shipping_state' ] );
2011-09-06 11:11:22 +00:00
elseif ( $this -> posted [ 'shiptobilling' ] && $woocommerce -> cart -> needs_shipping () ) :
2011-09-14 14:55:03 +00:00
update_user_meta ( $user_id , 'shipping_first_name' , $this -> posted [ 'billing_first_name' ] );
update_user_meta ( $user_id , 'shipping_last_name' , $this -> posted [ 'billing_last_name' ] );
update_user_meta ( $user_id , 'shipping_company' , $this -> posted [ 'billing_company' ] );
update_user_meta ( $user_id , 'shipping_address' , $this -> posted [ 'billing_address' ] );
update_user_meta ( $user_id , 'shipping_address-2' , $this -> posted [ 'billing_address-2' ] );
update_user_meta ( $user_id , 'shipping_city' , $this -> posted [ 'billing_city' ] );
update_user_meta ( $user_id , 'shipping_postcode' , $this -> posted [ 'billing_postcode' ] );
update_user_meta ( $user_id , 'shipping_country' , $this -> posted [ 'billing_country' ] );
update_user_meta ( $user_id , 'shipping_state' , $this -> posted [ 'billing_state' ] );
2011-08-09 15:16:18 +00:00
endif ;
2011-09-12 15:34:29 +00:00
do_action ( 'woocommerce_checkout_update_user_meta' , $user_id , $this -> posted );
2011-08-09 15:16:18 +00:00
endif ;
// 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 last time.
2011-08-10 17:11:11 +00:00
$_tax = new woocommerce_tax ();
2011-08-09 15:16:18 +00:00
$order_data = array (
'post_type' => 'shop_order' ,
'post_title' => 'Order – ' . date ( 'F j, Y @ h:i A' ),
'post_status' => 'publish' ,
'post_excerpt' => $this -> posted [ 'order_comments' ],
'post_author' => 1
);
2011-08-29 16:09:42 +00:00
2011-08-09 15:16:18 +00:00
// Cart items
$order_items = array ();
2011-09-06 11:11:22 +00:00
foreach ( $woocommerce -> cart -> cart_contents as $cart_item_key => $values ) :
2011-08-09 15:16:18 +00:00
$_product = $values [ 'data' ];
// Calc item tax to store
$rate = '' ;
if ( $_product -> is_taxable ()) :
2011-10-07 22:24:11 +00:00
$rate = $_tax -> get_rate ( $_product -> get_tax_class () );
2011-08-09 15:16:18 +00:00
endif ;
2011-08-20 15:41:42 +00:00
// Store any item meta data
$item_meta = array ();
// Variations meta
if ( $values [ 'variation' ] && is_array ( $values [ 'variation' ])) :
foreach ( $values [ 'variation' ] as $key => $value ) :
2011-10-16 20:56:18 +00:00
$item_meta [ esc_attr ( str_replace ( 'attribute_' , '' , $key )) ] = $value ;
2011-08-20 15:41:42 +00:00
endforeach ;
endif ;
2011-08-29 16:09:42 +00:00
2011-08-20 15:41:42 +00:00
// Run filter
2011-09-21 16:58:05 +00:00
$item_meta = apply_filters ( 'woocommerce_order_item_meta' , $item_meta , $values );
2011-08-20 15:41:42 +00:00
2011-08-09 15:16:18 +00:00
$order_items [] = apply_filters ( 'new_order_item' , array (
'id' => $values [ 'product_id' ],
'variation_id' => $values [ 'variation_id' ],
'name' => $_product -> get_title (),
'qty' => ( int ) $values [ 'quantity' ],
2011-08-11 22:54:26 +00:00
'cost' => $_product -> get_price_excluding_tax (),
2011-08-20 15:41:42 +00:00
'taxrate' => $rate ,
'item_meta' => $item_meta
), $values );
2011-08-09 15:16:18 +00:00
// Check stock levels
if ( $_product -> managing_stock ()) :
if ( ! $_product -> is_in_stock () || ! $_product -> has_enough_stock ( $values [ 'quantity' ] )) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( sprintf ( __ ( 'Sorry, we do not have enough "%s" in stock to fulfil your order. Please edit your cart and try again. We apologise for any inconvenience caused.' , 'woothemes' ), $_product -> get_title () ) );
2011-08-09 15:16:18 +00:00
break ;
endif ;
else :
if ( ! $_product -> is_in_stock ()) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( sprintf ( __ ( 'Sorry, we do not have enough "%s" in stock to fulfil your order. Please edit your cart and try again. We apologise for any inconvenience caused.' , 'woothemes' ), $_product -> get_title () ) );
2011-08-09 15:16:18 +00:00
break ;
endif ;
endif ;
endforeach ;
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> error_count () > 0 ) break ;
2011-08-09 15:16:18 +00:00
// Insert or update the post data
if ( isset ( $_SESSION [ 'order_awaiting_payment' ]) && $_SESSION [ 'order_awaiting_payment' ] > 0 ) :
$order_id = ( int ) $_SESSION [ 'order_awaiting_payment' ];
$order_data [ 'ID' ] = $order_id ;
wp_update_post ( $order_data );
2011-08-29 16:09:42 +00:00
do_action ( 'woocommerce_resume_order' , $order_id );
2011-08-09 15:16:18 +00:00
else :
$order_id = wp_insert_post ( $order_data );
if ( is_wp_error ( $order_id )) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( 'Error: Unable to create order. Please try again.' );
2011-08-09 15:16:18 +00:00
break ;
2011-08-15 16:48:24 +00:00
else :
// Inserted successfully
do_action ( 'woocommerce_new_order' , $order_id );
2011-08-09 15:16:18 +00:00
endif ;
endif ;
2011-10-09 13:23:20 +00:00
2011-09-02 14:42:04 +00:00
// Get better formatted shipping method (title/label)
$shipping_method = $this -> posted [ 'shipping_method' ];
if ( isset ( $available_methods ) && isset ( $available_methods [ $this -> posted [ 'shipping_method' ]])) :
$shipping_method = $available_methods [ $this -> posted [ 'shipping_method' ]] -> title ;
endif ;
2011-08-29 16:09:42 +00:00
// Update order meta
2011-09-14 14:55:03 +00:00
update_post_meta ( $order_id , '_billing_first_name' , $this -> posted [ 'billing_first_name' ]);
update_post_meta ( $order_id , '_billing_last_name' , $this -> posted [ 'billing_last_name' ]);
update_post_meta ( $order_id , '_billing_company' , $this -> posted [ 'billing_company' ]);
update_post_meta ( $order_id , '_billing_address_1' , $this -> posted [ 'billing_address' ]);
update_post_meta ( $order_id , '_billing_address_2' , $this -> posted [ 'billing_address-2' ]);
update_post_meta ( $order_id , '_billing_city' , $this -> posted [ 'billing_city' ]);
update_post_meta ( $order_id , '_billing_postcode' , $this -> posted [ 'billing_postcode' ]);
update_post_meta ( $order_id , '_billing_country' , $this -> posted [ 'billing_country' ]);
update_post_meta ( $order_id , '_billing_state' , $this -> posted [ 'billing_state' ]);
update_post_meta ( $order_id , '_billing_email' , $this -> posted [ 'billing_email' ]);
update_post_meta ( $order_id , '_billing_phone' , $this -> posted [ 'billing_phone' ]);
2011-08-29 16:09:42 +00:00
update_post_meta ( $order_id , '_shipping_first_name' , $shipping_first_name );
update_post_meta ( $order_id , '_shipping_last_name' , $shipping_last_name );
update_post_meta ( $order_id , '_shipping_company' , $shipping_company );
update_post_meta ( $order_id , '_shipping_address_1' , $shipping_address_1 );
update_post_meta ( $order_id , '_shipping_address_2' , $shipping_address_2 );
update_post_meta ( $order_id , '_shipping_city' , $shipping_city );
update_post_meta ( $order_id , '_shipping_postcode' , $shipping_postcode );
update_post_meta ( $order_id , '_shipping_country' , $shipping_country );
update_post_meta ( $order_id , '_shipping_state' , $shipping_state );
2011-09-02 14:42:04 +00:00
update_post_meta ( $order_id , '_shipping_method' , $shipping_method );
2011-08-29 16:09:42 +00:00
update_post_meta ( $order_id , '_payment_method' , $this -> posted [ 'payment_method' ]);
2011-09-06 11:11:22 +00:00
update_post_meta ( $order_id , '_order_subtotal' , number_format ( $woocommerce -> cart -> subtotal_ex_tax , 2 , '.' , '' ));
update_post_meta ( $order_id , '_order_shipping' , number_format ( $woocommerce -> cart -> shipping_total , 2 , '.' , '' ));
update_post_meta ( $order_id , '_order_discount' , number_format ( $woocommerce -> cart -> discount_total , 2 , '.' , '' ));
update_post_meta ( $order_id , '_order_tax' , number_format ( $woocommerce -> cart -> tax_total , 2 , '.' , '' ));
update_post_meta ( $order_id , '_order_shipping_tax' , number_format ( $woocommerce -> cart -> shipping_tax_total , 2 , '.' , '' ));
update_post_meta ( $order_id , '_order_total' , number_format ( $woocommerce -> cart -> total , 2 , '.' , '' ));
2011-08-29 16:09:42 +00:00
update_post_meta ( $order_id , '_order_key' , uniqid ( 'order_' ) );
update_post_meta ( $order_id , '_customer_user' , ( int ) $user_id );
update_post_meta ( $order_id , '_order_items' , $order_items );
2011-09-12 15:34:29 +00:00
do_action ( 'woocommerce_checkout_update_order_meta' , $order_id , $this -> posted );
2011-08-29 16:09:42 +00:00
// Order status
2011-08-09 15:16:18 +00:00
wp_set_object_terms ( $order_id , 'pending' , 'shop_order_status' );
2011-08-29 16:09:42 +00:00
// Discount code meta
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> applied_coupons ) update_post_meta ( $order_id , 'coupons' , implode ( ', ' , $woocommerce -> cart -> applied_coupons ));
2011-08-09 15:16:18 +00:00
2011-10-09 13:23:20 +00:00
// Order is saved
do_action ( 'woocommerce_checkout_order_processed' , $order_id , $this -> posted );
// Process payment
2011-08-10 17:11:11 +00:00
$order = & new woocommerce_order ( $order_id );
2011-10-09 13:23:20 +00:00
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> needs_payment ()) :
2011-08-09 15:16:18 +00:00
// Store Order ID in session so it can be re-used after payment failure
$_SESSION [ 'order_awaiting_payment' ] = $order_id ;
// Process Payment
$result = $available_gateways [ $this -> posted [ 'payment_method' ]] -> process_payment ( $order_id );
// Redirect to success/confirmation/payment page
if ( $result [ 'result' ] == 'success' ) :
if ( is_ajax ()) :
ob_clean ();
echo json_encode ( $result );
exit ;
else :
wp_safe_redirect ( $result [ 'redirect' ] );
exit ;
endif ;
endif ;
else :
// No payment was required for order
$order -> payment_complete ();
// Empty the Cart
2011-09-06 11:11:22 +00:00
$woocommerce -> cart -> empty_cart ();
2011-08-09 15:16:18 +00:00
// Redirect to success/confirmation/payment page
if ( is_ajax ()) :
ob_clean ();
2011-08-10 17:11:11 +00:00
echo json_encode ( array ( 'redirect' => get_permalink ( get_option ( 'woocommerce_thanks_page_id' ))) );
2011-08-09 15:16:18 +00:00
exit ;
else :
2011-08-10 17:11:11 +00:00
wp_safe_redirect ( get_permalink ( get_option ( 'woocommerce_thanks_page_id' )) );
2011-08-09 15:16:18 +00:00
exit ;
endif ;
endif ;
// Break out of loop
break ;
endwhile ;
endif ;
// If we reached this point then there were errors
if ( is_ajax ()) :
ob_clean ();
2011-09-06 11:11:22 +00:00
$woocommerce -> show_messages ();
2011-08-09 15:16:18 +00:00
exit ;
else :
2011-09-06 11:11:22 +00:00
$woocommerce -> show_messages ();
2011-08-09 15:16:18 +00:00
endif ;
endif ;
}
/** Gets the value either from the posted data, or from the users meta data */
function get_value ( $input ) {
if ( isset ( $this -> posted [ $input ] ) && ! empty ( $this -> posted [ $input ])) :
return $this -> posted [ $input ];
elseif ( is_user_logged_in ()) :
if ( get_user_meta ( get_current_user_id (), $input , true )) return get_user_meta ( get_current_user_id (), $input , true );
$current_user = wp_get_current_user ();
switch ( $input ) :
2011-09-14 14:55:03 +00:00
case " billing_email " :
2011-08-09 15:16:18 +00:00
return $current_user -> user_email ;
break ;
endswitch ;
endif ;
}
}