woocommerce/classes/class-wc-checkout.php

760 lines
28 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
*
2012-08-14 19:42:38 +00:00
* @class WC_Cart
* @version 1.6.4
2012-08-14 22:43:48 +00:00
* @package WooCommerce/Classes
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-15 17:08:42 +00:00
/** @var array Array of posted form data. */
2011-08-09 15:16:18 +00:00
var $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. */
var $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. */
2011-08-09 15:16:18 +00:00
var $must_create_account;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var bool True when the user is creating an account. */
2011-08-09 15:16:18 +00:00
var $creating_account;
2012-08-14 19:42:38 +00:00
/**
2012-08-15 17:08:42 +00:00
* Constructor for the checkout class. Hooks in methods and defines eheckout fields.
2012-08-14 19:42:38 +00:00
*
* @access public
* @return void
*/
2011-09-06 11:18:22 +00:00
function __construct () {
global $woocommerce;
2012-02-12 11:36:33 +00:00
add_action('woocommerce_checkout_process',array(&$this,'checkout_process'));
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'));
$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_' );
if ( get_option( 'woocommerce_registration_email_for_username' ) == 'no' ) {
$this->checkout_fields['account']['account_username'] = array(
'type' => 'text',
'label' => __( 'Account username', 'woocommerce' ),
'placeholder' => _x( 'Username', 'placeholder', 'woocommerce' )
);
}
$this->checkout_fields['account']['account_password'] = array(
'type' => 'password',
'label' => __( 'Account password', 'woocommerce' ),
'placeholder' => _x( 'Password', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-first' )
);
$this->checkout_fields['account']['account_password-2'] = array(
'type' => 'password',
'label' => __( 'Account password', 'woocommerce' ),
'placeholder' => _x( 'Password', 'placeholder', 'woocommerce' ),
'class' => array( 'form-row-last' ),
'label_class' => array( 'hidden' )
);
$this->checkout_fields['order'] = array(
'order_comments' => array(
'type' => 'textarea',
'class' => array('notes'),
2012-10-16 09:45:33 +00:00
'label' => __( 'Order Notes', 'woocommerce' ),
'placeholder' => _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce')
)
);
$this->checkout_fields = apply_filters('woocommerce_checkout_fields', $this->checkout_fields);
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Checkout process
*
* @access public
* @return void
*/
2012-02-12 11:36:33 +00:00
function checkout_process() {
// When we process the checkout, lets ensure cart items are rechecked to prevent checkout
do_action('woocommerce_check_cart_items');
}
2012-08-14 19:42:38 +00:00
/**
* Output the billing information form
*
* @access public
* @return void
*/
2011-08-09 15:16:18 +00:00
function checkout_form_billing() {
2012-02-03 16:17:35 +00:00
woocommerce_get_template( 'checkout/form-billing.php', array( 'checkout' => $this ) );
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Output the shipping information form
*
* @access public
* @return void
*/
2011-08-09 15:16:18 +00:00
function checkout_form_shipping() {
2012-02-03 16:17:35 +00:00
woocommerce_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
/**
* Process the checkout after the confirm order button is pressed
2012-08-14 19:42:38 +00:00
*
* @access public
* @return void
*/
2011-08-09 15:16:18 +00:00
function process_checkout() {
global $wpdb, $woocommerce;
2012-09-07 17:26:13 +00:00
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) )
define( 'WOOCOMMERCE_CHECKOUT', true );
2012-09-07 17:26:13 +00:00
$woocommerce->verify_nonce( 'process_checkout' );
2012-09-07 17:26:13 +00:00
do_action( 'woocommerce_before_checkout_process' );
2012-06-10 11:40:07 +00:00
if ( sizeof( $woocommerce->cart->get_cart() ) == 0 )
2012-09-07 17:26:13 +00:00
$woocommerce->add_error( sprintf( __( 'Sorry, your session has expired. <a href="%s">Return to homepage &rarr;</a>', 'woocommerce' ), home_url() ) );
2012-09-07 17:26:13 +00:00
do_action( 'woocommerce_checkout_process' );
2012-01-12 00:54:45 +00:00
// Checkout fields (not defined in checkout_fields)
2012-09-07 17:26:13 +00:00
$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'] ) : '';
2012-01-12 00:54:45 +00:00
// Ship to billing only option
2012-09-07 17:26:13 +00:00
if ( $woocommerce->cart->ship_to_billing_address_only() )
$this->posted['shiptobilling'] = 1;
2012-01-12 00:54:45 +00:00
// Update customer shipping and payment method to posted method
2012-09-07 17:26:13 +00:00
$woocommerce->session->chosen_shipping_method = $this->posted['shipping_method'];
2012-09-12 11:48:30 +00:00
$woocommerce->session->chosen_payment_method = $this->posted['payment_method'];
2012-01-12 00:54:45 +00:00
// 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->ship_to_billing_address_only() || $this->posted['shiptobilling'] || ( ! $woocommerce->cart->needs_shipping() && get_option('woocommerce_require_shipping_address') == 'no' ) ) ) {
2012-01-12 00:54:45 +00:00
$skipped_shipping = true;
continue;
}
2012-07-16 19:21:44 +00:00
foreach ( $fieldset as $key => $field ) {
2012-07-16 19:21:44 +00:00
if ( ! isset( $field['type'] ) ) $field['type'] = 'text';
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" :
2012-07-16 19:21:44 +00:00
$this->posted[ $key ] = isset( $_POST[$key] ) ? 1 : 0;
2012-01-12 00:54:45 +00:00
break;
default :
2012-07-16 19:21:44 +00:00
$this->posted[ $key ] = isset( $_POST[$key] ) ? woocommerce_clean( $_POST[ $key ] ) : '';
2012-01-12 00:54:45 +00:00
break;
2012-07-16 19:21:44 +00:00
}
2012-01-12 00:54:45 +00:00
// Hook to allow modification of value
2012-07-16 19:21:44 +00:00
$this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_field_' . $key, $this->posted[$key] );
2012-01-12 00:54:45 +00:00
// Validation: Required fields
2012-10-16 09:45:33 +00:00
if ( isset( $field['required'] ) && $field['required'] && empty( $this->posted[$key] ) ) $woocommerce->add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is a required field.', 'woocommerce' ) );
2012-07-16 19:21:44 +00:00
if ( ! empty( $this->posted[ $key ] ) ) {
2012-01-12 00:54:45 +00:00
// Special handling for validation and formatting
2012-07-16 19:21:44 +00:00
switch ( $key ) {
2012-01-12 00:54:45 +00:00
case "billing_postcode" :
case "shipping_postcode" :
$validate_against = $key == 'billing_postcode' ? 'billing_country' : 'shipping_country';
$this->posted[ $key ] = strtoupper( str_replace( ' ', '', $this->posted[ $key ] ) );
2012-07-16 19:21:44 +00:00
if ( ! $validation->is_postcode( $this->posted[ $key ], $_POST[ $validate_against ] ) )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( '<strong>' . $field['label'] . '</strong> ' . sprintf( __( '(%s) is not a valid postcode/ZIP.', 'woocommerce' ), $this->posted[ $key ] ) );
2012-07-16 19:21:44 +00:00
else
$this->posted[ $key ] = $validation->format_postcode( $this->posted[$key], $_POST[ $validate_against ] );
2012-07-16 19:21:44 +00:00
break;
case "billing_state" :
case "shipping_state" :
2012-07-16 19:21:44 +00:00
// Get valid states
$validate_against = $key == 'billing_state' ? 'billing_country' : 'shipping_country';
$valid_states = $woocommerce->countries->get_states( $_POST[ $validate_against ] );
if ( $valid_states )
$valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) );
2012-07-16 19:21:44 +00:00
// 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 ] ) ];
2012-07-16 19:21:44 +00:00
// 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 ) ) )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not valid. Please enter one of the following:', 'woocommerce' ) . ' ' . implode( ', ', $valid_states ) );
break;
2012-01-12 00:54:45 +00:00
case "billing_phone" :
2012-06-10 09:44:13 +00:00
$this->posted[ $key ] = $validation->format_phone( $this->posted[ $key ] );
2012-06-10 09:44:13 +00:00
if ( ! $validation->is_phone( $this->posted[ $key ] ) )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid number.', 'woocommerce' ) );
2012-01-12 00:54:45 +00:00
break;
case "billing_email" :
2012-07-16 19:21:44 +00:00
$this->posted[ $key ] = strtolower( $this->posted[$key] );
if ( ! $validation->is_email( $this->posted[$key] ) )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ) );
break;
2012-07-16 19:21:44 +00:00
}
}
}
}
2012-01-12 00:54:45 +00:00
// Update customer location to posted location so we can correctly check available shipping methods
if ( isset( $this->posted['billing_country'] ) )
2012-03-26 19:28:19 +00:00
$woocommerce->customer->set_country( $this->posted['billing_country'] );
if ( isset( $this->posted['billing_state'] ) )
2012-03-26 19:28:19 +00:00
$woocommerce->customer->set_state( $this->posted['billing_state'] );
if ( isset( $this->posted['billing_postcode'] ) )
2012-03-26 19:28:19 +00:00
$woocommerce->customer->set_postcode( $this->posted['billing_postcode'] );
2012-01-12 00:54:45 +00:00
// Shipping Information
2012-03-26 19:28:19 +00:00
if ( ! $skipped_shipping ) {
// Update customer location to posted location so we can correctly check available shipping methods
if ( isset( $this->posted['shipping_country'] ) )
2012-03-26 19:16:05 +00:00
$woocommerce->customer->set_shipping_country( $this->posted['shipping_country'] );
if ( isset( $this->posted['shipping_state'] ) )
$woocommerce->customer->set_shipping_state( $this->posted['shipping_state'] );
if ( isset( $this->posted['shipping_postcode'] ) )
$woocommerce->customer->set_shipping_postcode( $this->posted['shipping_postcode'] );
2012-03-26 19:28:19 +00:00
} else {
2012-01-12 00:54:45 +00:00
// Update customer location to posted location so we can correctly check available shipping methods
if ( isset( $this->posted['billing_country'] ) )
2012-03-26 19:16:05 +00:00
$woocommerce->customer->set_shipping_country( $this->posted['billing_country'] );
if ( isset( $this->posted['billing_state'] ) )
2012-03-26 19:16:05 +00:00
$woocommerce->customer->set_shipping_state( $this->posted['billing_state'] );
if ( isset( $this->posted['billing_postcode'] ) )
2012-03-26 19:16:05 +00:00
$woocommerce->customer->set_shipping_postcode( $this->posted['billing_postcode'] );
2012-03-26 19:28:19 +00:00
}
2012-07-16 19:21:44 +00:00
// Update cart totals now we have customer address
$woocommerce->cart->calculate_totals();
2012-07-16 19:21:44 +00:00
// Handle accounts
2012-04-21 18:10:01 +00:00
if ( is_user_logged_in() )
2012-01-12 00:54:45 +00:00
$this->creating_account = false;
2012-04-21 18:10:01 +00:00
elseif ( ! empty( $this->posted['createaccount'] ) )
2012-01-12 00:54:45 +00:00
$this->creating_account = true;
2012-04-21 18:10:01 +00:00
elseif ($this->must_create_account)
2012-01-12 00:54:45 +00:00
$this->creating_account = true;
else
2012-01-12 00:54:45 +00:00
$this->creating_account = false;
2012-04-21 18:10:01 +00:00
if ( $this->creating_account ) {
if ( get_option( 'woocommerce_registration_email_for_username' ) == 'no' ) {
if ( empty( $this->posted['account_username'] ) )
$woocommerce->add_error( __( 'Please enter an account username.', 'woocommerce' ) );
// Check the username
if ( ! validate_username( $this->posted['account_username'] ) )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( __( 'Invalid email/username.', 'woocommerce' ) );
elseif ( username_exists( $this->posted['account_username'] ) )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( __( 'An account is already registered with that username. Please choose another.', 'woocommerce' ) );
} else {
$this->posted['account_username'] = $this->posted['billing_email'];
}
// Validate passwords
if ( empty($this->posted['account_password']) )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( __( 'Please enter an account password.', 'woocommerce' ) );
if ( $this->posted['account_password-2'] !== $this->posted['account_password'] )
2012-10-16 09:45:33 +00:00
$woocommerce->add_error( __( 'Passwords do not match.', 'woocommerce' ) );
2012-01-12 00:54:45 +00:00
// Check the e-mail address
2012-07-16 19:21:44 +00:00
if ( email_exists( $this->posted['billing_email'] ) )
$woocommerce->add_error( __( 'An account is already registered with your email address. Please login.', 'woocommerce' ) );
2012-04-21 18:10:01 +00:00
}
2012-01-12 00:54:45 +00:00
// Terms
if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && empty( $this->posted['terms'] ) && woocommerce_get_page_id( 'terms' ) > 0 )
2012-09-07 17:26:13 +00:00
$woocommerce->add_error( __( 'You must accept our Terms &amp; Conditions.', 'woocommerce' ) );
2012-07-16 19:21:44 +00:00
if ( $woocommerce->cart->needs_shipping() ) {
2012-01-12 00:54:45 +00:00
// Shipping Method
$available_methods = $woocommerce->shipping->get_available_shipping_methods();
2012-07-16 19:21:44 +00:00
if ( ! isset( $available_methods[ $this->posted['shipping_method'] ] ) )
2012-09-07 17:26:13 +00:00
$woocommerce->add_error( __( 'Invalid shipping method.', 'woocommerce' ) );
}
2012-09-07 17:26:13 +00:00
if ( $woocommerce->cart->needs_payment() ) {
2012-01-12 00:54:45 +00:00
// Payment Method
$available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
2012-07-16 19:21:44 +00:00
if ( ! isset( $available_gateways[ $this->posted['payment_method'] ] ) )
2012-09-07 17:26:13 +00:00
$woocommerce->add_error( __( 'Invalid payment method.', 'woocommerce' ) );
2012-07-16 19:21:44 +00:00
else
2012-09-07 17:26:13 +00:00
$available_gateways[ $this->posted['payment_method'] ]->validate_fields(); // Payment Method Field Validation
2012-07-16 19:21:44 +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-09-07 17:26:13 +00:00
if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && $woocommerce->error_count() == 0 ) {
2012-01-12 00:54:45 +00:00
$user_id = get_current_user_id();
2012-09-07 17:26:13 +00:00
try {
2012-01-12 00:54:45 +00:00
// Create customer account and log them in
2012-09-07 17:26:13 +00:00
if ( $this->creating_account && ! $user_id ) {
2012-01-12 00:54:45 +00:00
$reg_errors = new WP_Error();
2012-01-12 00:54:45 +00:00
do_action('woocommerce_register_post', $this->posted['account_username'], $this->posted['billing_email'], $reg_errors);
2012-01-12 00:54:45 +00:00
$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
2012-09-07 17:26:13 +00:00
if ( ! $reg_errors->get_error_code() ) {
2012-02-15 12:42:10 +00:00
$user_pass = esc_attr( $this->posted['account_password'] );
$user_id = wp_create_user( $this->posted['account_username'], $user_pass, $this->posted['billing_email'] );
2012-09-07 17:26:13 +00:00
if ( ! $user_id )
2012-10-16 09:45:33 +00:00
throw new MyException( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Couldn&#8217;t register you&hellip; please contact us if you continue to have problems.', 'woocommerce' ) );
2012-01-12 00:54:45 +00:00
// Change role
2012-09-07 17:26:13 +00:00
wp_update_user( array('ID' => $user_id, 'role' => 'customer') ) ;
// Action
do_action( 'woocommerce_created_customer', $user_id );
2012-01-12 00:54:45 +00:00
// send the user a confirmation and their login details
$mailer = $woocommerce->mailer();
2012-02-15 12:42:10 +00:00
$mailer->customer_new_account( $user_id, $user_pass );
2012-01-12 00:54:45 +00:00
// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie($user_id, true, $secure_cookie);
2012-09-07 17:26:13 +00:00
} else {
throw new MyException( $reg_errors->get_error_message() );
}
2012-09-07 17:26:13 +00:00
}
2012-01-12 00:54:45 +00:00
// 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.
2012-01-27 16:38:39 +00:00
$_tax = new WC_Tax();
2012-01-12 00:54:45 +00:00
$order_data = array(
'post_type' => 'shop_order',
2012-06-29 16:38:00 +00:00
'post_title' => sprintf( __( 'Order &ndash; %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) ),
2012-01-12 00:54:45 +00:00
'post_status' => 'publish',
'ping_status' => 'closed',
2012-01-12 00:54:45 +00:00
'post_excerpt' => $this->posted['order_comments'],
2012-03-12 09:18:10 +00:00
'post_author' => 1,
2012-09-07 17:26:13 +00:00
'post_password' => uniqid( 'order_' ) // Protects the post just in case
2012-01-12 00:54:45 +00:00
);
// Cart items
$order_items = array();
2012-07-17 18:11:14 +00:00
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
2012-01-12 00:54:45 +00:00
$_product = $values['data'];
// Store any item meta data - item meta class lets plugins add item meta in a standardized way
2012-08-15 17:08:42 +00:00
$item_meta = new WC_Order_Item_Meta();
2012-01-12 00:54:45 +00:00
$item_meta->new_order_item( $values );
2012-01-12 00:54:45 +00:00
// Store variation data in meta so admin can view it
2012-07-17 18:11:14 +00:00
if ( $values['variation'] && is_array( $values['variation'] ) ) {
foreach ( $values['variation'] as $key => $value ) {
$item_meta->add( esc_attr( str_replace( 'attribute_', '', $key ) ), $value );
}
}
2012-07-17 18:11:14 +00:00
// Store backorder status
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $values['quantity'] ) )
$item_meta->add( __( 'Backordered', 'woocommerce' ), $values['quantity'] - max( 0, $_product->get_total_stock() ) );
2012-07-17 18:11:14 +00:00
$order_items[] = apply_filters( 'new_order_item', array(
2012-01-12 00:54:45 +00:00
'id' => $values['product_id'],
'variation_id' => $values['variation_id'],
'name' => $_product->get_title(),
'qty' => (int) $values['quantity'],
'item_meta' => $item_meta->meta,
2012-07-17 18:11:14 +00:00
'line_subtotal' => woocommerce_format_decimal( $values['line_subtotal'] ), // Line subtotal (before discounts)
'line_subtotal_tax' => woocommerce_format_decimal( $values['line_subtotal_tax'] ), // Line tax (before discounts)
'line_total' => woocommerce_format_decimal( $values['line_total'] ), // Line total (after discounts)
'line_tax' => woocommerce_format_decimal( $values['line_tax'] ), // Line Tax (after discounts)
'tax_class' => $_product->get_tax_class() // Tax class (adjusted by filters)
), $values );
2012-07-17 18:11:14 +00:00
}
2012-01-12 00:54:45 +00:00
// Check order items for errors
do_action('woocommerce_check_new_order_items', $order_items);
2012-09-07 17:26:13 +00:00
if ( $woocommerce->error_count() > 0 )
throw new MyException();
2012-01-12 00:54:45 +00:00
// Insert or update the post data
$create_new_order = true;
2012-09-07 17:26:13 +00:00
if ( $woocommerce->session->order_awaiting_payment > 0 ) {
2012-09-07 17:26:13 +00:00
$order_id = (int) $woocommerce->session->order_awaiting_payment;
/* Check order is unpaid by getting its status */
2012-09-07 17:26:13 +00:00
$terms = wp_get_object_terms( $order_id, 'shop_order_status', array( 'fields' => 'slugs' ) );
$order_status = isset( $terms[0] ) ? $terms[0] : 'pending';
2012-09-07 17:26:13 +00:00
if ( $order_status == 'pending' ) {
2012-01-12 00:54:45 +00:00
// Resume the unpaid order
$order_data['ID'] = $order_id;
wp_update_post( $order_data );
2012-09-07 17:26:13 +00:00
do_action( 'woocommerce_resume_order', $order_id );
2012-01-12 00:54:45 +00:00
$create_new_order = false;
2012-09-07 17:26:13 +00:00
}
}
2012-09-07 17:26:13 +00:00
if ( $create_new_order ) {
2012-01-12 00:54:45 +00:00
$order_id = wp_insert_post( $order_data );
2012-09-07 17:26:13 +00:00
if ( is_wp_error( $order_id ) )
throw new MyException( 'Error: Unable to create order. Please try again.' );
else
do_action( 'woocommerce_new_order', $order_id ); // Inserted successfully
}
// Get better formatted shipping method (title)
2012-01-12 00:54:45 +00:00
$shipping_method = $this->posted['shipping_method'];
2012-08-12 14:12:52 +00:00
if ( isset( $available_methods[ $this->posted['shipping_method'] ] ) )
$shipping_method = $available_methods[ $this->posted['shipping_method'] ]->label;
// Get better formatted payment method (title/label)
2012-01-12 00:54:45 +00:00
$payment_method = $this->posted['payment_method'];
2012-08-12 14:12:52 +00:00
if ( isset( $available_gateways[ $this->posted['payment_method'] ] ) )
$payment_method = $available_gateways[ $this->posted['payment_method'] ]->get_title();
2012-01-12 00:54:45 +00:00
// UPDATE ORDER META
2012-01-12 00:54:45 +00:00
// Save billing and shipping first, also save to user meta if logged in
2012-09-07 17:26:13 +00:00
if ( $this->checkout_fields['billing'] ) {
foreach ( $this->checkout_fields['billing'] as $key => $field ) {
2012-01-12 00:54:45 +00:00
// Post
2012-09-07 17:26:13 +00:00
update_post_meta( $order_id, '_' . $key, $this->posted[ $key ] );
2012-01-12 00:54:45 +00:00
// User
2012-09-07 17:26:13 +00:00
if ( $user_id > 0 && ! empty( $this->posted[ $key ] ) ) {
update_user_meta( $user_id, $key, $this->posted[ $key ] );
// Special fields
2012-09-07 17:26:13 +00:00
switch ( $key ) {
case "billing_email" :
2012-09-07 17:26:13 +00:00
if ( ! email_exists( $this->posted[ $key ] ) )
wp_update_user( array ( 'ID' => $user_id, 'user_email' => $this->posted[ $key ] ) ) ;
break;
case "billing_first_name" :
2012-09-07 17:26:13 +00:00
wp_update_user( array ( 'ID' => $user_id, 'first_name' => $this->posted[ $key ] ) ) ;
break;
case "billing_last_name" :
2012-09-07 17:26:13 +00:00
wp_update_user( array ( 'ID' => $user_id, 'last_name' => $this->posted[ $key ] ) ) ;
break;
}
}
}
}
if ( $this->checkout_fields['shipping'] && ( $woocommerce->cart->needs_shipping() || get_option('woocommerce_require_shipping_address') == 'yes' ) ) {
2012-09-07 17:26:13 +00:00
foreach ( $this->checkout_fields['shipping'] as $key => $field ) {
if ( $this->posted['shiptobilling'] ) {
2012-01-12 00:54:45 +00:00
$field_key = str_replace('shipping_', 'billing_', $key);
// Post
2012-09-07 17:26:13 +00:00
update_post_meta( $order_id, '_' . $key, $this->posted[ $field_key ] );
} else {
2012-01-12 00:54:45 +00:00
// Post
2012-09-07 17:26:13 +00:00
update_post_meta( $order_id, '_' . $key, $this->posted[ $key ] );
2012-01-12 00:54:45 +00:00
// User
if ( $user_id > 0 )
2012-09-07 17:26:13 +00:00
update_user_meta( $user_id, $key, $this->posted[ $key ] );
}
}
}
// Save any other user meta
2012-09-07 17:26:13 +00:00
if ( $user_id )
do_action( 'woocommerce_checkout_update_user_meta', $user_id, $this->posted );
// Prepare order taxes for storage
2012-01-12 00:54:45 +00:00
$order_taxes = array();
2012-09-07 17:26:13 +00:00
foreach ( array_keys( $woocommerce->cart->taxes + $woocommerce->cart->shipping_taxes ) as $key ) {
2012-09-07 17:26:13 +00:00
$is_compound = $woocommerce->cart->tax->is_compound( $key ) ? 1 : 0;
2012-09-07 17:26:13 +00:00
$cart_tax = isset( $woocommerce->cart->taxes[ $key ] ) ? $woocommerce->cart->taxes[ $key ] : 0;
$shipping_tax = isset( $woocommerce->cart->shipping_taxes[ $key ] ) ? $woocommerce->cart->shipping_taxes[ $key ] : 0;
2012-01-12 00:54:45 +00:00
$order_taxes[] = array(
2012-08-21 18:01:56 +00:00
'label' => $woocommerce->cart->tax->get_rate_label( $key ),
'compound' => $is_compound,
'cart_tax' => woocommerce_format_total( $cart_tax ),
'shipping_tax' => woocommerce_format_total( $shipping_tax )
2012-01-12 00:54:45 +00:00
);
}
2012-05-25 11:10:17 +00:00
2012-01-12 00:54:45 +00:00
// Save other order meta fields
2012-09-07 17:26:13 +00:00
update_post_meta( $order_id, '_shipping_method', $this->posted['shipping_method'] );
update_post_meta( $order_id, '_payment_method', $this->posted['payment_method'] );
2012-08-21 18:01:56 +00:00
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', woocommerce_format_total( $woocommerce->cart->shipping_total ) );
update_post_meta( $order_id, '_order_discount', woocommerce_format_total( $woocommerce->cart->get_order_discount_total() ) );
update_post_meta( $order_id, '_cart_discount', woocommerce_format_total( $woocommerce->cart->get_cart_discount_total() ) );
update_post_meta( $order_id, '_order_tax', woocommerce_format_total( $woocommerce->cart->tax_total ) );
update_post_meta( $order_id, '_order_shipping_tax', woocommerce_format_total( $woocommerce->cart->shipping_tax_total ) );
update_post_meta( $order_id, '_order_total', woocommerce_format_total( $woocommerce->cart->total ) );
update_post_meta( $order_id, '_order_key', apply_filters('woocommerce_generate_order_key', uniqid('order_') ) );
2012-01-12 00:54:45 +00:00
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 );
2012-04-16 15:27:18 +00:00
update_post_meta( $order_id, '_order_currency', get_woocommerce_currency() );
2012-09-07 17:26:13 +00:00
update_post_meta( $order_id, '_prices_include_tax', get_option( 'woocommerce_prices_include_tax' ) );
// Store technical customer details in meta
$customer_ip = isset( $_SERVER['HTTP_X_FORWARD_FOR'] ) ? $_SERVER['HTTP_X_FORWARD_FOR'] : $_SERVER['REMOTE_ADDR'];
$customer_user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
update_post_meta( $order_id, __( 'Customer IP Address', 'woocommerce' ), $customer_ip );
update_post_meta( $order_id, __( 'Customer UA', 'woocommerce' ), $customer_user_agent );
// Let plugins add meta
2012-09-07 17:26:13 +00:00
do_action( 'woocommerce_checkout_update_order_meta', $order_id, $this->posted );
2012-01-12 00:54:45 +00:00
// Order status
wp_set_object_terms( $order_id, 'pending', 'shop_order_status' );
2012-01-12 00:54:45 +00:00
// Discount code meta
2012-07-20 22:52:27 +00:00
if ( $applied_coupons = $woocommerce->cart->get_applied_coupons() ) {
2012-07-20 22:52:27 +00:00
update_post_meta( $order_id, 'coupons', implode(', ', $applied_coupons) );
2012-08-12 14:12:52 +00:00
if ( empty( $order ) )
$order = new WC_Order( $order_id );
2012-07-20 22:52:27 +00:00
$order->add_order_note( sprintf( __( 'Coupon Code Used: %s', 'woocommerce' ), implode(', ', $applied_coupons ) ) );
}
2012-01-12 00:54:45 +00:00
// Order is saved
2012-09-07 17:26:13 +00:00
do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted );
// Prevent timeout
@set_time_limit(0);
2012-01-12 00:54:45 +00:00
// Process payment
2012-09-07 17:26:13 +00:00
if ( $woocommerce->cart->needs_payment() ) {
2012-01-12 00:54:45 +00:00
// Store Order ID in session so it can be re-used after payment failure
2012-09-07 17:26:13 +00:00
$woocommerce->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-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-01-12 00:54:45 +00:00
$result = apply_filters('woocommerce_payment_successful_result', $result );
2012-09-07 17:26:13 +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;
2012-09-07 17:26:13 +00:00
} else {
wp_redirect( $result['redirect'] );
2011-08-09 15:16:18 +00:00
exit;
2012-09-07 17:26:13 +00:00
}
2012-09-07 17:26:13 +00:00
}
2012-09-07 17:26:13 +00:00
} else {
2012-08-12 14:12:52 +00:00
if ( empty( $order ) )
$order = new WC_Order( $order_id );
2012-01-12 00:54:45 +00:00
// No payment was required for order
$order->payment_complete();
2012-01-12 00:54:45 +00:00
// Empty the Cart
$woocommerce->cart->empty_cart();
2012-03-05 13:25:02 +00:00
// Get redirect
2012-09-07 17:26:13 +00:00
$return_url = get_permalink( woocommerce_get_page_id( 'thanks' ) );
$return_url = add_query_arg( 'key', $order->order_key, add_query_arg( 'order', $order->id, $return_url ) );
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() ) {
echo json_encode(
2012-01-18 12:32:39 +00:00
array(
'result' => 'success',
2012-03-05 13:25:02 +00:00
'redirect' => 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
} else {
wp_safe_redirect(
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-09-07 17:26:13 +00:00
}
2012-09-07 17:26:13 +00:00
} catch ( Exception $e ) {
if ( ! empty( $e ) )
$woocommerce->add_error( $e );
}
2012-01-12 00:54:45 +00:00
2012-09-07 17:26:13 +00:00
} // endif
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() ) {
ob_start();
2012-01-12 00:54:45 +00:00
$woocommerce->show_messages();
$messages = ob_get_clean();
echo json_encode(
array(
'result' => 'failure',
'messages' => $messages,
2012-09-07 17:26:13 +00:00
'refresh' => isset( $woocommerce->session->refresh_totals ) ? 'true' : 'false'
)
);
2012-09-07 17:26:13 +00:00
unset( $woocommerce->session->refresh_totals );
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-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
*/
2011-08-09 15:16:18 +00:00
function get_value( $input ) {
global $woocommerce;
2012-01-13 11:44:35 +00:00
if (isset( $_POST[$input] ) && !empty($_POST[$input])) :
2012-01-13 11:44:35 +00:00
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 :
2012-03-07 13:07:56 +00:00
$default_billing_country = apply_filters('default_checkout_country', ($woocommerce->customer->get_country()) ? $woocommerce->customer->get_country() : $woocommerce->countries->get_base_country());
2012-03-07 13:07:56 +00:00
$default_shipping_country = apply_filters('default_checkout_country', ($woocommerce->customer->get_shipping_country()) ? $woocommerce->customer->get_shipping_country() : $woocommerce->countries->get_base_country());
if ( $woocommerce->customer->has_calculated_shipping() ) {
$default_billing_state = apply_filters('default_checkout_state', $woocommerce->customer->get_state());
$default_shipping_state = apply_filters('default_checkout_state', $woocommerce->customer->get_shipping_state());
} else {
$default_billing_state = apply_filters('default_checkout_state', '');
$default_shipping_state = apply_filters('default_checkout_state', '');
}
2012-03-07 13:07:56 +00:00
if ($input == "billing_country") return $default_billing_country;
2012-03-07 13:07:56 +00:00
if ($input == "billing_state") return $default_billing_state;
2012-02-18 10:08:12 +00:00
if ($input == "billing_postcode") return ($woocommerce->customer->get_postcode()) ? $woocommerce->customer->get_postcode() : '';
2012-03-07 13:07:56 +00:00
if ($input == "shipping_country") return $default_shipping_country;
2012-03-07 13:07:56 +00:00
if ($input == "shipping_state") return $default_shipping_state;
2012-02-18 10:08:12 +00:00
if ($input == "shipping_postcode") return ($woocommerce->customer->get_shipping_postcode()) ? $woocommerce->customer->get_shipping_postcode() : '';
2011-08-09 15:16:18 +00:00
endif;
}
2012-01-27 16:38:39 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* woocommerce_checkout class.
*
2012-08-15 17:08:42 +00:00
* @extends WC_Checkout
* @deprecated 1.4
* @package WooCommerce/Classes
2012-08-14 19:42:38 +00:00
*/
2012-01-27 16:38:39 +00:00
class woocommerce_checkout extends WC_Checkout {
public function __construct() {
2012-01-27 16:38:39 +00:00
_deprecated_function( 'woocommerce_checkout', '1.4', 'WC_Checkout()' );
parent::__construct();
}
2011-08-09 15:16:18 +00:00
}