2011-08-09 15:16:18 +00:00
< ? php
/**
* Order
2012-08-08 08:21:27 +00:00
*
2011-08-10 17:11:11 +00:00
* The WooCommerce order class handles order data .
2011-08-09 15:16:18 +00:00
*
2012-01-27 16:38:39 +00:00
* @ class WC_Order
2012-08-14 20:19:41 +00:00
* @ version 1.6 . 4
* @ package WooCommerce / classes
* @ author WooThemes
2011-08-09 15:16:18 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Order {
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
var $id ;
2012-01-17 11:58:12 +00:00
var $status ;
2012-01-13 00:46:56 +00:00
var $order_date ;
var $modified_date ;
var $customer_note ;
var $order_custom_fields ;
var $order_key ;
var $billing_first_name ;
var $billing_last_name ;
var $billing_company ;
var $billing_address_1 ;
var $billing_address_2 ;
var $billing_city ;
var $billing_postcode ;
var $billing_country ;
var $billing_state ;
var $billing_email ;
var $billing_phone ;
var $shipping_first_name ;
var $shipping_last_name ;
var $shipping_company ;
var $shipping_address_1 ;
var $shipping_address_2 ;
var $shipping_city ;
var $shipping_postcode ;
var $shipping_country ;
var $shipping_state ;
var $shipping_method ;
var $shipping_method_title ;
var $payment_method ;
var $payment_method_title ;
var $order_discount ;
var $cart_discount ;
var $order_tax ;
var $order_shipping ;
var $order_shipping_tax ;
var $order_total ;
var $items ;
var $taxes ;
var $customer_user ;
var $user_id ;
var $completed_date ;
var $billing_address ;
var $formatted_billing_address ;
var $shipping_address ;
var $formatted_shipping_address ;
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/** Get the order if ID is passed, otherwise the order is new and empty */
2012-01-27 16:38:39 +00:00
function __construct ( $id = '' ) {
2011-11-26 23:33:57 +00:00
$this -> prices_include_tax = ( get_option ( 'woocommerce_prices_include_tax' ) == 'yes' ) ? true : false ;
$this -> display_totals_ex_tax = ( get_option ( 'woocommerce_display_totals_excluding_tax' ) == 'yes' ) ? true : false ;
2011-11-27 00:03:46 +00:00
$this -> display_cart_ex_tax = ( get_option ( 'woocommerce_display_cart_prices_excluding_tax' ) == 'yes' ) ? true : false ;
2011-08-09 15:16:18 +00:00
if ( $id > 0 ) $this -> get_order ( $id );
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/** Gets an order from the database */
function get_order ( $id = 0 ) {
if ( ! $id ) return false ;
2012-08-08 08:21:27 +00:00
if ( $result = get_post ( $id )) :
$this -> populate ( $result );
2011-08-09 15:16:18 +00:00
return true ;
endif ;
return false ;
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/** Populates an order from the loaded post data */
function populate ( $result ) {
// Standard post data
2012-08-08 08:21:27 +00:00
$this -> id = $result -> ID ;
2011-08-09 15:16:18 +00:00
$this -> order_date = $result -> post_date ;
2012-08-08 08:21:27 +00:00
$this -> modified_date = $result -> post_modified ;
2011-08-09 15:16:18 +00:00
$this -> customer_note = $result -> post_excerpt ;
2012-01-13 00:46:56 +00:00
$this -> order_custom_fields = get_post_custom ( $this -> id );
2012-08-08 08:21:27 +00:00
2011-08-29 16:09:42 +00:00
// Define the data we're going to load: Key => Default value
2012-01-09 15:42:37 +00:00
$load_data = apply_filters ( 'woocommerce_load_order_data' , array (
2011-08-29 16:09:42 +00:00
'order_key' => '' ,
'billing_first_name' => '' ,
'billing_last_name' => '' ,
'billing_company' => '' ,
'billing_address_1' => '' ,
'billing_address_2' => '' ,
'billing_city' => '' ,
'billing_postcode' => '' ,
'billing_country' => '' ,
'billing_state' => '' ,
'billing_email' => '' ,
'billing_phone' => '' ,
'shipping_first_name' => '' ,
'shipping_last_name' => '' ,
'shipping_company' => '' ,
'shipping_address_1' => '' ,
'shipping_address_2' => '' ,
'shipping_city' => '' ,
'shipping_postcode' => '' ,
'shipping_country' => '' ,
'shipping_state' => '' ,
'shipping_method' => '' ,
2011-12-05 18:16:13 +00:00
'shipping_method_title' => '' ,
2011-08-29 16:09:42 +00:00
'payment_method' => '' ,
2011-12-05 18:16:13 +00:00
'payment_method_title' => '' ,
2011-08-29 16:09:42 +00:00
'order_discount' => '' ,
2011-11-25 19:31:06 +00:00
'cart_discount' => '' ,
2011-08-29 16:09:42 +00:00
'order_tax' => '' ,
2011-09-02 14:42:04 +00:00
'order_shipping' => '' ,
2011-08-29 16:09:42 +00:00
'order_shipping_tax' => '' ,
2012-01-13 00:46:56 +00:00
'order_total' => '' ,
'customer_user' => '' ,
'completed_date' => $this -> modified_date
2012-01-09 15:42:37 +00:00
));
2012-08-08 08:21:27 +00:00
2011-08-29 16:09:42 +00:00
// Load the data from the custom fields
foreach ( $load_data as $key => $default ) :
2012-01-13 00:46:56 +00:00
if ( isset ( $this -> order_custom_fields [ '_' . $key ][ 0 ]) && $this -> order_custom_fields [ '_' . $key ][ 0 ] !== '' ) :
$this -> $key = $this -> order_custom_fields [ '_' . $key ][ 0 ];
2011-08-29 16:09:42 +00:00
else :
$this -> $key = $default ;
endif ;
endforeach ;
2012-08-08 08:21:27 +00:00
2012-01-21 01:15:39 +00:00
// Aliases
2012-01-13 00:46:56 +00:00
$this -> user_id = ( int ) $this -> customer_user ;
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
// Get status
$terms = wp_get_object_terms ( $this -> id , 'shop_order_status' , array ( 'fields' => 'slugs' ) );
$this -> status = ( isset ( $terms [ 0 ])) ? $terms [ 0 ] : 'pending' ;
}
2012-08-08 08:21:27 +00:00
2012-02-19 17:13:00 +00:00
function key_is_valid ( $key ) {
2012-08-08 08:21:27 +00:00
if ( $key == $this -> order_key ) return true ;
2012-02-19 17:13:00 +00:00
return false ;
}
2012-08-08 08:21:27 +00:00
2012-03-26 10:26:50 +00:00
/**
* get_order_number function .
*
* Gets the order number for display ( by default , order ID )
2012-08-08 08:21:27 +00:00
*
2012-03-26 10:26:50 +00:00
* @ access public
* @ return string
*/
function get_order_number () {
2012-06-20 17:25:12 +00:00
return apply_filters ( 'woocommerce_order_number' , _x ( '#' , 'hash before order number' , 'woocommerce' ) . $this -> id , $this );
2012-03-26 10:26:50 +00:00
}
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
function get_formatted_billing_address () {
if ( ! $this -> formatted_billing_address ) :
global $woocommerce ;
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
// Formatted Addresses
2011-12-18 13:41:42 +00:00
$address = array (
2012-01-13 00:46:56 +00:00
'first_name' => $this -> billing_first_name ,
'last_name' => $this -> billing_last_name ,
'company' => $this -> billing_company ,
'address_1' => $this -> billing_address_1 ,
'address_2' => $this -> billing_address_2 ,
2012-08-08 08:21:27 +00:00
'city' => $this -> billing_city ,
2012-01-13 00:46:56 +00:00
'state' => $this -> billing_state ,
'postcode' => $this -> billing_postcode ,
'country' => $this -> billing_country
2011-12-18 13:41:42 +00:00
);
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
$this -> formatted_billing_address = $woocommerce -> countries -> get_formatted_address ( $address );
endif ;
return $this -> formatted_billing_address ;
}
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
function get_billing_address () {
if ( ! $this -> billing_address ) :
// Formatted Addresses
$address = array (
'address_1' => $this -> billing_address_1 ,
'address_2' => $this -> billing_address_2 ,
2012-08-08 08:21:27 +00:00
'city' => $this -> billing_city ,
2012-01-13 00:46:56 +00:00
'state' => $this -> billing_state ,
'postcode' => $this -> billing_postcode ,
'country' => $this -> billing_country
);
2011-12-19 17:50:41 +00:00
$joined_address = array ();
2011-12-18 13:41:42 +00:00
foreach ( $address as $part ) if ( ! empty ( $part )) $joined_address [] = $part ;
2012-01-13 00:46:56 +00:00
$this -> billing_address = implode ( ', ' , $joined_address );
2011-08-09 15:16:18 +00:00
endif ;
2012-01-13 00:46:56 +00:00
return $this -> billing_address ;
}
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
function get_formatted_shipping_address () {
if ( ! $this -> formatted_shipping_address ) :
if ( $this -> shipping_address_1 ) :
global $woocommerce ;
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
// Formatted Addresses
$address = array (
'first_name' => $this -> shipping_first_name ,
'last_name' => $this -> shipping_last_name ,
'company' => $this -> shipping_company ,
'address_1' => $this -> shipping_address_1 ,
'address_2' => $this -> shipping_address_2 ,
2012-08-08 08:21:27 +00:00
'city' => $this -> shipping_city ,
2012-01-13 00:46:56 +00:00
'state' => $this -> shipping_state ,
'postcode' => $this -> shipping_postcode ,
'country' => $this -> shipping_country
);
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
$this -> formatted_shipping_address = $woocommerce -> countries -> get_formatted_address ( $address );
endif ;
2011-08-09 15:16:18 +00:00
endif ;
2012-01-13 00:46:56 +00:00
return $this -> formatted_shipping_address ;
}
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
function get_shipping_address () {
if ( ! $this -> shipping_address ) :
if ( $this -> shipping_address_1 ) :
// Formatted Addresses
$address = array (
'address_1' => $this -> shipping_address_1 ,
'address_2' => $this -> shipping_address_2 ,
2012-08-08 08:21:27 +00:00
'city' => $this -> shipping_city ,
2012-01-13 00:46:56 +00:00
'state' => $this -> shipping_state ,
'postcode' => $this -> shipping_postcode ,
'country' => $this -> shipping_country
);
$joined_address = array ();
foreach ( $address as $part ) if ( ! empty ( $part )) $joined_address [] = $part ;
$this -> shipping_address = implode ( ', ' , $joined_address );
endif ;
endif ;
return $this -> shipping_address ;
}
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
function get_items () {
if ( ! $this -> items ) :
2012-01-13 11:58:35 +00:00
$this -> items = isset ( $this -> order_custom_fields [ '_order_items' ][ 0 ] ) ? maybe_unserialize ( $this -> order_custom_fields [ '_order_items' ][ 0 ] ) : array ();
2012-01-13 00:46:56 +00:00
endif ;
return $this -> items ;
}
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
function get_taxes () {
if ( ! $this -> taxes ) :
2012-01-13 11:58:35 +00:00
$this -> taxes = isset ( $this -> order_custom_fields [ '_order_taxes' ][ 0 ] ) ? maybe_unserialize ( $this -> order_custom_fields [ '_order_taxes' ][ 0 ] ) : array ();
2012-01-13 00:46:56 +00:00
endif ;
return $this -> taxes ;
2011-08-09 15:16:18 +00:00
}
2012-04-16 17:20:36 +00:00
/** Total Getters *******************************************************/
2011-08-09 15:16:18 +00:00
/** Gets shipping and product tax */
function get_total_tax () {
2012-08-12 08:28:07 +00:00
return apply_filters ( 'woocommerce_order_amount_total_tax' , number_format ( ( double ) $this -> order_tax + ( double ) $this -> order_shipping_tax , 2 , '.' , '' ) );
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2011-11-26 23:33:57 +00:00
/**
* gets the total ( product ) discount amount - these are applied before tax
*/
function get_cart_discount () {
2012-08-12 08:28:07 +00:00
return apply_filters ( 'woocommerce_order_amount_cart_discount' , number_format ( ( double ) $this -> cart_discount , 2 , '.' , '' ) );
2011-11-26 23:33:57 +00:00
}
2012-08-08 08:21:27 +00:00
2011-11-26 23:33:57 +00:00
/**
* gets the total ( product ) discount amount - these are applied before tax
*/
function get_order_discount () {
2012-08-12 08:28:07 +00:00
return apply_filters ( 'woocommerce_order_amount_order_discount' , number_format ( ( double ) $this -> order_discount , 2 , '.' , '' ) );
2011-11-26 23:33:57 +00:00
}
2012-08-08 08:21:27 +00:00
2011-11-26 23:33:57 +00:00
/**
* gets the total discount amount - both kinds
*/
2011-11-19 20:59:16 +00:00
function get_total_discount () {
2011-11-26 23:33:57 +00:00
if ( $this -> order_discount || $this -> cart_discount ) :
2012-08-12 08:28:07 +00:00
return apply_filters ( 'woocommerce_order_amount_total_discount' , number_format ( ( double ) $this -> order_discount + ( double ) $this -> cart_discount , 2 , '.' , '' ) );
2011-11-26 23:33:57 +00:00
endif ;
}
2012-08-08 08:21:27 +00:00
2011-11-26 23:33:57 +00:00
/** Gets shipping */
function get_shipping () {
2012-08-12 08:28:07 +00:00
return apply_filters ( 'woocommerce_order_amount_shipping' , number_format ( ( double ) $this -> order_shipping , 2 , '.' , '' ) );
2011-11-26 23:33:57 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-23 14:02:48 +00:00
/** Gets shipping tax amount */
function get_shipping_tax () {
2012-08-12 08:28:07 +00:00
return apply_filters ( 'woocommerce_order_amount_shipping_tax' , number_format ( ( double ) $this -> order_shipping_tax , 2 , '.' , '' ) );
}
/** Gets order total */
function get_total () {
return apply_filters ( 'woocommerce_order_amount_total' , number_format ( ( double ) $this -> order_total , 2 , '.' , '' ) );
2012-04-23 14:02:48 +00:00
}
2012-08-08 08:21:27 +00:00
2012-06-12 16:22:54 +00:00
/** Gets shipping method title */
function get_shipping_method () {
return apply_filters ( 'woocommerce_order_shipping_method' , ucwords ( $this -> shipping_method_title ) );
}
2012-08-08 08:21:27 +00:00
2012-01-22 02:36:10 +00:00
/** Get item subtotal - this is the cost before discount */
2012-01-24 16:56:10 +00:00
function get_item_subtotal ( $item , $inc_tax = false , $round = true ) {
2012-01-14 01:23:16 +00:00
if ( $inc_tax ) :
2012-01-24 16:56:10 +00:00
$price = ( $item [ 'line_subtotal' ] + $item [ 'line_subtotal_tax' ] / $item [ 'qty' ] );
2012-01-14 01:23:16 +00:00
else :
2012-01-24 16:56:10 +00:00
$price = ( $item [ 'line_subtotal' ] / $item [ 'qty' ] );
2012-01-14 01:23:16 +00:00
endif ;
2012-05-09 10:09:39 +00:00
return apply_filters ( 'woocommerce_order_amount_item_subtotal' , ( $round ) ? number_format ( $price , 2 , '.' , '' ) : $price );
2012-01-14 01:23:16 +00:00
}
2012-08-08 08:21:27 +00:00
2012-01-22 02:36:10 +00:00
/** Get line subtotal - this is the cost before discount */
2012-01-24 16:56:10 +00:00
function get_line_subtotal ( $item , $inc_tax = false , $round = true ) {
2012-05-08 17:47:35 +00:00
if ( $inc_tax ) :
2012-01-24 16:56:10 +00:00
$price = $item [ 'line_subtotal' ] + $item [ 'line_subtotal_tax' ];
2012-01-14 01:23:16 +00:00
else :
2012-01-24 16:56:10 +00:00
$price = $item [ 'line_subtotal' ];
2012-01-14 01:23:16 +00:00
endif ;
2012-05-09 10:09:39 +00:00
return apply_filters ( 'woocommerce_order_amount_line_subtotal' , ( $round ) ? number_format ( $price , 2 , '.' , '' ) : $price );
2012-01-14 01:23:16 +00:00
}
2012-08-08 08:21:27 +00:00
2011-11-29 00:52:57 +00:00
/** Calculate item cost - useful for gateways */
2012-01-24 16:56:10 +00:00
function get_item_total ( $item , $inc_tax = false , $round = true ) {
2011-11-29 00:52:57 +00:00
if ( $inc_tax ) :
2012-01-24 16:56:10 +00:00
$price = ( ( $item [ 'line_total' ] + $item [ 'line_tax' ] ) / $item [ 'qty' ] );
2011-11-29 00:52:57 +00:00
else :
2012-01-24 16:56:10 +00:00
$price = $item [ 'line_total' ] / $item [ 'qty' ];
2011-11-29 00:52:57 +00:00
endif ;
2012-05-09 10:09:39 +00:00
return apply_filters ( 'woocommerce_order_amount_item_total' , ( $round ) ? number_format ( $price , 2 , '.' , '' ) : $price );
2012-01-24 16:56:10 +00:00
}
2012-08-08 08:21:27 +00:00
2012-01-24 16:56:10 +00:00
/** Calculate item tax - useful for gateways */
function get_item_tax ( $item , $round = true ) {
$price = $item [ 'line_tax' ] / $item [ 'qty' ];
2012-05-09 10:09:39 +00:00
return apply_filters ( 'woocommerce_order_amount_item_tax' , ( $round ) ? number_format ( $price , 2 , '.' , '' ) : $price );
2011-11-29 00:52:57 +00:00
}
2012-08-08 08:21:27 +00:00
2012-01-22 02:36:10 +00:00
/** Calculate line total - useful for gateways */
function get_line_total ( $item , $inc_tax = false ) {
2011-11-28 17:03:53 +00:00
if ( $inc_tax ) :
2012-05-09 10:09:39 +00:00
return apply_filters ( 'woocommerce_order_amount_line_total' , number_format ( $item [ 'line_total' ] + $item [ 'line_tax' ] , 2 , '.' , '' ) );
2011-11-28 17:03:53 +00:00
else :
2012-05-09 10:09:39 +00:00
return apply_filters ( 'woocommerce_order_amount_line_total' , number_format ( $item [ 'line_total' ] , 2 , '.' , '' ) );
2011-11-28 17:03:53 +00:00
endif ;
}
2012-08-08 08:21:27 +00:00
2012-01-24 16:56:10 +00:00
/** Calculate line tax - useful for gateways */
function get_line_tax ( $item ) {
2012-05-09 10:09:39 +00:00
return apply_filters ( 'woocommerce_order_amount_line_tax' , number_format ( $item [ 'line_tax' ], 2 , '.' , '' ) );
2012-01-24 16:56:10 +00:00
}
2012-08-08 08:21:27 +00:00
2012-07-23 18:14:02 +00:00
/** Deprecated functions */
2012-08-08 08:21:27 +00:00
2012-05-09 10:09:39 +00:00
function get_order_total () {
2012-08-10 15:56:13 +00:00
return $this -> get_total ();
2012-05-09 10:09:39 +00:00
}
2012-08-08 08:21:27 +00:00
2012-01-24 16:56:10 +00:00
function get_item_cost ( $item , $inc_tax = false ) {
_deprecated_function ( __FUNCTION__ , '1.4' , 'get_item_total()' );
return $this -> get_item_total ( $item , $inc_tax );
}
2012-08-08 08:21:27 +00:00
2012-01-24 16:56:10 +00:00
function get_row_cost ( $item , $inc_tax = false ) {
_deprecated_function ( __FUNCTION__ , '1.4' , 'get_row_cost()' );
return $this -> get_line_total ( $item , $inc_tax );
}
2012-08-08 08:21:27 +00:00
2012-04-16 17:20:36 +00:00
/** End Total Getters *******************************************************/
2012-08-08 08:21:27 +00:00
2012-01-22 02:36:10 +00:00
/** Gets line subtotal - formatted for display */
function get_formatted_line_subtotal ( $item ) {
2012-01-04 23:01:47 +00:00
$subtotal = 0 ;
2012-08-08 08:21:27 +00:00
2012-01-22 02:36:10 +00:00
if ( ! isset ( $item [ 'line_subtotal' ]) || ! isset ( $item [ 'line_subtotal_tax' ])) return ;
2012-08-08 08:21:27 +00:00
if ( $this -> display_cart_ex_tax || ! $this -> prices_include_tax ) :
2012-05-08 17:47:35 +00:00
if ( $this -> prices_include_tax ) $ex_tax_label = 1 ; else $ex_tax_label = 0 ;
$subtotal = woocommerce_price ( $this -> get_line_subtotal ( $item ), array ( 'ex_tax_label' => $ex_tax_label ) );
2012-01-04 23:01:47 +00:00
else :
2012-01-22 02:36:10 +00:00
$subtotal = woocommerce_price ( $this -> get_line_subtotal ( $item , true ) );
2012-01-04 23:01:47 +00:00
endif ;
2012-04-27 02:18:18 +00:00
return apply_filters ( 'woocommerce_order_formatted_line_subtotal' , $subtotal , $item , $this );
}
2012-08-08 08:21:27 +00:00
2012-04-27 02:18:18 +00:00
/** Gets order total - formatted for display */
function get_formatted_order_total () {
2012-04-27 02:27:37 +00:00
$formatted_total = woocommerce_price ( $this -> order_total );
2012-04-27 02:18:18 +00:00
return apply_filters ( 'woocommerce_get_formatted_order_total' , $formatted_total , $this );
2012-01-04 23:01:47 +00:00
}
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
/** Gets subtotal - subtotal is shown before discounts, but with localised taxes */
2012-01-02 00:49:00 +00:00
function get_subtotal_to_display ( $compound = false ) {
2011-10-31 10:54:49 +00:00
global $woocommerce ;
2012-08-08 08:21:27 +00:00
2012-01-02 00:49:00 +00:00
$subtotal = 0 ;
2012-08-08 08:21:27 +00:00
2012-05-08 17:47:35 +00:00
if ( ! $compound ) :
2012-01-04 23:01:47 +00:00
2012-01-13 00:46:56 +00:00
foreach ( $this -> get_items () as $item ) :
2012-08-08 08:21:27 +00:00
2012-05-08 17:47:35 +00:00
if ( ! isset ( $item [ 'line_subtotal' ] ) || ! isset ( $item [ 'line_subtotal_tax' ] ) ) return ;
2012-08-08 08:21:27 +00:00
2012-01-22 02:36:10 +00:00
$subtotal += $this -> get_line_subtotal ( $item );
2012-08-08 08:21:27 +00:00
2012-05-08 17:47:35 +00:00
if ( ! $this -> display_cart_ex_tax ) :
2012-01-22 02:36:10 +00:00
$subtotal += $item [ 'line_subtotal_tax' ];
2012-01-04 23:01:47 +00:00
endif ;
2012-01-02 00:49:00 +00:00
endforeach ;
2012-08-08 08:21:27 +00:00
2012-05-08 17:47:35 +00:00
$subtotal = woocommerce_price ( $subtotal );
2012-08-08 08:21:27 +00:00
if ( $this -> display_cart_ex_tax && $this -> prices_include_tax ) :
2012-01-04 23:01:47 +00:00
$subtotal .= ' <small>' . $woocommerce -> countries -> ex_tax_or_vat () . '</small>' ;
endif ;
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
else :
2012-08-08 08:21:27 +00:00
2012-05-08 17:47:35 +00:00
if ( $this -> prices_include_tax ) return ;
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
foreach ( $this -> get_items () as $item ) :
2012-08-08 08:21:27 +00:00
2012-01-22 02:36:10 +00:00
$subtotal += $item [ 'line_subtotal' ];
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
endforeach ;
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
// Add Shipping Costs
$subtotal += $this -> get_shipping ();
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
// Remove non-compound taxes
2012-05-08 17:47:35 +00:00
foreach ( $this -> get_taxes () as $tax ) :
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
if ( isset ( $tax [ 'compound' ]) && $tax [ 'compound' ]) continue ;
2012-08-08 08:21:27 +00:00
2012-01-27 15:00:03 +00:00
$subtotal = $subtotal + $tax [ 'cart_tax' ] + $tax [ 'shipping_tax' ];
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
endforeach ;
2012-08-08 08:21:27 +00:00
2012-05-08 17:47:35 +00:00
// Remove discounts
$subtotal = $subtotal - $this -> get_cart_discount ();
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
$subtotal = woocommerce_price ( $subtotal );
2011-08-29 16:09:42 +00:00
endif ;
2012-08-08 08:21:27 +00:00
2012-05-25 06:36:38 +00:00
return apply_filters ( 'woocommerce_order_subtotal_to_display' , $subtotal , $compound , $this );
2011-08-09 15:16:18 +00:00
}
2011-11-26 23:33:57 +00:00
/** Gets shipping (formatted) */
2011-08-09 15:16:18 +00:00
function get_shipping_to_display () {
2011-10-31 10:54:49 +00:00
global $woocommerce ;
2012-08-08 08:21:27 +00:00
2012-06-12 16:22:54 +00:00
if ( $this -> order_shipping > 0 ) :
2012-08-08 08:21:27 +00:00
2011-11-27 00:03:46 +00:00
$tax_text = '' ;
2012-08-08 08:21:27 +00:00
2011-11-27 00:03:46 +00:00
if ( $this -> display_totals_ex_tax || ! $this -> prices_include_tax ) :
2011-08-09 15:16:18 +00:00
2011-11-27 00:03:46 +00:00
// Show shipping excluding tax
$shipping = woocommerce_price ( $this -> order_shipping );
if ( $this -> order_shipping_tax > 0 && $this -> prices_include_tax ) :
2012-08-08 08:21:27 +00:00
$tax_text = $woocommerce -> countries -> ex_tax_or_vat () . ' ' ;
2011-11-27 00:03:46 +00:00
endif ;
2012-08-08 08:21:27 +00:00
2011-09-02 14:42:04 +00:00
else :
2012-08-08 08:21:27 +00:00
2011-11-27 00:03:46 +00:00
// Show shipping including tax
$shipping = woocommerce_price ( $this -> order_shipping + $this -> order_shipping_tax );
if ( $this -> order_shipping_tax > 0 && ! $this -> prices_include_tax ) :
2012-08-08 08:21:27 +00:00
$tax_text = $woocommerce -> countries -> inc_tax_or_vat () . ' ' ;
2011-11-27 00:03:46 +00:00
endif ;
2012-08-08 08:21:27 +00:00
2011-08-29 16:09:42 +00:00
endif ;
2012-08-08 08:21:27 +00:00
2012-06-12 16:22:54 +00:00
$shipping .= sprintf ( __ ( ' <small>%svia %s</small>' , 'woocommerce' ), $tax_text , $this -> get_shipping_method () );
2012-08-08 08:21:27 +00:00
2012-06-12 16:22:54 +00:00
elseif ( $this -> get_shipping_method () ) :
$shipping = $this -> get_shipping_method ();
2011-08-09 15:16:18 +00:00
else :
2012-01-05 11:31:22 +00:00
$shipping = __ ( 'Free!' , 'woocommerce' );
2011-08-09 15:16:18 +00:00
endif ;
2012-08-08 08:21:27 +00:00
2012-05-25 06:36:38 +00:00
return apply_filters ( 'woocommerce_order_shipping_to_display' , $shipping , $this );
2011-08-09 15:16:18 +00:00
}
2012-05-25 10:54:52 +00:00
/** Get cart discount (formatted) */
function get_cart_discount_to_display () {
return apply_filters ( 'woocommerce_order_cart_discount_to_display' , woocommerce_price ( $this -> get_cart_discount () ), $this );
}
2012-08-08 08:21:27 +00:00
2012-05-25 10:54:52 +00:00
/** Get cart discount (formatted) */
function get_order_discount_to_display () {
return apply_filters ( 'woocommerce_order_discount_to_display' , woocommerce_price ( $this -> get_order_discount () ), $this );
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/** Get a product (either product or variation) */
function get_product_from_item ( $item ) {
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
if ( isset ( $item [ 'variation_id' ]) && $item [ 'variation_id' ] > 0 ) :
2012-01-27 16:38:39 +00:00
$_product = new WC_Product_Variation ( $item [ 'variation_id' ] );
2011-08-09 15:16:18 +00:00
else :
2012-01-27 16:38:39 +00:00
$_product = new WC_Product ( $item [ 'id' ] );
2011-08-09 15:16:18 +00:00
endif ;
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
return $_product ;
}
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
/** Get totals for display on pages and in emails */
2012-01-02 17:45:26 +00:00
function get_order_item_totals () {
2012-01-13 21:25:39 +00:00
global $woocommerce ;
2012-08-08 08:21:27 +00:00
2012-01-02 17:45:26 +00:00
$total_rows = array ();
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
if ( $subtotal = $this -> get_subtotal_to_display () )
2012-07-11 18:32:00 +00:00
$total_rows [ 'cart_subtotal' ] = array (
'label' => __ ( 'Cart Subtotal:' , 'woocommerce' ),
'value' => $subtotal
);
2012-08-08 08:21:27 +00:00
if ( $this -> get_cart_discount () > 0 )
2012-07-11 18:32:00 +00:00
$total_rows [ 'cart_discount' ] = array (
'label' => __ ( 'Cart Discount:' , 'woocommerce' ),
'value' => '-' . $this -> get_cart_discount_to_display ()
);
2012-08-08 08:21:27 +00:00
2012-06-12 16:22:54 +00:00
if ( $this -> get_shipping_method () )
2012-07-11 18:32:00 +00:00
$total_rows [ 'shipping' ] = array (
'label' => __ ( 'Shipping:' , 'woocommerce' ),
'value' => $this -> get_shipping_to_display ()
);
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
if ( $this -> get_total_tax () > 0 ) {
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
if ( sizeof ( $this -> get_taxes () ) > 0 ) {
2012-08-08 08:21:27 +00:00
2012-01-02 17:45:26 +00:00
$has_compound_tax = false ;
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
foreach ( $this -> get_taxes () as $tax ) {
if ( $tax [ 'compound' ] ) {
$has_compound_tax = true ;
continue ;
}
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
if ( ( $tax [ 'cart_tax' ] + $tax [ 'shipping_tax' ] ) == 0 )
continue ;
2012-08-08 08:21:27 +00:00
2012-07-11 18:32:00 +00:00
$total_rows [ sanitize_title ( $tax [ 'label' ] ) ] = array (
'label' => $tax [ 'label' ],
'value' => woocommerce_price ( ( $tax [ 'cart_tax' ] + $tax [ 'shipping_tax' ] ) )
);
2012-04-18 10:29:30 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
if ( $has_compound_tax ) {
if ( $subtotal = $this -> get_subtotal_to_display ( true ) ) {
2012-07-11 18:32:00 +00:00
$total_rows [ 'subtotal' ] = array (
'label' => __ ( 'Subtotal:' , 'woocommerce' ),
'value' => $subtotal
);
2012-04-18 10:29:30 +00:00
}
}
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
foreach ( $this -> get_taxes () as $tax ) {
if ( ! $tax [ 'compound' ] )
continue ;
if ( ( $tax [ 'cart_tax' ] + $tax [ 'shipping_tax' ] ) == 0 )
continue ;
2012-08-08 08:21:27 +00:00
2012-07-11 18:32:00 +00:00
$total_rows [ sanitize_title ( $tax [ 'label' ] ) ] = array (
'label' => $tax [ 'label' ],
'value' => woocommerce_price ( ( $tax [ 'cart_tax' ] + $tax [ 'shipping_tax' ] ) )
);
2012-04-18 10:29:30 +00:00
}
} else {
2012-07-11 18:32:00 +00:00
$total_rows [ 'tax' ] = array (
'label' => $woocommerce -> countries -> tax_or_vat (),
'value' => woocommerce_price ( $this -> get_total_tax () )
);
2012-04-18 10:29:30 +00:00
}
2012-03-12 11:26:48 +00:00
2012-04-18 10:29:30 +00:00
} elseif ( get_option ( 'woocommerce_display_cart_taxes_if_zero' ) == 'yes' ) {
2012-07-11 18:32:00 +00:00
$total_rows [ 'tax' ] = array (
'label' => $woocommerce -> countries -> tax_or_vat (),
'value' => _x ( 'N/A' , 'Relating to tax' , 'woocommerce' )
);
2012-04-18 10:29:30 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
if ( $this -> get_order_discount () > 0 )
2012-07-11 18:32:00 +00:00
$total_rows [ 'order_discount' ] = array (
'label' => __ ( 'Order Discount:' , 'woocommerce' ),
'value' => '-' . $this -> get_order_discount_to_display ()
);
2012-08-08 08:21:27 +00:00
2012-07-11 18:32:00 +00:00
$total_rows [ 'order_total' ] = array (
'label' => __ ( 'Order Total:' , 'woocommerce' ),
'value' => $this -> get_formatted_order_total ()
);
2012-08-08 08:21:27 +00:00
2012-04-18 10:29:30 +00:00
return apply_filters ( 'woocommerce_get_order_item_totals' , $total_rows , $this );
2012-01-02 17:45:26 +00:00
}
2012-08-08 08:21:27 +00:00
2011-08-16 14:06:08 +00:00
/** Output items for display in html emails */
2012-02-27 17:05:26 +00:00
function email_order_items_table ( $show_download_links = false , $show_sku = false , $show_purchase_note = false , $show_image = false , $image_size = array ( 32 , 32 ) ) {
2011-08-16 14:06:08 +00:00
2012-03-18 14:51:21 +00:00
ob_start ();
2012-08-08 08:21:27 +00:00
woocommerce_get_template ( 'emails/email-order-items.php' , array (
2012-03-18 14:51:21 +00:00
'order' => $this ,
2012-08-08 08:21:27 +00:00
'items' => $this -> get_items (),
2012-03-18 14:51:21 +00:00
'show_download_links' => $show_download_links ,
'show_sku' => $show_sku ,
'show_purchase_note' => $show_purchase_note ,
'show_image' => $show_image ,
'image_size' => $image_size
) );
2012-08-08 08:21:27 +00:00
2012-03-18 14:51:21 +00:00
$return = apply_filters ( 'woocommerce_email_order_items_table' , ob_get_clean () );
2012-02-15 14:06:44 +00:00
2012-08-08 08:21:27 +00:00
return $return ;
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2011-10-12 17:32:30 +00:00
/** Returns true if the order contains a downloadable product */
function has_downloadable_item () {
$has_downloadable_item = false ;
2012-08-08 08:21:27 +00:00
foreach ( $this -> get_items () as $item ) :
2011-10-12 17:32:30 +00:00
$_product = $this -> get_product_from_item ( $item );
2012-04-20 10:17:40 +00:00
if ( $_product -> exists () && $_product -> is_downloadable ()) :
2011-10-12 17:32:30 +00:00
$has_downloadable_item = true ;
endif ;
2012-08-08 08:21:27 +00:00
endforeach ;
2011-10-12 17:32:30 +00:00
return $has_downloadable_item ;
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/** Generates a URL so that a customer can checkout/pay for their (unpaid - pending) order via a link */
function get_checkout_payment_url () {
2012-08-08 08:21:27 +00:00
2012-01-06 17:14:31 +00:00
$payment_page = get_permalink ( woocommerce_get_page_id ( 'pay' ));
2012-08-08 08:21:27 +00:00
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_force_ssl_checkout' ) == 'yes' || is_ssl ()) $payment_page = str_replace ( 'http:' , 'https:' , $payment_page );
2012-08-08 08:21:27 +00:00
2012-01-06 17:14:31 +00:00
return apply_filters ( 'woocommerce_get_checkout_payment_url' , add_query_arg ( 'pay_for_order' , 'true' , add_query_arg ( 'order' , $this -> order_key , add_query_arg ( 'order_id' , $this -> id , $payment_page ))));
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/** Generates a URL so that a customer can cancel their (unpaid - pending) order */
function get_cancel_order_url () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2012-01-06 17:14:31 +00:00
return apply_filters ( 'woocommerce_get_cancel_order_url' , $woocommerce -> nonce_url ( 'cancel_order' , add_query_arg ( 'cancel_order' , 'true' , add_query_arg ( 'order' , $this -> order_key , add_query_arg ( 'order_id' , $this -> id , trailingslashit ( home_url () ))))));
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/** Gets a downloadable products file url */
2011-11-09 23:06:17 +00:00
function get_downloadable_file_url ( $item_id , $variation_id ) {
2012-08-08 08:21:27 +00:00
2012-07-31 11:58:00 +00:00
$download_id = $variation_id > 0 ? $variation_id : $item_id ;
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
$user_email = $this -> billing_email ;
2012-08-08 08:21:27 +00:00
2012-07-31 11:58:00 +00:00
return add_query_arg ( 'download_file' , $download_id , add_query_arg ( 'order' , $this -> order_key , add_query_arg ( 'email' , $user_email , trailingslashit ( home_url () ) ) ) );
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/**
* Adds a note ( comment ) to the order
*
* @ param string $note Note to add
2011-09-23 14:22:21 +00:00
* @ param int $is_customer_note Is this a note for the customer ?
2011-08-09 15:16:18 +00:00
*/
2011-09-23 14:22:21 +00:00
function add_order_note ( $note , $is_customer_note = 0 ) {
2012-08-08 08:21:27 +00:00
2012-02-27 15:02:44 +00:00
$comment_post_ID = $this -> id ;
2012-08-08 08:21:27 +00:00
$comment_author = __ ( 'WooCommerce' , 'woocommerce' );
$comment_author_email = strtolower ( __ ( 'WooCommerce' , 'woocommerce' ) ) . '@' . str_replace ( 'www.' , '' , $_SERVER [ 'HTTP_HOST' ]);
2012-02-27 15:02:44 +00:00
$comment_author_url = '' ;
2012-06-20 17:19:15 +00:00
$comment_content = $note ;
2012-02-27 15:02:44 +00:00
$comment_agent = 'WooCommerce' ;
2012-07-11 14:53:40 +00:00
$comment_type = 'order_note' ;
2012-04-14 23:37:37 +00:00
$comment_parent = 0 ;
$comment_approved = 1 ;
$commentdata = compact ( 'comment_post_ID' , 'comment_author' , 'comment_author_email' , 'comment_author_url' , 'comment_content' , 'comment_agent' , 'comment_type' , 'comment_parent' , 'comment_approved' );
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
$comment_id = wp_insert_comment ( $commentdata );
2012-08-08 08:21:27 +00:00
2012-07-11 14:53:40 +00:00
add_comment_meta ( $comment_id , 'is_customer_note' , $is_customer_note );
2012-08-08 08:21:27 +00:00
2012-02-27 15:02:44 +00:00
if ( $is_customer_note ) do_action ( 'woocommerce_new_customer_note' , array ( 'order_id' => $this -> id , 'customer_note' => $note ) );
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
return $comment_id ;
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
}
/**
2012-08-01 02:15:37 +00:00
* Updates status of order
2011-08-09 15:16:18 +00:00
*
* @ param string $new_status Status to change the order to
* @ param string $note Optional note to add
*/
2012-02-27 15:02:44 +00:00
function update_status ( $new_status_slug , $note = '' ) {
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
if ( $note ) $note .= ' ' ;
2012-08-08 08:21:27 +00:00
2012-03-01 12:25:23 +00:00
$old_status = get_term_by ( 'slug' , sanitize_title ( $this -> status ), 'shop_order_status' );
2012-02-27 15:02:44 +00:00
$new_status = get_term_by ( 'slug' , sanitize_title ( $new_status_slug ), 'shop_order_status' );
2012-08-08 08:21:27 +00:00
2012-02-14 14:45:35 +00:00
if ( $new_status ) {
2012-08-08 08:21:27 +00:00
2012-02-27 15:02:44 +00:00
wp_set_object_terms ( $this -> id , array ( $new_status -> slug ), 'shop_order_status' , false );
2012-08-08 08:21:27 +00:00
2012-02-14 14:45:35 +00:00
if ( $this -> status != $new_status -> slug ) {
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
// Status was changed
2012-02-27 15:02:44 +00:00
do_action ( 'woocommerce_order_status_' . $new_status -> slug , $this -> id );
do_action ( 'woocommerce_order_status_' . $this -> status . '_to_' . $new_status -> slug , $this -> id );
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
$this -> add_order_note ( $note . sprintf ( __ ( 'Order status changed from %s to %s.' , 'woocommerce' ), __ ( $old_status -> name , 'woocommerce' ), __ ( $new_status -> name , 'woocommerce' ) ) );
2012-02-27 15:02:44 +00:00
2012-07-10 15:52:52 +00:00
// Record the completed date of the order
2012-08-08 08:21:27 +00:00
if ( $new_status -> slug == 'completed' )
2012-07-10 15:52:52 +00:00
update_post_meta ( $this -> id , '_completed_date' , current_time ( 'mysql' ) );
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
if ( $new_status -> slug == 'processing' || $new_status -> slug == 'completed' || $new_status -> slug == 'on-hold' ) {
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
// Record the sales
$this -> record_product_sales ();
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
// Increase coupon usage counts
$this -> increase_coupon_usage_counts ();
}
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
// If the order is cancelled, restore used coupons
2012-08-08 08:21:27 +00:00
if ( $new_status -> slug == 'cancelled' )
2012-07-10 15:52:52 +00:00
$this -> decrease_coupon_usage_counts ();
2012-08-08 08:21:27 +00:00
2012-02-14 14:45:35 +00:00
}
2012-02-27 15:02:44 +00:00
2012-02-14 14:45:35 +00:00
}
2012-08-08 08:21:27 +00:00
2012-05-28 09:19:29 +00:00
delete_transient ( 'woocommerce_processing_order_count' );
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/**
* Cancel the order and restore the cart ( before payment )
*
* @ param string $note Optional note to add
*/
function cancel_order ( $note = '' ) {
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
unset ( $_SESSION [ 'order_awaiting_payment' ]);
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
$this -> update_status ( 'cancelled' , $note );
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
}
/**
* When a payment is complete this function is called
*
* Most of the time this should mark an order as 'processing' so that admin can process / post the items
* If the cart contains only downloadable items then the order is 'complete' since the admin needs to take no action
* Stock levels are reduced at this point
2011-11-01 15:41:47 +00:00
* Sales are also recorded for products
2012-06-03 12:12:08 +00:00
* Finally , record the date of payment
2011-08-09 15:16:18 +00:00
*/
function payment_complete () {
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
unset ( $_SESSION [ 'order_awaiting_payment' ] );
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
if ( $this -> status == 'on-hold' || $this -> status == 'pending' || $this -> status == 'failed' ) {
2012-08-08 08:21:27 +00:00
2011-11-18 11:29:55 +00:00
$downloadable_order = false ;
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
if ( sizeof ( $this -> get_items () ) > 0 ) {
foreach ( $this -> get_items () as $item ) {
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
if ( $item [ 'id' ] > 0 ) {
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
$_product = $this -> get_product_from_item ( $item );
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
if ( $_product -> is_downloadable () && $_product -> is_virtual () ) {
$downloadable_order = true ;
continue ;
}
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
}
$downloadable_order = false ;
break ;
}
}
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
$new_order_status = ( $downloadable_order ) ? 'completed' : 'processing' ;
2012-08-08 08:21:27 +00:00
2011-11-18 11:29:55 +00:00
$new_order_status = apply_filters ( 'woocommerce_payment_complete_order_status' , $new_order_status , $this -> id );
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
$this -> update_status ( $new_order_status );
2012-08-08 08:21:27 +00:00
2012-06-08 10:46:10 +00:00
add_post_meta ( $this -> id , '_paid_date' , current_time ( 'mysql' ), true );
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
$this_order = array (
'ID' => $this -> id ,
'post_date' => current_time ( 'mysql' , 0 ),
'post_date_gmt' => current_time ( 'mysql' , 1 )
);
wp_update_post ( $this_order );
$this -> reduce_order_stock (); // Payment is complete so reduce stock levels
2012-08-08 08:21:27 +00:00
2011-11-18 11:29:55 +00:00
do_action ( 'woocommerce_payment_complete' , $this -> id );
2012-06-03 12:12:08 +00:00
}
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2011-11-01 15:41:47 +00:00
/**
* Record sales
*/
function record_product_sales () {
2012-08-08 08:21:27 +00:00
if ( get_post_meta ( $this -> id , '_recorded_sales' , true ) == 'yes' )
2012-07-10 15:52:52 +00:00
return ;
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
if ( sizeof ( $this -> get_items () ) > 0 ) {
foreach ( $this -> get_items () as $item ) {
if ( $item [ 'id' ] > 0 ) {
$sales = ( int ) get_post_meta ( $item [ 'id' ], 'total_sales' , true );
$sales += ( int ) $item [ 'qty' ];
2012-08-08 08:21:27 +00:00
if ( $sales )
2012-07-10 15:52:52 +00:00
update_post_meta ( $item [ 'id' ], 'total_sales' , $sales );
}
}
}
2012-08-08 08:21:27 +00:00
2011-11-18 11:29:55 +00:00
update_post_meta ( $this -> id , '_recorded_sales' , 'yes' );
2012-07-10 15:52:52 +00:00
}
/**
* Increase applied coupon counts
*/
function get_used_coupons () {
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
$coupons = get_post_meta ( $this -> id , 'coupons' , true );
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
return array_map ( 'trim' , explode ( ',' , $coupons ) );
}
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
/**
* Increase applied coupon counts
*/
function increase_coupon_usage_counts () {
global $woocommerce ;
2012-08-08 08:21:27 +00:00
if ( get_post_meta ( $this -> id , '_recorded_coupon_usage_counts' , true ) == 'yes' )
2012-07-10 15:52:52 +00:00
return ;
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
if ( sizeof ( $this -> get_used_coupons () ) > 0 ) {
foreach ( $this -> get_used_coupons () as $code ) {
if ( ! $code )
continue ;
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
$coupon = $woocommerce -> coupon ( $code );
$coupon -> inc_usage_count ();
}
}
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
update_post_meta ( $this -> id , '_recorded_coupon_usage_counts' , 'yes' );
2011-11-01 15:41:47 +00:00
}
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
/**
* Decrease applied coupon counts
*/
function decrease_coupon_usage_counts () {
global $woocommerce ;
2012-08-08 08:21:27 +00:00
if ( get_post_meta ( $this -> id , '_recorded_coupon_usage_counts' , true ) != 'yes' )
2012-07-10 15:52:52 +00:00
return ;
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
if ( sizeof ( $this -> get_used_coupons () ) > 0 ) {
foreach ( $this -> get_used_coupons () as $code ) {
if ( ! $code )
continue ;
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
$coupon = $woocommerce -> coupon ( $code );
$coupon -> dcr_usage_count ();
}
}
2012-08-08 08:21:27 +00:00
2012-07-10 15:52:52 +00:00
delete_post_meta ( $this -> id , '_recorded_coupon_usage_counts' );
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
/**
* Reduce stock levels
*/
function reduce_order_stock () {
2012-08-08 08:21:27 +00:00
2012-04-09 13:58:00 +00:00
if ( get_option ( 'woocommerce_manage_stock' ) == 'yes' && sizeof ( $this -> get_items () ) > 0 ) {
2012-08-08 08:21:27 +00:00
2011-10-30 11:18:12 +00:00
// Reduce stock levels and do any other actions with products in the cart
2012-04-09 13:58:00 +00:00
foreach ( $this -> get_items () as $item ) {
2012-08-08 08:21:27 +00:00
2012-04-09 13:58:00 +00:00
if ( $item [ 'id' ] > 0 ) {
2011-10-30 11:18:12 +00:00
$_product = $this -> get_product_from_item ( $item );
2012-08-08 08:21:27 +00:00
2012-04-20 10:17:40 +00:00
if ( $_product && $_product -> exists () && $_product -> managing_stock () ) {
2012-08-08 08:21:27 +00:00
2011-10-30 11:18:12 +00:00
$old_stock = $_product -> stock ;
2012-08-08 08:21:27 +00:00
2011-10-30 11:18:12 +00:00
$new_quantity = $_product -> reduce_stock ( $item [ 'qty' ] );
2012-08-08 08:21:27 +00:00
2012-01-05 11:31:22 +00:00
$this -> add_order_note ( sprintf ( __ ( 'Item #%s stock reduced from %s to %s.' , 'woocommerce' ), $item [ 'id' ], $old_stock , $new_quantity ) );
2012-08-08 08:21:27 +00:00
2012-04-17 11:53:02 +00:00
$this -> send_stock_notifications ( $_product , $new_quantity , $item [ 'qty' ] );
2012-08-08 08:21:27 +00:00
2012-04-09 13:58:00 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-09 13:58:00 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-09 13:58:00 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-09 13:58:00 +00:00
do_action ( 'woocommerce_reduce_order_stock' , $this );
2012-08-08 08:21:27 +00:00
2012-01-05 11:31:22 +00:00
$this -> add_order_note ( __ ( 'Order item stock reduced successfully.' , 'woocommerce' ) );
2012-08-08 08:21:27 +00:00
2012-04-09 13:58:00 +00:00
}
2012-08-08 08:21:27 +00:00
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-17 11:53:02 +00:00
/**
* send_stock_notifications function .
*/
function send_stock_notifications ( $product , $new_stock , $qty_ordered ) {
2012-08-08 08:21:27 +00:00
2012-04-17 11:53:02 +00:00
// Backorders
if ( $new_stock < 0 )
do_action ( 'woocommerce_product_on_backorder' , array ( 'product' => $product , 'order_id' => $this -> id , 'quantity' => $qty_ordered ) );
2012-08-08 08:21:27 +00:00
2012-04-17 11:53:02 +00:00
// stock status notifications
$notification_sent = false ;
2012-08-08 08:21:27 +00:00
2012-04-17 11:53:02 +00:00
if ( get_option ( 'woocommerce_notify_no_stock' ) == 'yes' && get_option ( 'woocommerce_notify_no_stock_amount' ) >= $new_stock ) {
do_action ( 'woocommerce_no_stock' , $product );
$notification_sent = true ;
}
if ( ! $notification_sent && get_option ( 'woocommerce_notify_low_stock' ) == 'yes' && get_option ( 'woocommerce_notify_low_stock_amount' ) >= $new_stock ) {
do_action ( 'woocommerce_low_stock' , $product );
$notification_sent = true ;
}
}
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
/**
* List order notes ( public ) for the customer
*/
function get_customer_order_notes () {
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
$notes = array ();
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
$args = array (
'post_id' => $this -> id ,
'approve' => 'approve' ,
'type' => ''
);
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
remove_filter ( 'comments_clauses' , 'woocommerce_exclude_order_comments' );
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
$comments = get_comments ( $args );
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
foreach ( $comments as $comment ) :
$is_customer_note = get_comment_meta ( $comment -> comment_ID , 'is_customer_note' , true );
2012-02-24 20:20:17 +00:00
$comment -> comment_content = make_clickable ( $comment -> comment_content );
2012-08-08 08:21:27 +00:00
if ( $is_customer_note )
2012-02-24 20:20:17 +00:00
$notes [] = $comment ;
2011-09-23 14:22:21 +00:00
endforeach ;
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
add_filter ( 'comments_clauses' , 'woocommerce_exclude_order_comments' );
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
return ( array ) $notes ;
2012-08-08 08:21:27 +00:00
2011-09-23 14:22:21 +00:00
}
2011-11-06 15:45:22 +00:00
}
/**
* Order Item Meta
2012-08-08 08:21:27 +00:00
*
2011-11-06 15:45:22 +00:00
* A Simple class for managing order item meta so plugins add it in the correct format
*/
class order_item_meta {
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
var $meta ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
/**
* Constructor
*/
function __construct ( $item_meta = '' ) {
$this -> meta = array ();
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
if ( $item_meta ) $this -> meta = $item_meta ;
}
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
/**
* Load item meta
*/
function new_order_item ( $item ) {
if ( $item ) :
2011-11-16 10:08:22 +00:00
do_action ( 'woocommerce_order_item_meta' , $this , $item );
2011-11-06 15:45:22 +00:00
endif ;
}
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
/**
* Add meta
*/
function add ( $name , $value ) {
$this -> meta [] = array (
2012-08-08 08:21:27 +00:00
'meta_name' => $name ,
2011-11-06 15:45:22 +00:00
'meta_value' => $value
);
}
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
/**
* Display meta in a formatted list
*/
function display ( $flat = false , $return = false ) {
global $woocommerce ;
2012-08-08 08:21:27 +00:00
2012-06-10 17:45:43 +00:00
if ( $this -> meta && is_array ( $this -> meta ) ) :
2012-08-08 08:21:27 +00:00
2012-06-10 17:45:43 +00:00
if ( ! $flat ) $output = '<dl class="variation">' ; else $output = '' ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
$meta_list = array ();
2012-08-08 08:21:27 +00:00
2012-06-10 17:45:43 +00:00
foreach ( $this -> meta as $meta ) :
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
$name = $meta [ 'meta_name' ];
$value = $meta [ 'meta_value' ];
2012-08-08 08:21:27 +00:00
2012-06-10 17:45:43 +00:00
if ( ! $value ) continue ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
// If this is a term slug, get the term's nice name
2012-06-10 17:45:43 +00:00
if ( taxonomy_exists ( esc_attr ( str_replace ( 'attribute_' , '' , $name ) ) ) ) :
2011-11-06 15:45:22 +00:00
$term = get_term_by ( 'slug' , $value , esc_attr ( str_replace ( 'attribute_' , '' , $name )));
if ( ! is_wp_error ( $term ) && $term -> name ) :
$value = $term -> name ;
endif ;
else :
$value = ucfirst ( $value );
endif ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
if ( $flat ) :
$meta_list [] = $woocommerce -> attribute_label ( str_replace ( 'attribute_' , '' , $name )) . ': ' . $value ;
else :
$meta_list [] = '<dt>' . $woocommerce -> attribute_label ( str_replace ( 'attribute_' , '' , $name )) . ':</dt><dd>' . $value . '</dd>' ;
endif ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
endforeach ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
if ( $flat ) :
2012-06-10 17:45:43 +00:00
$output .= implode ( " , \n " , $meta_list );
2011-11-06 15:45:22 +00:00
else :
$output .= implode ( '' , $meta_list );
endif ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
if ( ! $flat ) $output .= '</dl>' ;
if ( $return ) return $output ; else echo $output ;
2012-08-08 08:21:27 +00:00
2011-11-06 15:45:22 +00:00
endif ;
}
2012-08-08 08:21:27 +00:00
2012-01-27 16:38:39 +00:00
}
2012-07-23 18:14:02 +00:00
/** Deprecated */
2012-01-27 16:38:39 +00:00
class woocommerce_order extends WC_Order {
2012-08-08 08:21:27 +00:00
public function __construct ( $id = '' ) {
parent :: __construct ( $id );
}
2011-08-09 15:16:18 +00:00
}