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
2012-08-14 22:43:48 +00:00
* @ package WooCommerce / Classes
2013-02-20 17:14:46 +00:00
* @ category Class
2012-08-14 20:19:41 +00:00
* @ 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-12-14 21:48:36 +00:00
/** @public int Order (post) ID */
public $id ;
2012-08-15 17:08:42 +00:00
2012-08-14 22:43:48 +00:00
/**
* Get the order if ID is passed , otherwise the order is new and empty .
*
* @ access public
* @ param string $id ( default : '' )
* @ return void
*/
2012-12-14 21:48:36 +00:00
public function __construct ( $id = '' ) {
2012-12-03 16:36:54 +00:00
$this -> prices_include_tax = get_option ( 'woocommerce_prices_include_tax' ) == 'yes' ? true : false ;
$this -> tax_display_cart = get_option ( 'woocommerce_tax_display_cart' );
$this -> display_totals_ex_tax = $this -> tax_display_cart == 'excl' ? true : false ;
$this -> display_cart_ex_tax = $this -> tax_display_cart == 'excl' ? true : false ;
2012-08-14 22:43:48 +00:00
if ( $id > 0 )
$this -> get_order ( $id );
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Gets an order from the database .
*
* @ access public
* @ param int $id ( default : 0 )
* @ return bool
*/
2012-12-14 21:48:36 +00:00
public function get_order ( $id = 0 ) {
2012-08-14 22:43:48 +00:00
if ( ! $id )
return false ;
if ( $result = get_post ( $id ) ) {
2012-08-08 08:21:27 +00:00
$this -> populate ( $result );
2011-08-09 15:16:18 +00:00
return true ;
2012-08-14 22:43:48 +00:00
}
2011-08-09 15:16:18 +00:00
return false ;
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Populates an order from the loaded post data .
*
* @ access public
* @ param mixed $result
* @ return void
*/
2012-12-14 21:48:36 +00:00
public function populate ( $result ) {
2011-08-09 15:16:18 +00:00
// Standard post data
2013-07-31 14:12:53 +00:00
$this -> id = $result -> ID ;
$this -> order_date = $result -> post_date ;
$this -> modified_date = $result -> post_modified ;
$this -> customer_message = $result -> post_excerpt ;
2013-08-16 15:43:26 +00:00
$this -> customer_note = $result -> post_excerpt ;
2013-07-31 14:12:53 +00:00
$this -> post_status = $result -> post_status ;
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
// Get status
2013-08-16 15:43:26 +00:00
$terms = wp_get_object_terms ( $this -> id , 'shop_order_status' , array ( 'fields' => 'slugs' ) );
2013-08-06 10:41:20 +00:00
$this -> status = isset ( $terms [ 0 ] ) ? $terms [ 0 ] : apply_filters ( 'woocommerce_default_order_status' , 'pending' );
2012-01-13 00:46:56 +00:00
}
2012-08-08 08:21:27 +00:00
2013-08-16 15:43:26 +00:00
/**
* __isset function .
*
* @ access public
* @ param mixed $key
* @ return bool
*/
public function __isset ( $key ) {
if ( ! $this -> id )
return false ;
return metadata_exists ( 'post' , $this -> id , '_' . $key );
}
/**
* __get function .
*
* @ access public
* @ param mixed $key
* @ return mixed
*/
public function __get ( $key ) {
// Get values or default if not set
if ( 'completed_date' == $key )
$value = ( $value = get_post_meta ( $this -> id , '_completed_date' , true ) ) ? $value : $this -> modified_date ;
elseif ( 'user_id' == $key )
$value = ( $value = get_post_meta ( $this -> id , '_customer_user' , true ) ) ? absint ( $value ) : '' ;
else
$value = get_post_meta ( $this -> id , '_' . $key , true );
return $value ;
}
2012-08-14 22:43:48 +00:00
/**
* Check if an order key is valid .
*
* @ access public
* @ param mixed $key
* @ return bool
*/
2012-12-14 21:48:36 +00:00
public function key_is_valid ( $key ) {
2012-08-14 22:43:48 +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
*/
2012-12-14 21:48:36 +00:00
public 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-08-14 22:43:48 +00:00
/**
* Get a formatted billing address for the order .
*
* @ access public
* @ return string
*/
2012-12-14 21:48:36 +00:00
public function get_formatted_billing_address () {
2012-08-14 22:43:48 +00:00
if ( ! $this -> formatted_billing_address ) {
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
// Formatted Addresses
2013-04-07 08:31:21 +00:00
$address = apply_filters ( 'woocommerce_order_formatted_billing_address' , array (
'first_name' => $this -> billing_first_name ,
2012-01-13 00:46:56 +00:00
'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
2013-04-07 08:31:21 +00:00
), $this );
2012-08-08 08:21:27 +00:00
2013-10-25 19:02:21 +00:00
$this -> formatted_billing_address = WC () -> countries -> get_formatted_address ( $address );
2012-08-14 22:43:48 +00:00
}
2012-01-13 00:46:56 +00:00
return $this -> formatted_billing_address ;
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Get the billing address in an array .
*
* @ access public
2013-11-04 10:20:45 +00:00
* @ return string
2012-08-14 22:43:48 +00:00
*/
2012-12-14 21:48:36 +00:00
public function get_billing_address () {
2012-08-14 22:43:48 +00:00
if ( ! $this -> billing_address ) {
2012-01-13 00:46:56 +00:00
// 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 );
2012-08-14 22:43:48 +00:00
}
2012-01-13 00:46:56 +00:00
return $this -> billing_address ;
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Get a formatted shipping address for the order .
*
* @ access public
* @ return void
*/
2012-12-14 21:48:36 +00:00
public function get_formatted_shipping_address () {
2012-08-14 22:43:48 +00:00
if ( ! $this -> formatted_shipping_address ) {
if ( $this -> shipping_address_1 ) {
2012-08-08 08:21:27 +00:00
2012-01-13 00:46:56 +00:00
// Formatted Addresses
2013-04-07 08:31:21 +00:00
$address = apply_filters ( 'woocommerce_order_formatted_shipping_address' , array (
2012-01-13 00:46:56 +00:00
'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
2013-04-07 08:31:21 +00:00
), $this );
2012-08-08 08:21:27 +00:00
2013-10-25 19:02:21 +00:00
$this -> formatted_shipping_address = WC () -> countries -> get_formatted_address ( $address );
2012-08-14 22:43:48 +00:00
}
}
2012-01-13 00:46:56 +00:00
return $this -> formatted_shipping_address ;
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Get the shipping address in an array .
*
* @ access public
* @ return array
*/
2012-12-14 21:48:36 +00:00
public function get_shipping_address () {
2012-08-14 22:43:48 +00:00
if ( ! $this -> shipping_address ) {
if ( $this -> shipping_address_1 ) {
2012-01-13 00:46:56 +00:00
// 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 );
2012-08-14 22:43:48 +00:00
}
}
2012-01-13 00:46:56 +00:00
return $this -> shipping_address ;
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
2012-11-12 16:08:05 +00:00
* Return an array of items / products within this order .
2012-11-27 16:22:47 +00:00
*
2012-08-14 22:43:48 +00:00
* @ access public
2012-11-12 16:08:05 +00:00
* @ param string $type Types of line items to get ( array or string )
* @ return void
2012-08-14 22:43:48 +00:00
*/
2012-12-14 21:48:36 +00:00
public function get_items ( $type = '' ) {
2013-10-25 19:02:21 +00:00
global $wpdb ;
2012-11-27 16:22:47 +00:00
2012-11-12 16:08:05 +00:00
if ( empty ( $type ) )
$type = array ( 'line_item' );
2012-11-27 16:22:47 +00:00
2013-02-15 09:54:06 +00:00
if ( ! is_array ( $type ) )
2012-11-12 16:08:05 +00:00
$type = array ( $type );
2012-11-27 16:22:47 +00:00
2012-11-12 16:08:05 +00:00
$type = array_map ( 'esc_attr' , $type );
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
$line_items = $wpdb -> get_results ( $wpdb -> prepare ( "
SELECT order_item_id , order_item_name , order_item_type
FROM { $wpdb -> prefix } woocommerce_order_items
WHERE order_id = % d
AND order_item_type IN ( '" . implode( "' , '", $type ) . "' )
ORDER BY order_item_id
" , $this->id ) );
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
$items = array ();
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
foreach ( $line_items as $item ) {
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
// Place line item into array to return
$items [ $item -> order_item_id ][ 'name' ] = $item -> order_item_name ;
$items [ $item -> order_item_id ][ 'type' ] = $item -> order_item_type ;
$items [ $item -> order_item_id ][ 'item_meta' ] = $this -> get_item_meta ( $item -> order_item_id );
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
// Put meta into item array
foreach ( $items [ $item -> order_item_id ][ 'item_meta' ] as $name => $value ) {
$key = substr ( $name , 0 , 1 ) == '_' ? substr ( $name , 1 ) : $name ;
$items [ $item -> order_item_id ][ $key ] = $value [ 0 ];
2012-10-19 17:59:17 +00:00
}
2012-10-18 13:47:21 +00:00
}
2013-02-24 15:00:42 +00:00
return apply_filters ( 'woocommerce_order_get_items' , $items , $this );
2012-01-13 00:46:56 +00:00
}
2012-08-08 08:21:27 +00:00
2013-02-15 09:54:06 +00:00
/**
* Gets order total - formatted for display .
*
* @ access public
* @ return string
*/
public function get_item_count ( $type = '' ) {
2013-10-25 19:02:21 +00:00
global $wpdb ;
2013-02-15 09:54:06 +00:00
if ( empty ( $type ) )
$type = array ( 'line_item' );
if ( ! is_array ( $type ) )
$type = array ( $type );
$items = $this -> get_items ( $type );
$count = 0 ;
foreach ( $items as $item ) {
if ( ! empty ( $item [ 'qty' ] ) )
$count += $item [ 'qty' ];
else
$count ++ ;
}
return apply_filters ( 'woocommerce_get_item_count' , $count , $type , $this );
}
2012-11-12 13:41:54 +00:00
/**
* Return an array of fees within this order .
*
* @ access public
* @ return array
*/
2012-12-14 21:48:36 +00:00
public function get_fees () {
2012-11-12 16:08:05 +00:00
return $this -> get_items ( 'fee' );
2012-11-12 13:41:54 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
/**
* Return an array of taxes within this order .
2012-11-27 16:22:47 +00:00
*
2012-11-13 14:54:34 +00:00
* @ access public
* @ return void
*/
2012-12-14 21:48:36 +00:00
public function get_taxes () {
2012-11-13 14:54:34 +00:00
return $this -> get_items ( 'tax' );
}
2012-11-12 13:41:54 +00:00
2013-08-16 15:43:26 +00:00
/**
* Return an array of shipping costs within this order .
*
* @ return void
*/
public function get_shipping_methods () {
return $this -> get_items ( 'shipping' );
}
2013-10-03 14:59:08 +00:00
/**
* Check whether this order has a specific shipping method or not
*
* @ return void
*/
public function has_shipping_method ( $method_id ) {
2013-10-03 15:02:26 +00:00
$shipping_methods = $this -> get_shipping_methods ();
2013-10-03 14:59:08 +00:00
$has_method = false ;
if ( ! $shipping_methods )
return false ;
2013-10-03 15:02:26 +00:00
foreach ( $shipping_methods as $shipping_method ) {
2013-10-03 14:59:08 +00:00
if ( $shipping_method [ 'method_id' ] == $method_id )
$has_method = true ;
}
return $has_method ;
}
2013-03-27 18:20:56 +00:00
/**
* Get taxes , merged by code , formatted ready for output .
*
* @ access public
* @ return void
*/
public function get_tax_totals () {
$taxes = $this -> get_items ( 'tax' );
$tax_totals = array ();
foreach ( $taxes as $key => $tax ) {
$code = $tax [ 'name' ];
if ( ! isset ( $tax_totals [ $code ] ) ) {
$tax_totals [ $code ] = new stdClass ();
$tax_totals [ $code ] -> amount = 0 ;
}
$tax_totals [ $code ] -> is_compound = $tax [ 'compound' ];
$tax_totals [ $code ] -> label = isset ( $tax [ 'label' ] ) ? $tax [ 'label' ] : $tax [ 'name' ];
$tax_totals [ $code ] -> amount += $tax [ 'tax_amount' ] + $tax [ 'shipping_tax_amount' ];
2013-10-24 12:15:42 +00:00
$tax_totals [ $code ] -> formatted_amount = woocommerce_price ( woocommerce_round_tax_total ( $tax_totals [ $code ] -> amount ) );
2013-03-27 18:20:56 +00:00
}
return apply_filters ( 'woocommerce_order_tax_totals' , $tax_totals , $this );
}
2012-10-23 16:41:42 +00:00
/**
* has_meta function for order items .
2012-11-27 16:22:47 +00:00
*
2012-10-23 16:41:42 +00:00
* @ access public
* @ return array of meta data
*/
2012-12-14 21:48:36 +00:00
public function has_meta ( $order_item_id ) {
2012-10-23 16:41:42 +00:00
global $wpdb ;
2012-11-27 16:22:47 +00:00
2012-10-23 16:41:42 +00:00
return $wpdb -> get_results ( $wpdb -> prepare ( " SELECT meta_key, meta_value, meta_id, order_item_id
FROM { $wpdb -> prefix } woocommerce_order_itemmeta WHERE order_item_id = % d
ORDER BY meta_key , meta_id " , absint( $order_item_id ) ), ARRAY_A );
}
2012-11-27 16:22:47 +00:00
2012-10-18 13:47:21 +00:00
/**
* Get order item meta .
2012-11-27 16:22:47 +00:00
*
2012-10-18 13:47:21 +00:00
* @ access public
* @ param mixed $item_id
* @ param string $key ( default : '' )
2012-10-23 16:41:42 +00:00
* @ param bool $single ( default : false )
2012-10-18 13:47:21 +00:00
* @ return void
*/
2012-12-14 21:48:36 +00:00
public function get_item_meta ( $order_item_id , $key = '' , $single = false ) {
2012-10-23 16:41:42 +00:00
return get_metadata ( 'order_item' , $order_item_id , $key , $single );
2012-10-18 13:47:21 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-16 17:20:36 +00:00
/** Total Getters *******************************************************/
2011-11-26 23:33:57 +00:00
/**
2012-08-14 22:43:48 +00:00
* Gets the total ( product ) discount amount - these are applied before tax .
*
* @ access public
* @ return float
2011-11-26 23:33:57 +00:00
*/
2012-12-14 21:48:36 +00:00
public function get_cart_discount () {
2013-10-17 11:43:44 +00:00
return apply_filters ( 'woocommerce_order_amount_cart_discount' , ( double ) $this -> cart_discount , $this );
2011-11-26 23:33:57 +00:00
}
2012-08-08 08:21:27 +00:00
2011-11-26 23:33:57 +00:00
/**
2012-08-14 22:43:48 +00:00
* Gets the total ( product ) discount amount - these are applied before tax .
*
* @ access public
* @ return float
2011-11-26 23:33:57 +00:00
*/
2012-12-14 21:48:36 +00:00
public function get_order_discount () {
2013-10-17 11:43:44 +00:00
return apply_filters ( 'woocommerce_order_amount_order_discount' , ( double ) $this -> order_discount , $this );
2011-11-26 23:33:57 +00:00
}
2012-08-08 08:21:27 +00:00
2011-11-26 23:33:57 +00:00
/**
2012-08-14 22:43:48 +00:00
* Gets the total discount amount - both kinds
*
* @ access public
* @ return float
2011-11-26 23:33:57 +00:00
*/
2012-12-14 21:48:36 +00:00
public function get_total_discount () {
2013-10-17 11:43:44 +00:00
return apply_filters ( 'woocommerce_order_amount_total_discount' , $this -> get_cart_discount () + $this -> get_order_discount (), $this );
2011-11-26 23:33:57 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
2013-10-17 11:43:44 +00:00
* Gets shipping tax amount .
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return float
*/
2013-10-17 11:43:44 +00:00
public function get_cart_tax () {
return apply_filters ( 'woocommerce_order_amount_cart_tax' , ( double ) $this -> order_tax , $this );
2011-11-26 23:33:57 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Gets shipping tax amount .
*
* @ access public
* @ return float
*/
2012-12-14 21:48:36 +00:00
public function get_shipping_tax () {
2013-10-17 11:43:44 +00:00
return apply_filters ( 'woocommerce_order_amount_shipping_tax' , ( double ) $this -> order_shipping_tax , $this );
2012-08-12 08:28:07 +00:00
}
2012-08-14 22:43:48 +00:00
/**
2013-10-17 11:43:44 +00:00
* Gets shipping and product tax .
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return float
*/
2013-10-17 11:43:44 +00:00
public function get_total_tax () {
2013-10-24 12:15:42 +00:00
return apply_filters ( 'woocommerce_order_amount_total_tax' , woocommerce_round_tax_total ( $this -> get_cart_tax () + $this -> get_shipping_tax () ), $this );
2013-10-17 11:43:44 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-19 14:05:03 +00:00
/**
2013-10-17 11:43:44 +00:00
* Gets shipping total .
2012-11-27 16:22:47 +00:00
*
2012-11-19 14:05:03 +00:00
* @ access public
2013-10-17 11:43:44 +00:00
* @ return float
2012-11-19 14:05:03 +00:00
*/
2013-10-17 11:43:44 +00:00
public function get_total_shipping () {
return apply_filters ( 'woocommerce_order_amount_total_shipping' , ( double ) $this -> order_shipping , $this );
2012-11-19 14:05:03 +00:00
}
2012-08-14 22:43:48 +00:00
/**
2013-10-17 11:43:44 +00:00
* Gets order total .
2012-08-14 22:43:48 +00:00
*
2013-10-17 11:43:44 +00:00
* @ access public
* @ return float
2012-08-14 22:43:48 +00:00
*/
2013-10-17 11:43:44 +00:00
public function get_total () {
return apply_filters ( 'woocommerce_order_amount_total' , ( double ) $this -> order_total , $this );
2013-08-16 15:43:26 +00:00
}
2012-08-14 22:43:48 +00:00
/**
* Get item subtotal - this is the cost before discount .
*
* @ access public
* @ param mixed $item
* @ param bool $inc_tax ( default : false )
* @ param bool $round ( default : true )
* @ return float
*/
2012-12-14 21:48:36 +00:00
public function get_item_subtotal ( $item , $inc_tax = false , $round = true ) {
2012-08-14 22:43:48 +00:00
if ( $inc_tax )
2013-10-25 12:25:53 +00:00
$price = ( $item [ 'line_subtotal' ] + $item [ 'line_subtotal_tax' ] ) / $item [ 'qty' ];
2012-08-14 22:43:48 +00:00
else
2012-01-24 16:56:10 +00:00
$price = ( $item [ 'line_subtotal' ] / $item [ 'qty' ] );
2012-08-08 08:21:27 +00:00
2013-10-17 11:43:44 +00:00
$price = $round ? number_format ( $price , 2 , '.' , '' ) : $price ;
return apply_filters ( 'woocommerce_order_amount_item_subtotal' , $price , $this );
}
2012-08-14 22:43:48 +00:00
/**
* Get line subtotal - this is the cost before discount .
*
* @ access public
* @ param mixed $item
* @ param bool $inc_tax ( default : false )
* @ param bool $round ( default : true )
* @ return float
*/
2012-12-14 21:48:36 +00:00
public function get_line_subtotal ( $item , $inc_tax = false , $round = true ) {
2012-08-14 22:43:48 +00:00
if ( $inc_tax )
2012-01-24 16:56:10 +00:00
$price = $item [ 'line_subtotal' ] + $item [ 'line_subtotal_tax' ];
2012-08-14 22:43:48 +00:00
else
2012-01-24 16:56:10 +00:00
$price = $item [ 'line_subtotal' ];
2012-08-08 08:21:27 +00:00
2013-10-17 11:43:44 +00:00
$price = $round ? number_format ( $price , 2 , '.' , '' ) : $price ;
return apply_filters ( 'woocommerce_order_amount_line_subtotal' , $price , $this );
}
2012-08-14 22:43:48 +00:00
/**
* Calculate item cost - useful for gateways .
*
* @ access public
* @ param mixed $item
* @ param bool $inc_tax ( default : false )
* @ param bool $round ( default : true )
* @ return float
*/
2012-12-14 21:48:36 +00:00
public function get_item_total ( $item , $inc_tax = false , $round = true ) {
2012-08-14 22:43:48 +00:00
if ( $inc_tax )
2013-10-25 12:25:53 +00:00
$price = ( $item [ 'line_total' ] + $item [ 'line_tax' ] ) / $item [ 'qty' ];
2012-08-14 22:43:48 +00:00
else
2012-01-24 16:56:10 +00:00
$price = $item [ 'line_total' ] / $item [ 'qty' ];
2013-10-17 11:43:44 +00:00
$price = $round ? number_format ( $price , 2 , '.' , '' ) : $price ;
return apply_filters ( 'woocommerce_order_amount_item_total' , $price , $this );
2012-01-24 16:56:10 +00:00
}
2012-08-08 08:21:27 +00:00
2013-10-17 11:43:44 +00:00
/**
* Calculate line total - useful for gateways .
*
* @ access public
* @ param mixed $item
* @ param bool $inc_tax ( default : false )
* @ return float
*/
public function get_line_total ( $item , $inc_tax = false ) {
$line_total = $inc_tax ? number_format ( $item [ 'line_total' ] + $item [ 'line_tax' ] , 2 , '.' , '' ) : number_format ( $item [ 'line_total' ] , 2 , '.' , '' );
return apply_filters ( 'woocommerce_order_amount_line_total' , $line_total , $this );
}
2012-08-14 22:43:48 +00:00
/**
* Calculate item tax - useful for gateways .
*
* @ access public
* @ param mixed $item
* @ param bool $round ( default : true )
* @ return float
*/
2012-12-14 21:48:36 +00:00
public function get_item_tax ( $item , $round = true ) {
2012-01-24 16:56:10 +00:00
$price = $item [ 'line_tax' ] / $item [ 'qty' ];
2013-10-24 15:19:28 +00:00
$price = $round ? woocommerce_round_tax_total ( $price ) : $price ;
return apply_filters ( 'woocommerce_order_amount_item_tax' , $price , $item , $round , $this );
2011-11-29 00:52:57 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
2013-10-17 11:43:44 +00:00
* Calculate line tax - useful for gateways .
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ param mixed $item
* @ return float
*/
2013-10-17 11:43:44 +00:00
public function get_line_tax ( $item ) {
2013-10-24 15:19:28 +00:00
return apply_filters ( 'woocommerce_order_amount_line_tax' , woocommerce_round_tax_total ( $item [ 'line_tax' ] ), $item , $this );
2011-11-28 17:03:53 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
2013-10-17 11:43:44 +00:00
* Gets shipping total .
2012-08-14 22:43:48 +00:00
*
2013-10-17 11:43:44 +00:00
* @ deprecated As of 2.1 , use of get_total_shipping () is preferred
2012-08-14 22:43:48 +00:00
* @ access public
* @ return float
*/
2013-10-17 11:43:44 +00:00
public function get_shipping () {
_deprecated_function ( 'get_shipping' , '2.1' , 'get_total_shipping' );
return $this -> get_total_shipping ();
}
/**
* get_order_total function . Alias for get_total ()
*
* @ deprecated As of 2.1 , use of get_total () is preferred
* @ access public
* @ return void
*/
public function get_order_total () {
_deprecated_function ( 'get_order_total' , '2.1' , 'get_total' );
return $this -> get_total ();
2012-01-24 16:56:10 +00:00
}
2012-08-08 08:21:27 +00:00
2012-04-16 17:20:36 +00:00
/** End Total Getters *******************************************************/
2013-10-17 11:43:44 +00:00
/**
* Gets formatted shipping method title .
*
* @ return string
*/
public function get_shipping_method () {
$labels = array ();
// Backwards compat < 2.1 - get shipping title stored in meta
if ( $this -> shipping_method_title )
$labels [] = $this -> shipping_method_title ;
// 2.1+ get line items for shipping
$shipping_methods = $this -> get_shipping_methods ();
foreach ( $shipping_methods as $shipping )
$labels [] = $shipping [ 'name' ];
return apply_filters ( 'woocommerce_order_shipping_method' , implode ( ', ' , $labels ), $this );
}
2012-08-14 22:43:48 +00:00
/**
* Gets line subtotal - formatted for display .
*
* @ access public
* @ param mixed $item
* @ return string
*/
2012-12-19 19:37:13 +00:00
public function get_formatted_line_subtotal ( $item , $tax_display = '' ) {
if ( ! $tax_display )
$tax_display = $this -> tax_display_cart ;
2012-01-04 23:01:47 +00:00
$subtotal = 0 ;
2012-08-08 08:21:27 +00:00
2013-10-25 19:02:21 +00:00
if ( ! isset ( $item [ 'line_subtotal' ] ) || ! isset ( $item [ 'line_subtotal_tax' ] ) )
return ;
2012-08-08 08:21:27 +00:00
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'excl' ) {
2013-10-25 19:02:21 +00:00
$ex_tax_label = $this -> prices_include_tax ? 1 : 0 ;
2012-12-03 16:36:54 +00:00
$subtotal = woocommerce_price ( $this -> get_line_subtotal ( $item ), array ( 'ex_tax_label' => $ex_tax_label ) );
} else {
2012-01-22 02:36:10 +00:00
$subtotal = woocommerce_price ( $this -> get_line_subtotal ( $item , true ) );
2012-12-03 16:36:54 +00:00
}
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-08-14 22:43:48 +00:00
/**
* Gets order total - formatted for display .
*
* @ access public
* @ return string
*/
2012-12-14 21:48:36 +00:00
public function get_formatted_order_total () {
2012-04-27 02:18:18 +00:00
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-08-14 22:43:48 +00:00
/**
* Gets subtotal - subtotal is shown before discounts , but with localised taxes .
*
* @ access public
* @ param bool $compound ( default : false )
* @ return string
*/
2012-12-19 19:37:13 +00:00
public function get_subtotal_to_display ( $compound = false , $tax_display = '' ) {
if ( ! $tax_display )
$tax_display = $this -> tax_display_cart ;
2012-01-02 00:49:00 +00:00
$subtotal = 0 ;
2012-08-08 08:21:27 +00:00
2012-12-03 16:36:54 +00:00
if ( ! $compound ) {
foreach ( $this -> get_items () as $item ) {
2012-08-08 08:21:27 +00:00
2013-10-25 19:02:21 +00:00
if ( ! isset ( $item [ 'line_subtotal' ] ) || ! isset ( $item [ 'line_subtotal_tax' ] ) )
return ;
2012-08-08 08:21:27 +00:00
2013-10-25 19:02:21 +00:00
$subtotal += $item [ 'line_subtotal' ];
2012-08-08 08:21:27 +00:00
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'incl' ) {
2012-01-22 02:36:10 +00:00
$subtotal += $item [ 'line_subtotal_tax' ];
2012-12-03 16:36:54 +00:00
}
}
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
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'excl' && $this -> prices_include_tax )
2013-10-25 19:02:21 +00:00
$subtotal .= ' <small>' . WC () -> countries -> ex_tax_or_vat () . '</small>' ;
2012-08-08 08:21:27 +00:00
2012-12-03 16:36:54 +00:00
} else {
2012-08-08 08:21:27 +00:00
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'incl' )
2012-12-03 16:36:54 +00:00
return ;
2012-08-08 08:21:27 +00:00
2012-12-03 16:36:54 +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-12-03 16:36:54 +00:00
}
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
// Add Shipping Costs
2013-10-17 11:43:44 +00:00
$subtotal += $this -> get_total_shipping ();
2012-08-08 08:21:27 +00:00
2012-01-04 23:01:47 +00:00
// Remove non-compound taxes
2012-12-03 16:36:54 +00:00
foreach ( $this -> get_taxes () as $tax ) {
2012-08-08 08:21:27 +00:00
2012-11-13 14:54:34 +00:00
if ( ! empty ( $tax [ 'compound' ] ) ) continue ;
2012-08-08 08:21:27 +00:00
2012-11-13 14:54:34 +00:00
$subtotal = $subtotal + $tax [ 'tax_amount' ] + $tax [ 'shipping_tax_amount' ];
2012-08-08 08:21:27 +00:00
2012-12-03 16:36:54 +00:00
}
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-11-13 14:54:34 +00:00
$subtotal = woocommerce_price ( $subtotal );
2012-12-03 16:36:54 +00:00
}
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
2012-08-14 22:43:48 +00:00
/**
* Gets shipping ( formatted ) .
*
* @ access public
* @ return string
*/
2012-12-19 19:37:13 +00:00
public function get_shipping_to_display ( $tax_display = '' ) {
if ( ! $tax_display )
$tax_display = $this -> tax_display_cart ;
2012-12-03 16:36: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
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'excl' ) {
2011-08-09 15:16:18 +00:00
2011-11-27 00:03:46 +00:00
// Show shipping excluding tax
2012-12-03 16:36:54 +00:00
$shipping = woocommerce_price ( $this -> order_shipping );
if ( $this -> order_shipping_tax > 0 && $this -> prices_include_tax )
2013-10-25 19:02:21 +00:00
$tax_text = WC () -> countries -> ex_tax_or_vat () . ' ' ;
2012-08-08 08:21:27 +00:00
2012-12-03 16:36:54 +00:00
} else {
2012-08-08 08:21:27 +00:00
2011-11-27 00:03:46 +00:00
// Show shipping including tax
2012-12-03 16:36:54 +00:00
$shipping = woocommerce_price ( $this -> order_shipping + $this -> order_shipping_tax );
if ( $this -> order_shipping_tax > 0 && ! $this -> prices_include_tax )
2013-10-25 19:02:21 +00:00
$tax_text = WC () -> countries -> inc_tax_or_vat () . ' ' ;
2012-08-08 08:21:27 +00:00
2012-12-03 16:36:54 +00:00
}
2012-08-08 08:21:27 +00:00
2012-10-16 09:45:33 +00:00
$shipping .= sprintf ( __ ( ' <small>%svia %s</small>' , 'woocommerce' ), $tax_text , $this -> get_shipping_method () );
2012-08-08 08:21:27 +00:00
2012-12-03 16:36:54 +00:00
} elseif ( $this -> get_shipping_method () ) {
2012-06-12 16:22:54 +00:00
$shipping = $this -> get_shipping_method ();
2012-12-03 16:36:54 +00:00
} else {
2012-10-16 09:45:33 +00:00
$shipping = __ ( 'Free!' , 'woocommerce' );
2012-12-03 16:36:54 +00:00
}
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
2012-08-14 22:43:48 +00:00
/**
* Get cart discount ( formatted ) .
*
* @ access public
* @ return string .
*/
2012-12-14 21:48:36 +00:00
public function get_cart_discount_to_display () {
2012-05-25 10:54:52 +00:00
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-08-14 22:43:48 +00:00
/**
* Get cart discount ( formatted ) .
*
* @ access public
* @ return string
*/
2012-12-14 21:48:36 +00:00
public function get_order_discount_to_display () {
2012-05-25 10:54:52 +00:00
return apply_filters ( 'woocommerce_order_discount_to_display' , woocommerce_price ( $this -> get_order_discount () ), $this );
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Get a product ( either product or variation ) .
*
* @ access public
* @ param mixed $item
* @ return WC_Product
*/
2012-12-14 21:48:36 +00:00
public function get_product_from_item ( $item ) {
2012-11-21 18:07:45 +00:00
$_product = get_product ( $item [ 'variation_id' ] ? $item [ 'variation_id' ] : $item [ 'product_id' ] );
2013-04-23 15:38:48 +00:00
return apply_filters ( 'woocommerce_get_product_from_item' , $_product , $item , $this );
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Get totals for display on pages and in emails .
*
* @ access public
* @ return array
*/
2012-12-19 19:37:13 +00:00
public function get_order_item_totals ( $tax_display = '' ) {
if ( ! $tax_display )
$tax_display = $this -> tax_display_cart ;
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-11-27 16:22:47 +00:00
2012-11-12 16:08:05 +00:00
if ( $fees = $this -> get_fees () )
2012-11-12 17:15:54 +00:00
foreach ( $fees as $id => $fee ) {
2013-08-15 15:49:09 +00:00
if ( $fee [ 'line_total' ] + $fee [ 'line_tax' ] == 0 )
continue ;
2012-11-27 16:22:47 +00:00
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'excl' ) {
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
$total_rows [ 'fee_' . $id ] = array (
'label' => $fee [ 'name' ],
'value' => woocommerce_price ( $fee [ 'line_total' ] )
);
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
} else {
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
$total_rows [ 'fee_' . $id ] = array (
'label' => $fee [ 'name' ],
'value' => woocommerce_price ( $fee [ 'line_total' ] + $fee [ 'line_tax' ] )
);
}
2012-11-12 16:08:05 +00:00
}
2012-11-27 16:22:47 +00:00
2012-10-12 11:48:06 +00:00
// Tax for tax exclusive prices
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'excl' ) {
2013-09-23 15:10:09 +00:00
if ( get_option ( 'woocommerce_tax_total_display' ) == 'itemized' ) {
foreach ( $this -> get_tax_totals () as $code => $tax ) {
$total_rows [ sanitize_title ( $code ) ] = array (
'label' => $tax -> label . ':' ,
'value' => $tax -> formatted_amount
);
}
} else {
$total_rows [ 'tax' ] = array (
'label' => WC () -> countries -> tax_or_vat () . ':' ,
'value' => woocommerce_price ( $this -> get_total_tax () )
2012-07-11 18:32:00 +00:00
);
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-11-27 16:22:47 +00:00
2012-10-12 11:48:06 +00:00
// Tax for inclusive prices
2012-12-19 19:37:13 +00:00
if ( $tax_display == 'incl' ) {
2012-11-27 16:22:47 +00:00
2012-10-12 11:48:06 +00:00
$tax_string_array = array ();
2012-11-27 16:22:47 +00:00
2013-09-23 15:10:09 +00:00
if ( get_option ( 'woocommerce_tax_total_display' ) == 'itemized' ) {
foreach ( $this -> get_tax_totals () as $code => $tax ) {
$tax_string_array [] = sprintf ( '%s %s' , $tax -> formatted_amount , $tax -> label );
}
} else {
$tax_string_array [] = sprintf ( '%s %s' , woocommerce_price ( $this -> get_total_tax () ), WC () -> countries -> tax_or_vat () );
2012-10-12 11:48:06 +00:00
}
2012-11-27 16:22:47 +00:00
2013-02-06 18:07:52 +00:00
if ( ! empty ( $tax_string_array ) )
$total_rows [ 'order_total' ][ 'value' ] .= ' ' . sprintf ( __ ( '(Includes %s)' , 'woocommerce' ), implode ( ', ' , $tax_string_array ) );
2012-10-12 11:48:06 +00:00
}
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
2012-08-14 22:43:48 +00:00
/**
* Output items for display in html emails .
*
* @ access public
* @ param bool $show_download_links ( default : false )
* @ param bool $show_sku ( default : false )
* @ param bool $show_purchase_note ( default : false )
* @ param bool $show_image ( default : false )
2012-09-17 00:53:17 +00:00
* @ param array $image_size ( default : array ( 32 , 32 )
* @ param bool plain text
2012-08-14 22:43:48 +00:00
* @ return string
*/
2012-12-14 21:48:36 +00:00
public function email_order_items_table ( $show_download_links = false , $show_sku = false , $show_purchase_note = false , $show_image = false , $image_size = array ( 32 , 32 ), $plain_text = false ) {
2011-08-16 14:06:08 +00:00
2012-03-18 14:51:21 +00:00
ob_start ();
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
$template = $plain_text ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php' ;
2012-08-08 08:21:27 +00:00
2012-09-17 00:53:17 +00:00
woocommerce_get_template ( $template , 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
2013-08-30 18:42:19 +00:00
$return = apply_filters ( 'woocommerce_email_order_items_table' , ob_get_clean (), $this );
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
2012-08-28 15:21:54 +00:00
/**
* Checks if product download is permitted
2012-11-27 16:22:47 +00:00
*
2012-08-28 15:21:54 +00:00
* @ access public
* @ return bool
*/
2012-12-14 21:48:36 +00:00
public function is_download_permitted () {
2012-08-28 15:21:54 +00:00
return apply_filters ( 'woocommerce_order_is_download_permitted' , $this -> status == 'completed' || ( get_option ( 'woocommerce_downloads_grant_access_after_payment' ) == 'yes' && $this -> status == 'processing' ), $this );
}
2012-08-14 22:43:48 +00:00
/**
* Returns true if the order contains a downloadable product .
*
* @ access public
* @ return bool
*/
2012-12-14 21:48:36 +00:00
public function has_downloadable_item () {
2011-10-12 17:32:30 +00:00
$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 );
2013-07-24 20:24:53 +00:00
if ( $_product && $_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
2012-08-14 22:43:48 +00:00
/**
2013-05-31 15:13:14 +00:00
* Generates a URL so that a customer can pay for their ( unpaid - pending ) order . Pass 'true' for the checkout version which doesn ' t offer gateway choices .
2012-08-14 22:43:48 +00:00
*
* @ access public
2013-05-31 15:13:14 +00:00
* @ param boolean $on_checkout
2012-08-14 22:43:48 +00:00
* @ return string
*/
2013-05-31 15:13:14 +00:00
public function get_checkout_payment_url ( $on_checkout = false ) {
2012-08-08 08:21:27 +00:00
2013-07-23 16:05:01 +00:00
$pay_url = woocommerce_get_endpoint_url ( 'order-pay' , $this -> id , get_permalink ( woocommerce_get_page_id ( 'checkout' ) ) );
2013-05-31 15:13:14 +00:00
if ( get_option ( 'woocommerce_force_ssl_checkout' ) == 'yes' || is_ssl () )
$pay_url = str_replace ( 'http:' , 'https:' , $pay_url );
if ( $on_checkout ) {
$pay_url = add_query_arg ( 'key' , $this -> order_key , $pay_url );
} else {
$pay_url = add_query_arg ( array ( 'pay_for_order' => 'true' , 'key' => $this -> order_key ), $pay_url );
}
return apply_filters ( 'woocommerce_get_checkout_payment_url' , $pay_url , $this );
}
/**
* Generates a URL for the thanks page ( order received )
*
* @ access public
* @ return string
*/
public function get_checkout_order_received_url () {
2013-07-23 16:05:01 +00:00
$order_received_url = woocommerce_get_endpoint_url ( 'order-received' , $this -> id , get_permalink ( woocommerce_get_page_id ( 'checkout' ) ) );
2013-05-31 15:13:14 +00:00
if ( get_option ( 'woocommerce_force_ssl_checkout' ) == 'yes' || is_ssl () )
$order_received_url = str_replace ( 'http:' , 'https:' , $order_received_url );
$order_received_url = add_query_arg ( 'key' , $this -> order_key , $order_received_url );
2012-08-08 08:21:27 +00:00
2013-05-31 15:13:14 +00:00
return apply_filters ( 'woocommerce_get_checkout_order_received_url' , $order_received_url , $this );
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
/**
* Generates a URL so that a customer can cancel their ( unpaid - pending ) order .
*
* @ access public
* @ return string
*/
2012-12-14 21:48:36 +00:00
public function get_cancel_order_url () {
2013-06-11 16:55:55 +00:00
return apply_filters ( 'woocommerce_get_cancel_order_url' , wp_nonce_url ( add_query_arg ( array ( 'cancel_order' => 'true' , 'order' => $this -> order_key , 'order_id' => $this -> id ), trailingslashit ( home_url () ) ), 'woocommerce-cancel_order' ) );
2011-08-09 15:16:18 +00:00
}
2012-08-08 08:21:27 +00:00
2013-06-04 16:33:43 +00:00
/**
* Generates a URL to view an order from the my account page
*
* @ return string
*/
public function get_view_order_url () {
2013-07-23 16:05:01 +00:00
$view_order_url = woocommerce_get_endpoint_url ( 'view-order' , $this -> id , get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
2013-06-04 16:33:43 +00:00
return apply_filters ( 'woocommerce_get_view_order_url' , $view_order_url , $this );
}
2012-08-08 08:21:27 +00:00
2012-08-28 15:21:54 +00:00
/**
* Gets any downloadable product file urls .
*
2013-09-20 16:01:09 +00:00
* @ deprecated as of 2.1 get_item_downloads is prefered as downloads are more than just file urls
2012-08-28 15:21:54 +00:00
* @ param int $product_id product identifier
* @ param int $variation_id variation identifier , or null
* @ param array $item the item
* @ return array available downloadable file urls
*/
2012-12-14 21:48:36 +00:00
public function get_downloadable_file_urls ( $product_id , $variation_id , $item ) {
2012-08-28 15:21:54 +00:00
global $wpdb ;
2013-09-20 16:01:09 +00:00
_deprecated_function ( 'get_downloadable_file_urls' , '2.1' , 'get_item_downloads' );
2012-08-28 15:21:54 +00:00
$download_file = $variation_id > 0 ? $variation_id : $product_id ;
2012-11-21 18:07:45 +00:00
$_product = get_product ( $download_file );
2012-08-28 15:21:54 +00:00
$user_email = $this -> billing_email ;
$results = $wpdb -> get_results ( $wpdb -> prepare ( "
SELECT download_id
2013-09-20 16:01:09 +00:00
FROM { $wpdb -> prefix } woocommerce_downloadable_product_permissions
2012-08-28 15:21:54 +00:00
WHERE user_email = % s
AND order_key = % s
AND product_id = % s
" , $user_email , $this->order_key , $download_file ) );
$file_urls = array ();
foreach ( $results as $result ) {
if ( $_product -> has_file ( $result -> download_id ) ) {
2013-09-20 16:01:09 +00:00
$file_urls [ $_product -> get_file_download_path ( $result -> download_id ) ] = $this -> get_download_url ( $download_file , $result -> download_id );
}
}
return apply_filters ( 'woocommerce_get_downloadable_file_urls' , $file_urls , $product_id , $variation_id , $item );
}
2013-03-10 16:24:04 +00:00
2013-09-20 16:01:09 +00:00
/**
* Get the downloadable files for an item in this order
* @ param array $item
* @ return array
*/
public function get_item_downloads ( $item ) {
global $wpdb ;
2013-03-10 16:24:04 +00:00
2013-09-20 16:01:09 +00:00
$product_id = $item [ 'variation_id' ] > 0 ? $item [ 'variation_id' ] : $item [ 'product_id' ];
$product = get_product ( $product_id );
$download_ids = $wpdb -> get_col ( $wpdb -> prepare ( "
SELECT download_id
FROM { $wpdb -> prefix } woocommerce_downloadable_product_permissions
WHERE user_email = % s
AND order_key = % s
AND product_id = % s
2013-10-25 19:02:21 +00:00
ORDER BY permission_id
2013-09-20 16:01:09 +00:00
" , $this->billing_email , $this->order_key , $product_id ) );
$files = array ();
foreach ( $download_ids as $download_id ) {
if ( $product -> has_file ( $download_id ) ) {
$files [ $download_id ] = $product -> get_file ( $download_id );
$files [ $download_id ][ 'download_url' ] = $this -> get_download_url ( $product_id , $download_id );
2012-08-28 15:21:54 +00:00
}
}
2013-09-20 16:01:09 +00:00
return apply_filters ( 'woocommerce_get_item_downloads' , $files , $item , $this );
}
/**
* Get the Download URL
* @ param int $product_id
* @ param int $download_id
* @ return string
*/
public function get_download_url ( $product_id , $download_id ) {
return add_query_arg ( array (
'download_file' => $product_id ,
'order' => $this -> order_key ,
'email' => $this -> billing_email ,
'key' => $download_id
), trailingslashit ( home_url () ) );
2012-08-28 15:21:54 +00:00
}
2011-08-09 15:16:18 +00:00
/**
* Adds a note ( comment ) to the order
*
2012-08-14 22:43:48 +00:00
* @ access public
* @ param string $note Note to add
* @ param int $is_customer_note ( default : 0 ) Is this a note for the customer ?
* @ return id Comment ID
2011-08-09 15:16:18 +00:00
*/
2012-12-14 21:48:36 +00:00
public function add_order_note ( $note , $is_customer_note = 0 ) {
2012-08-08 08:21:27 +00:00
2012-10-18 10:33:47 +00:00
$is_customer_note = intval ( $is_customer_note );
2013-07-30 11:35:18 +00:00
if ( is_user_logged_in () && current_user_can ( 'manage_woocommerce' ) ) {
$user = get_user_by ( 'id' , get_current_user_id () );
$comment_author = $user -> display_name ;
$comment_author_email = $user -> user_email ;
} else {
$comment_author = __ ( 'WooCommerce' , 'woocommerce' );
$comment_author_email = strtolower ( __ ( 'WooCommerce' , 'woocommerce' ) ) . '@' ;
$comment_author_email .= isset ( $_SERVER [ 'HTTP_HOST' ] ) ? str_replace ( 'www.' , '' , $_SERVER [ 'HTTP_HOST' ] ) : 'noreply.com' ;
$comment_author_email = sanitize_email ( $comment_author_email );
}
2012-12-11 17:05:31 +00:00
2012-02-27 15:02:44 +00:00
$comment_post_ID = $this -> id ;
$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 ;
2013-06-28 09:50:05 +00:00
$commentdata = apply_filters ( 'woocommerce_new_order_note_data' , compact ( 'comment_post_ID' , 'comment_author' , 'comment_author_email' , 'comment_author_url' , 'comment_content' , 'comment_agent' , 'comment_type' , 'comment_parent' , 'comment_approved' ), array ( 'order_id' => $this -> id , 'is_customer_note' => $is_customer_note ) );
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
2013-06-28 09:50:05 +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 ;
2011-08-09 15:16:18 +00:00
}
2012-08-14 22:43:48 +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
*
2012-08-14 22:43:48 +00:00
* @ access public
* @ param string $new_status_slug Status to change the order to
* @ param string $note ( default : '' ) Optional note to add
* @ return void
2011-08-09 15:16:18 +00:00
*/
2012-12-14 21:48:36 +00:00
public function update_status ( $new_status_slug , $note = '' ) {
2012-08-08 08:21:27 +00:00
2012-11-27 16:22:47 +00:00
if ( $note )
2012-09-12 12:05:53 +00:00
$note .= ' ' ;
2012-08-08 08:21:27 +00:00
2012-09-12 12:05:53 +00:00
$old_status = get_term_by ( 'slug' , sanitize_title ( $this -> status ), 'shop_order_status' );
$new_status = get_term_by ( 'slug' , sanitize_title ( $new_status_slug ), 'shop_order_status' );
2012-11-27 16:22:47 +00:00
2012-09-12 12:05:53 +00:00
if ( $new_status ) {
2012-08-08 08:21:27 +00:00
2012-09-12 12:05:53 +00:00
wp_set_object_terms ( $this -> id , array ( $new_status -> slug ), 'shop_order_status' , false );
2012-08-08 08:21:27 +00:00
2013-10-10 09:49:08 +00:00
if ( $this -> id && $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-09-12 12:05:53 +00:00
do_action ( 'woocommerce_order_status_' . $new_status -> slug , $this -> id );
2012-02-27 15:02:44 +00:00
do_action ( 'woocommerce_order_status_' . $this -> status . '_to_' . $new_status -> slug , $this -> id );
2012-09-12 12:05:53 +00:00
do_action ( 'woocommerce_order_status_changed' , $this -> id , $this -> status , $new_status -> slug );
2012-11-27 16:22:47 +00:00
2013-02-12 13:52:50 +00:00
if ( $old_status )
$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
2013-03-25 14:56:17 +00:00
// Update last modified
wp_update_post ( array ( 'ID' => $this -> id ) );
2012-12-06 14:48:50 +00:00
$this -> status = $new_status -> slug ;
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
2012-08-14 22:43:48 +00:00
2011-08-09 15:16:18 +00:00
/**
* Cancel the order and restore the cart ( before payment )
*
2012-08-14 22:43:48 +00:00
* @ access public
* @ param string $note ( default : '' ) Optional note to add
* @ return void
2011-08-09 15:16:18 +00:00
*/
2012-12-14 21:48:36 +00:00
public function cancel_order ( $note = '' ) {
2013-10-25 19:02:21 +00:00
unset ( WC () -> 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
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return void
2011-08-09 15:16:18 +00:00
*/
2012-12-14 21:48:36 +00:00
public function payment_complete () {
2012-11-27 16:22:47 +00:00
2013-09-25 02:46:03 +00:00
do_action ( 'woocommerce_pre_payment_complete' , $this -> id );
2013-10-25 19:02:21 +00:00
if ( ! empty ( WC () -> session -> order_awaiting_payment ) )
unset ( WC () -> session -> order_awaiting_payment );
2012-08-08 08:21:27 +00:00
2013-02-12 13:52:50 +00:00
if ( $this -> id && ( $this -> status == 'on-hold' || $this -> status == 'pending' || $this -> status == 'failed' ) ) {
2012-08-08 08:21:27 +00:00
2012-10-17 11:34:05 +00:00
$order_needs_processing = true ;
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
if ( sizeof ( $this -> get_items () ) > 0 ) {
2012-11-27 16:22:47 +00:00
2012-06-03 12:12:08 +00:00
foreach ( $this -> get_items () as $item ) {
2012-11-27 16:22:47 +00:00
2012-10-23 16:41:42 +00:00
if ( $item [ 'product_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-10-17 11:34:05 +00:00
if ( ( $_product -> is_downloadable () && $_product -> is_virtual () ) || ! apply_filters ( 'woocommerce_order_item_needs_processing' , true , $_product , $this -> id ) ) {
$order_needs_processing = false ;
2012-06-03 12:12:08 +00:00
continue ;
}
2012-08-08 08:21:27 +00:00
2012-06-03 12:12:08 +00:00
}
2012-10-17 11:34:05 +00:00
$order_needs_processing = true ;
2012-06-03 12:12:08 +00:00
break ;
}
}
2012-08-08 08:21:27 +00:00
2012-10-17 11:34:05 +00:00
$new_order_status = $order_needs_processing ? 'processing' : 'completed' ;
2012-08-08 08:21:27 +00:00
2012-10-17 11:34:05 +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 );
2013-03-14 16:00:31 +00:00
if ( apply_filters ( 'woocommerce_payment_complete_reduce_order_stock' , true , $this -> id ) )
$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 );
2013-09-25 02:46:03 +00:00
} else {
do_action ( 'woocommerce_payment_complete_order_status_' . $this -> status , $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
2012-08-14 22:43:48 +00:00
2011-11-01 15:41:47 +00:00
/**
* Record sales
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return void
2011-11-01 15:41:47 +00:00
*/
2012-12-14 21:48:36 +00:00
public 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 ) {
2012-10-23 16:41:42 +00:00
if ( $item [ 'product_id' ] > 0 ) {
$sales = ( int ) get_post_meta ( $item [ 'product_id' ], 'total_sales' , true );
2012-07-10 15:52:52 +00:00
$sales += ( int ) $item [ 'qty' ];
2012-08-08 08:21:27 +00:00
if ( $sales )
2012-10-23 16:41:42 +00:00
update_post_meta ( $item [ 'product_id' ], 'total_sales' , $sales );
2012-07-10 15:52:52 +00:00
}
}
}
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
}
2012-08-14 22:43:48 +00:00
2012-07-10 15:52:52 +00:00
/**
2012-12-11 17:02:08 +00:00
* Get coupon codes only .
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return array
2012-07-10 15:52:52 +00:00
*/
2012-12-14 21:48:36 +00:00
public function get_used_coupons () {
2012-08-08 08:21:27 +00:00
2012-12-11 17:02:08 +00:00
$codes = array ();
$coupons = $this -> get_items ( 'coupon' );
foreach ( $coupons as $item_id => $item ) {
$codes [] = trim ( $item [ 'name' ] );
}
2012-08-08 08:21:27 +00:00
2012-12-11 17:02:08 +00:00
return $codes ;
2012-07-10 15:52:52 +00:00
}
2012-08-08 08:21:27 +00:00
2012-08-14 22:43:48 +00:00
2012-07-10 15:52:52 +00:00
/**
* Increase applied coupon counts
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return void
2012-07-10 15:52:52 +00:00
*/
2012-12-14 21:48:36 +00:00
public function increase_coupon_usage_counts () {
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-12-31 18:25:09 +00:00
$coupon = new WC_Coupon ( $code );
2013-10-01 10:48:27 +00:00
$used_by = $this -> user_id ;
if ( ! $used_by )
$used_by = $this -> billing_email ;
$coupon -> inc_usage_count ( $used_by );
2012-07-10 15:52:52 +00:00
}
}
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-08-14 22:43:48 +00:00
2012-07-10 15:52:52 +00:00
/**
* Decrease applied coupon counts
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return void
2012-07-10 15:52:52 +00:00
*/
2012-12-14 21:48:36 +00:00
public function decrease_coupon_usage_counts () {
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-12-31 18:25:09 +00:00
$coupon = new WC_Coupon ( $code );
2013-10-01 10:48:27 +00:00
$used_by = $this -> user_id ;
if ( ! $used_by )
$used_by = $this -> billing_email ;
$coupon -> dcr_usage_count ( $used_by );
2012-07-10 15:52:52 +00:00
}
}
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
2012-08-14 22:43:48 +00:00
2011-08-09 15:16:18 +00:00
/**
* Reduce stock levels
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return void
2011-08-09 15:16:18 +00:00
*/
2012-12-14 21:48:36 +00:00
public 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-10-23 16:41:42 +00:00
if ( $item [ 'product_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
2013-03-18 05:38:22 +00:00
$qty = apply_filters ( 'woocommerce_order_item_quantity' , $item [ 'qty' ], $this , $item );
$new_quantity = $_product -> reduce_stock ( $qty );
2012-08-08 08:21:27 +00:00
2012-10-23 16:41:42 +00:00
$this -> add_order_note ( sprintf ( __ ( 'Item #%s stock reduced from %s to %s.' , 'woocommerce' ), $item [ 'product_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-10-16 09:45:33 +00:00
$this -> add_order_note ( __ ( 'Order item stock reduced successfully.' , 'woocommerce' ) );
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-08-14 22:43:48 +00:00
2012-04-17 11:53:02 +00:00
/**
* send_stock_notifications function .
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ param object $product
* @ param int $new_stock
* @ param int $qty_ordered
* @ return void
2012-04-17 11:53:02 +00:00
*/
2012-12-14 21:48:36 +00:00
public 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
2012-08-14 22:43:48 +00:00
2011-09-23 14:22:21 +00:00
/**
* List order notes ( public ) for the customer
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return array
2011-09-23 14:22:21 +00:00
*/
2012-12-14 21:48:36 +00:00
public 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
2013-10-26 15:30:43 +00:00
remove_filter ( 'comments_clauses' , array ( 'WC_Comments' , '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
2013-10-26 15:30:43 +00:00
add_filter ( 'comments_clauses' , array ( 'WC_Comments' , '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
}
2013-10-16 13:14:15 +00:00
/**
* Checks if an order needs payment , based on status and order total
*
* @ access public
* @ return bool
*/
public function needs_payment () {
$valid_order_statuses = apply_filters ( 'woocommerce_valid_order_statuses_for_payment' , array ( 'pending' , 'failed' ), $this );
if ( in_array ( $this -> status , $valid_order_statuses ) && $this -> get_total () > 0 )
$needs_payment = true ;
else
$needs_payment = false ;
return apply_filters ( 'woocommerce_order_needs_payment' , $needs_payment , $this , $valid_order_statuses );
}
2013-03-18 05:38:22 +00:00
}