woocommerce/classes/checkout.class.php

649 lines
24 KiB
PHP
Raw Normal View History

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 $checkout_fields;
2011-08-09 15:16:18 +00:00
var $must_create_account;
var $creating_account;
var $localisation;
2011-08-09 15:16:18 +00:00
/** constructor */
2011-09-06 11:18:22 +00:00
function __construct () {
global $woocommerce;
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 = (get_option('woocommerce_enable_guest_checkout')=='yes' || is_user_logged_in()) ? false : true;
// Define all Checkout fields
$this->checkout_fields['billing'] = $woocommerce->countries->get_address_fields( $this->get_value('billing_country'), 'billing_' );
$this->checkout_fields['shipping'] = $woocommerce->countries->get_address_fields( $this->get_value('shipping_country'), 'shipping_' );
$this->checkout_fields['account'] = array(
'account_username' => array(
'type' => 'text',
2012-01-05 11:31:22 +00:00
'label' => __('Account username', 'woocommerce'),
'placeholder' => __('Username', 'woocommerce')
),
'account_password' => array(
'type' => 'password',
2012-01-05 11:31:22 +00:00
'label' => __('Account password', 'woocommerce'),
'placeholder' => __('Password', 'woocommerce'),
'class' => array('form-row-first')
),
'account_password-2' => array(
'type' => 'password',
2012-01-05 11:31:22 +00:00
'label' => __('Account password', 'woocommerce'),
'placeholder' => __('Password', 'woocommerce'),
'class' => array('form-row-last'),
'label_class' => array('hidden')
)
);
$this->checkout_fields['order'] = array(
'order_comments' => array(
'type' => 'textarea',
'class' => array('notes'),
2012-01-05 11:31:22 +00:00
'label' => __('Order Notes', 'woocommerce'),
'placeholder' => __('Notes about your order, e.g. special notes for delivery.', 'woocommerce')
)
);
$this->checkout_fields = apply_filters('woocommerce_checkout_fields', $this->checkout_fields);
2011-08-09 15:16:18 +00:00
}
/** Output the billing information form */
function checkout_form_billing() {
global $woocommerce;
2011-08-09 15:16:18 +00:00
if ($woocommerce->cart->ship_to_billing_address_only()) :
2012-01-05 11:31:22 +00:00
echo '<h3>'.__('Billing &amp Shipping', 'woocommerce').'</h3>';
2011-08-09 15:16:18 +00:00
else :
2012-01-05 11:31:22 +00:00
echo '<h3>'.__('Billing Address', 'woocommerce').'</h3>';
2011-08-09 15:16:18 +00:00
endif;
// Output billing form fields
2011-09-13 16:17:52 +00:00
do_action('woocommerce_before_checkout_billing_form', $this);
foreach ($this->checkout_fields['billing'] as $key => $field) :
woocommerce_form_field( $key, $field, $this->get_value( $key ) );
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
// Registration Form Fields
if (!is_user_logged_in() && get_option('woocommerce_enable_signup_and_login_from_checkout')=="yes") :
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
if (get_option('woocommerce_enable_guest_checkout')=='yes') :
2011-08-09 15:16:18 +00:00
2012-01-05 11:31:22 +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?', 'woocommerce').'</label></p>';
2011-08-09 15:16:18 +00:00
endif;
echo '<div class="create-account">';
2012-01-05 11:31:22 +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.', 'woocommerce').'</p>';
2011-08-27 11:53:46 +00:00
foreach ($this->checkout_fields['account'] as $key => $field) :
woocommerce_form_field( $key, $field, $this->get_value( $key ) );
endforeach;
2011-08-09 15:16:18 +00:00
echo '</div>';
2011-08-09 15:16:18 +00:00
endif;
}
/** Output the shipping information form */
function checkout_form_shipping() {
global $woocommerce;
2011-08-09 15:16:18 +00:00
// Shipping Details
if ($woocommerce->cart->needs_shipping() && !$woocommerce->cart->ship_to_billing_address_only()) :
2011-08-09 15:16:18 +00:00
2011-11-28 16:20:39 +00:00
if (!isset($_POST) || !$_POST) :
$shiptobilling = (get_option('woocommerce_ship_to_same_address')=='yes') ? 1 : 0;
$shiptobilling = apply_filters('woocommerce_shiptobilling_default', $shiptobilling);
else :
$shiptobilling = $this->get_value('shiptobilling');
endif;
2012-01-05 11:31:22 +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?', 'woocommerce').'</label></p>';
2011-08-09 15:16:18 +00:00
2012-01-05 11:31:22 +00:00
echo '<h3>'.__('Shipping Address', 'woocommerce').'</h3>';
2011-08-09 15:16:18 +00:00
echo'<div class="shipping_address">';
2011-08-09 15:16:18 +00:00
// Output shipping form fields
2011-09-13 16:17:52 +00:00
do_action('woocommerce_before_checkout_shipping_form', $this);
foreach ($this->checkout_fields['shipping'] as $key => $field) :
woocommerce_form_field( $key, $field, $this->get_value( $key ) );
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
echo '</div>';
2011-08-09 15:16:18 +00:00
endif;
2011-11-16 10:18:46 +00:00
do_action('woocommerce_before_order_notes', $this);
if (get_option('woocommerce_enable_order_comments')!='no') :
if ($woocommerce->cart->ship_to_billing_address_only()) :
2012-01-05 11:31:22 +00:00
echo '<h3>'.__('Additional Information', 'woocommerce').'</h3>';
2011-11-16 10:18:46 +00:00
endif;
foreach ($this->checkout_fields['order'] as $key => $field) :
woocommerce_form_field( $key, $field, $this->get_value( $key ) );
endforeach;
2011-11-16 10:18:46 +00:00
endif;
2011-10-08 10:45:41 +00:00
do_action('woocommerce_after_order_notes', $this);
2011-08-09 15:16:18 +00:00
}
/**
* Process the checkout after the confirm order button is pressed
*/
2011-08-09 15:16:18 +00:00
function process_checkout() {
global $wpdb, $woocommerce;
2012-01-12 00:54:45 +00:00
if (!defined('WOOCOMMERCE_CHECKOUT')) define('WOOCOMMERCE_CHECKOUT', true);
2012-01-12 00:54:45 +00:00
$woocommerce->verify_nonce('process_checkout');
do_action('woocommerce_before_checkout_process');
2012-01-12 00:54:45 +00:00
if (sizeof($woocommerce->cart->get_cart())==0) :
$woocommerce->add_error( sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage &rarr;</a>', 'woocommerce'), home_url()) );
endif;
do_action('woocommerce_checkout_process');
2012-01-12 00:54:45 +00:00
// Checkout fields (not defined in checkout_fields)
$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']) : '';
// Ship to billing only option
if ($woocommerce->cart->ship_to_billing_address_only()) $this->posted['shiptobilling'] = 1;
// Update customer shipping and payment method to posted method
$_SESSION['_chosen_shipping_method'] = $this->posted['shipping_method'];
$_SESSION['_chosen_payment_method'] = $this->posted['payment_method'];
// Update cart totals
$woocommerce->cart->calculate_totals();
// Note if we skip shipping
$skipped_shipping = false;
// Get validation class
$validation = $woocommerce->validation();
2012-01-12 00:54:45 +00:00
// Get posted checkout_fields and do validation
foreach ($this->checkout_fields as $fieldset_key => $fieldset) :
2012-01-12 00:54:45 +00:00
// Skip shipping if its not needed
if ($fieldset_key=='shipping' && (!$woocommerce->cart->needs_shipping() || $woocommerce->cart->ship_to_billing_address_only() || $this->posted['shiptobilling'])) :
$skipped_shipping = true;
continue;
endif;
2012-01-12 00:54:45 +00:00
foreach ($fieldset as $key => $field) :
2012-01-12 00:54:45 +00:00
if (!isset($field['type'])) $field['type'] = 'text';
2012-01-12 00:54:45 +00:00
// Get Value
switch ($field['type']) :
case "checkbox" :
$this->posted[$key] = isset($_POST[$key]) ? 1 : 0;
break;
default :
$this->posted[$key] = isset($_POST[$key]) ? woocommerce_clean($_POST[$key]) : '';
break;
endswitch;
// Hook to allow modification of value
$this->posted[$key] = apply_filters('woocommerce_process_checkout_field_' . $key, $this->posted[$key]);
// Validation: Required fields
if ( isset($field['required']) && $field['required'] && empty($this->posted[$key]) ) $woocommerce->add_error( $field['label'] . ' ' . __('is a required field.', 'woocommerce') );
if (!empty($this->posted[$key])) :
// Special handling for validation and formatting
switch ($key) :
case "billing_postcode" :
case "shipping_postcode" :
$this->posted[$key] = strtolower(str_replace(' ', '', $this->posted[$key]));
if (!$validation->is_postcode( $this->posted[$key], $_POST['billing_country'] )) : $woocommerce->add_error( $field['label'] . __(' (billing) is not a valid postcode/ZIP.', 'woocommerce') );
2012-01-12 00:54:45 +00:00
else :
$this->posted[$key] = $validation->format_postcode( $this->posted[$key], $_POST['billing_country'] );
2012-01-12 00:54:45 +00:00
endif;
break;
2012-01-12 00:54:45 +00:00
case "billing_phone" :
if (!$validation->is_phone( $this->posted[$key] )) : $woocommerce->add_error( $field['label'] . ' ' . __('is not a valid number.', 'woocommerce') ); endif;
2012-01-12 00:54:45 +00:00
break;
case "billing_email" :
if (!$validation->is_email( $this->posted[$key] )) : $woocommerce->add_error( $field['label'] . ' ' . __('is not a valid email address.', 'woocommerce') ); endif;
break;
endswitch;
2012-01-12 00:54:45 +00:00
endif;
2011-10-26 10:03:24 +00:00
endforeach;
2012-01-12 00:54:45 +00:00
endforeach;
// Update customer location to posted location so we can correctly check available shipping methods
$woocommerce->customer->set_country( $this->posted['billing_country'] );
$woocommerce->customer->set_state( $this->posted['billing_state'] );
$woocommerce->customer->set_postcode( $this->posted['billing_postcode'] );
// Shipping Information
if (!$skipped_shipping) :
// Update customer location to posted location so we can correctly check available shipping methods
2012-01-12 00:54:45 +00:00
$woocommerce->customer->set_shipping_country( $this->posted['shipping_country'] );
$woocommerce->customer->set_shipping_state( $this->posted['shipping_state'] );
$woocommerce->customer->set_shipping_postcode( $this->posted['shipping_postcode'] );
2012-01-12 00:54:45 +00:00
else :
// Update customer location to posted location so we can correctly check available shipping methods
$woocommerce->customer->set_shipping_country( $this->posted['billing_country'] );
$woocommerce->customer->set_shipping_state( $this->posted['billing_state'] );
$woocommerce->customer->set_shipping_postcode( $this->posted['billing_postcode'] );
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
endif;
2012-01-12 00:54:45 +00:00
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;
if ($this->creating_account) :
if ( empty($this->posted['account_username']) ) $woocommerce->add_error( __('Please enter an account username.', 'woocommerce') );
if ( empty($this->posted['account_password']) ) $woocommerce->add_error( __('Please enter an account password.', 'woocommerce') );
if ( $this->posted['account_password-2'] !== $this->posted['account_password'] ) $woocommerce->add_error( __('Passwords do not match.', 'woocommerce') );
// Check the username
if ( !validate_username( $this->posted['account_username'] ) ) :
$woocommerce->add_error( __('Invalid email/username.', 'woocommerce') );
elseif ( username_exists( $this->posted['account_username'] ) ) :
$woocommerce->add_error( __('An account is already registered with that username. Please choose another.', 'woocommerce') );
endif;
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
// Check the e-mail address
if ( email_exists( $this->posted['billing_email'] ) ) :
$woocommerce->add_error( __('An account is already registered with your email address. Please login.', 'woocommerce') );
endif;
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
endif;
// Terms
if (!isset($_POST['woocommerce_checkout_update_totals']) && empty($this->posted['terms']) && woocommerce_get_page_id('terms')>0 ) $woocommerce->add_error( __('You must accept our Terms &amp; Conditions.', 'woocommerce') );
if ($woocommerce->cart->needs_shipping()) :
2012-01-12 00:54:45 +00:00
// Shipping Method
$available_methods = $woocommerce->shipping->get_available_shipping_methods();
if (!isset($available_methods[$this->posted['shipping_method']])) :
$woocommerce->add_error( __('Invalid shipping method.', 'woocommerce') );
endif;
endif;
if ($woocommerce->cart->needs_payment()) :
// Payment Method
$available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
if (!isset($available_gateways[$this->posted['payment_method']])) :
$woocommerce->add_error( __('Invalid payment method.', 'woocommerce') );
else :
// Payment Method Field Validation
$available_gateways[$this->posted['payment_method']]->validate_fields();
endif;
2012-01-12 00:54:45 +00:00
endif;
do_action( 'woocommerce_after_checkout_validation', $this->posted );
if (!isset($_POST['woocommerce_checkout_update_totals']) && $woocommerce->error_count()==0) :
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
$user_id = get_current_user_id();
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
while (1) :
2012-01-12 00:54:45 +00:00
// Create customer account and log them in
if ($this->creating_account && !$user_id) :
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
$reg_errors = new WP_Error();
do_action('woocommerce_register_post', $this->posted['account_username'], $this->posted['billing_email'], $reg_errors);
$errors = apply_filters( 'woocommerce_registration_errors', $reg_errors, $this->posted['account_username'], $this->posted['billing_email'] );
2012-01-12 00:54:45 +00:00
// if there are no errors, let's create the user account
if ( !$reg_errors->get_error_code() ) :
$user_pass = $this->posted['account_password'];
$user_id = wp_create_user( $this->posted['account_username'], $user_pass, $this->posted['billing_email'] );
if ( !$user_id ) :
$woocommerce->add_error( '<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Couldn&#8217;t register you... please contact us if you continue to have problems.', 'woocommerce') );
break;
endif;
// Change role
wp_update_user( array ('ID' => $user_id, 'role' => 'customer') ) ;
// send the user a confirmation and their login details
$mailer = $woocommerce->mailer();
$mailer->customer_new_account( $user_id, $password );
// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie($user_id, true, $secure_cookie);
else :
$woocommerce->add_error( $reg_errors->get_error_message() );
break;
endif;
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +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.
$_tax = new woocommerce_tax();
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
$order_data = array(
'post_type' => 'shop_order',
'post_title' => 'Order &ndash; '.date('F j, Y @ h:i A'),
'post_status' => 'publish',
'post_excerpt' => $this->posted['order_comments'],
'post_author' => 1
);
// Cart items
$order_items = array();
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) :
$_product = $values['data'];
// Store any item meta data - item meta class lets plugins add item meta in a standardized way
$item_meta = new order_item_meta();
$item_meta->new_order_item( $values );
// Store variation data in meta so admin can view it
if ($values['variation'] && is_array($values['variation'])) :
foreach ($values['variation'] as $key => $value) :
$item_meta->add( esc_attr(str_replace('attribute_', '', $key)), $value );
endforeach;
endif;
2012-01-12 00:54:45 +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'],
'item_meta' => $item_meta->meta,
'base_tax' => number_format($values['base_tax'], 2, '.', ''), // Base tax (unit, before discounts)
'base_cost' => number_format($values['base_cost'], 2, '.', ''), // Base price (unit, before discounts)
'line_cost' => number_format($values['line_cost'], 2, '.', ''), // Discounted line cost
'line_tax' => number_format($values['line_tax'], 2, '.', ''), // Tax for the line (total)
'tax_status' => $_product->get_tax_status(), // Taxble, shipping, none
'tax_class' => $_product->get_tax_class() // Tax class (adjusted by filters)
), $values);
endforeach;
2012-01-12 00:54:45 +00:00
// Check order items for errors
do_action('woocommerce_check_new_order_items', $order_items);
2012-01-12 00:54:45 +00:00
if ($woocommerce->error_count()>0) break;
// Insert or update the post data
$create_new_order = true;
if (isset($_SESSION['order_awaiting_payment']) && $_SESSION['order_awaiting_payment'] > 0) :
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
$order_id = (int) $_SESSION['order_awaiting_payment'];
2011-12-07 17:25:08 +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';
if ( $order_status == 'pending' ) :
2011-12-07 17:25:08 +00:00
2012-01-12 00:54:45 +00:00
// Resume the unpaid order
$order_data['ID'] = $order_id;
wp_update_post( $order_data );
do_action('woocommerce_resume_order', $order_id);
2011-12-07 17:25:08 +00:00
2012-01-12 00:54:45 +00:00
$create_new_order = false;
2012-01-12 00:54:45 +00:00
endif;
2012-01-12 00:54:45 +00:00
endif;
if ($create_new_order) :
$order_id = wp_insert_post( $order_data );
2012-01-12 00:54:45 +00:00
if (is_wp_error($order_id)) :
$woocommerce->add_error( 'Error: Unable to create order. Please try again.' );
break;
else :
// Inserted successfully
do_action('woocommerce_new_order', $order_id);
endif;
endif;
// Get better formatted billing method (title)
$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']]->label;
endif;
// Get better formatted shipping method (title/label)
$payment_method = $this->posted['payment_method'];
if (isset($available_gateways) && isset($available_gateways[$this->posted['payment_method']])) :
$payment_method = $available_gateways[$this->posted['payment_method']]->title;
endif;
// UPDATE ORDER META
// Save billing and shipping first, also save to user meta if logged in
if ($this->checkout_fields['billing']) :
foreach ($this->checkout_fields['billing'] as $key => $field) :
2012-01-12 00:54:45 +00:00
// Post
update_post_meta( $order_id, '_' . $key, $this->posted[$key] );
2012-01-12 00:54:45 +00:00
// User
if ($user_id>0) :
update_user_meta( $user_id, $key, $this->posted[$key] );
endif;
2012-01-12 00:54:45 +00:00
endforeach;
endif;
if ($this->checkout_fields['shipping'] && $woocommerce->cart->needs_shipping()) :
foreach ($this->checkout_fields['shipping'] as $key => $field) :
2012-01-12 00:54:45 +00:00
if ($this->posted['shiptobilling']) :
$field_key = str_replace('shipping_', 'billing_', $key);
// Post
2012-01-12 00:54:45 +00:00
update_post_meta( $order_id, '_' . $key, $this->posted[$field_key] );
// User
if ($user_id>0) :
2012-01-12 00:54:45 +00:00
update_user_meta( $user_id, $key, $this->posted[$field_key] );
endif;
2012-01-12 00:54:45 +00:00
else :
// Post
update_post_meta( $order_id, '_' . $key, $this->posted[$key] );
2012-01-12 00:54:45 +00:00
// User
if ($user_id>0) :
update_user_meta( $user_id, $key, $this->posted[$key] );
endif;
2012-01-12 00:54:45 +00:00
endif;
2011-12-31 19:03:41 +00:00
endforeach;
2012-01-12 00:54:45 +00:00
endif;
// Save any other user meta
if ($user_id) do_action('woocommerce_checkout_update_user_meta', $user_id, $this->posted);
2012-01-12 00:54:45 +00:00
// Put order taxes into an array to store
$order_taxes = array();
if (is_array($woocommerce->cart->taxes) && sizeof($woocommerce->cart->taxes)>0) foreach ( $woocommerce->cart->taxes as $key => $tax ) :
2011-12-31 19:03:41 +00:00
2012-01-12 00:54:45 +00:00
$is_compound = ($woocommerce->cart->tax->is_compound( $key )) ? 1 : 0;
2012-01-12 00:54:45 +00:00
$order_taxes[] = array(
'label' => $woocommerce->cart->tax->get_rate_label( $key ),
'compound' => $is_compound,
'total' => number_format($tax, 2, '.', '')
);
2012-01-12 00:54:45 +00:00
endforeach;
// Save other order meta fields
update_post_meta( $order_id, '_shipping_method', $this->posted['shipping_method']);
update_post_meta( $order_id, '_payment_method', $this->posted['payment_method']);
update_post_meta( $order_id, '_shipping_method_title', $shipping_method);
update_post_meta( $order_id, '_payment_method_title', $payment_method);
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->get_order_discount_total(), 2, '.', ''));
update_post_meta( $order_id, '_cart_discount', number_format($woocommerce->cart->get_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, '.', ''));
update_post_meta( $order_id, '_order_key', apply_filters('woocommerce_generate_order_key', uniqid('order_') ));
update_post_meta( $order_id, '_customer_user', (int) $user_id );
update_post_meta( $order_id, '_order_items', $order_items );
update_post_meta( $order_id, '_order_taxes', $order_taxes );
update_post_meta( $order_id, '_order_currency', get_option('woocommerce_currency') );
update_post_meta( $order_id, '_order_price_include_tax',get_option('woocommerce_prices_include_tax') );
2012-01-12 00:54:45 +00:00
do_action('woocommerce_checkout_update_order_meta', $order_id, $this->posted);
// Order status
wp_set_object_terms( $order_id, 'pending', 'shop_order_status' );
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
// Discount code meta
if ($applied_coupons = $woocommerce->cart->get_applied_coupons()) update_post_meta($order_id, 'coupons', implode(', ', $applied_coupons));
// Order is saved
do_action('woocommerce_checkout_order_processed', $order_id, $this->posted);
// Process payment
if ($woocommerce->cart->needs_payment()) :
2011-10-09 13:23:20 +00:00
2012-01-12 00:54:45 +00:00
// Store Order ID in session so it can be re-used after payment failure
$_SESSION['order_awaiting_payment'] = $order_id;
2012-01-10 16:43:06 +00:00
2012-01-12 00:54:45 +00:00
// Process Payment
$result = $available_gateways[$this->posted['payment_method']]->process_payment( $order_id );
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
// Redirect to success/confirmation/payment page
if ($result['result']=='success') :
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
$result = apply_filters('woocommerce_payment_successful_result', $result );
2011-08-09 15:16:18 +00:00
if (is_ajax()) :
2012-01-12 00:54:45 +00:00
echo json_encode( $result );
2011-08-09 15:16:18 +00:00
exit;
else :
2012-01-12 00:54:45 +00:00
wp_safe_redirect( $result['redirect'] );
2011-08-09 15:16:18 +00:00
exit;
endif;
endif;
2012-01-12 00:54:45 +00:00
else :
2012-01-12 00:54:45 +00:00
$order = new woocommerce_order($order_id);
// No payment was required for order
$order->payment_complete();
// Empty the Cart
$woocommerce->cart->empty_cart();
// Redirect to success/confirmation/payment page
if (is_ajax()) :
2012-01-18 12:32:39 +00:00
echo json_encode(
array(
'redirect' => apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', get_permalink(woocommerce_get_page_id('thanks')), $order)
)
);
2012-01-12 00:54:45 +00:00
exit;
else :
2012-01-18 12:32:39 +00:00
wp_safe_redirect(
apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', get_permalink(woocommerce_get_page_id('thanks')), $order)
);
2012-01-12 00:54:45 +00:00
exit;
endif;
endif;
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
// Break out of loop
break;
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
endwhile;
endif;
2011-08-09 15:16:18 +00:00
2012-01-12 00:54:45 +00:00
// If we reached this point then there were errors
if (is_ajax()) :
$woocommerce->show_messages();
exit;
2011-08-09 15:16:18 +00:00
endif;
}
/** Gets the value either from the posted data, or from the users meta data */
function get_value( $input ) {
global $woocommerce;
2012-01-13 11:44:35 +00:00
if (isset( $_POST[$input] ) && !empty($_POST[$input])) :
return esc_attr($_POST[$input]);
2011-08-09 15:16:18 +00:00
elseif (is_user_logged_in()) :
if ($meta = get_user_meta( get_current_user_id(), $input, true )) return $meta;
2011-08-09 15:16:18 +00:00
$current_user = wp_get_current_user();
if ($input == "billing_email") :
return $current_user->user_email;
endif;
else :
if ($input == "billing_country") :
return $woocommerce->countries->get_base_country();
endif;
2011-08-09 15:16:18 +00:00
endif;
}
}