2011-08-09 15:16:18 +00:00
< ? php
2017-11-28 13:04:38 +00:00
/**
* Regular order
*
* @ package WooCommerce\Classes
* @ version 2.2 . 0
*/
2015-11-06 09:22:19 +00:00
2018-01-19 15:42:48 +00:00
defined ( 'ABSPATH' ) || exit ;
2015-11-06 09:22:19 +00:00
2011-08-09 15:16:18 +00:00
/**
2016-06-21 19:06:39 +00:00
* Order Class .
*
* These are regular WooCommerce orders , which extend the abstract order class .
2011-08-09 15:16:18 +00:00
*/
2014-06-13 13:35:53 +00:00
class WC_Order extends WC_Abstract_Order {
2014-07-08 18:23:22 +00:00
2015-07-15 15:02:26 +00:00
/**
2016-06-21 19:06:39 +00:00
* Stores data about status changes so relevant hooks can be fired .
2017-11-28 13:04:38 +00:00
*
2016-06-21 19:06:39 +00:00
* @ var bool | array
*/
2016-09-09 12:34:49 +00:00
protected $status_transition = false ;
2016-06-21 19:06:39 +00:00
/**
2017-03-15 16:36:53 +00:00
* Order Data array . This is the core order data exposed in APIs since 3.0 . 0.
2017-11-28 13:04:38 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-24 11:34:19 +00:00
* @ var array
*/
2016-09-09 12:34:49 +00:00
protected $data = array (
2017-11-28 13:04:38 +00:00
// Abstract order props.
2016-08-24 11:34:19 +00:00
'parent_id' => 0 ,
'status' => '' ,
'currency' => '' ,
'version' => '' ,
'prices_include_tax' => false ,
2017-03-08 16:51:10 +00:00
'date_created' => null ,
'date_modified' => null ,
2016-08-24 11:34:19 +00:00
'discount_total' => 0 ,
'discount_tax' => 0 ,
'shipping_total' => 0 ,
'shipping_tax' => 0 ,
'cart_tax' => 0 ,
'total' => 0 ,
'total_tax' => 0 ,
2017-11-28 13:04:38 +00:00
// Order props.
2016-08-24 11:34:19 +00:00
'customer_id' => 0 ,
'order_key' => '' ,
'billing' => array (
2017-11-28 13:04:38 +00:00
'first_name' => '' ,
'last_name' => '' ,
'company' => '' ,
'address_1' => '' ,
'address_2' => '' ,
'city' => '' ,
'state' => '' ,
'postcode' => '' ,
'country' => '' ,
'email' => '' ,
'phone' => '' ,
2016-08-24 11:34:19 +00:00
),
'shipping' => array (
2017-11-28 13:04:38 +00:00
'first_name' => '' ,
'last_name' => '' ,
'company' => '' ,
'address_1' => '' ,
'address_2' => '' ,
'city' => '' ,
'state' => '' ,
'postcode' => '' ,
'country' => '' ,
2016-08-24 11:34:19 +00:00
),
'payment_method' => '' ,
'payment_method_title' => '' ,
'transaction_id' => '' ,
'customer_ip_address' => '' ,
'customer_user_agent' => '' ,
'created_via' => '' ,
'customer_note' => '' ,
2017-03-10 14:25:40 +00:00
'date_completed' => null ,
'date_paid' => null ,
2016-08-24 11:34:19 +00:00
'cart_hash' => '' ,
);
2016-06-21 19:06:39 +00:00
/**
* When a payment is complete this function is called .
2015-07-15 15:02:26 +00:00
*
2016-06-21 19:06:39 +00:00
* 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 'completed' since the admin needs to take no action .
* Stock levels are reduced at this point .
* Sales are also recorded for products .
* Finally , record the date of payment .
*
* @ param string $transaction_id Optional transaction id to store in post meta .
2016-06-22 13:07:42 +00:00
* @ return bool success
2016-06-21 19:06:39 +00:00
*/
public function payment_complete ( $transaction_id = '' ) {
2018-01-03 15:47:55 +00:00
if ( ! $this -> get_id () ) { // Order must exist.
return false ;
}
2017-12-14 16:00:38 +00:00
2018-01-03 15:47:55 +00:00
try {
2016-08-24 15:02:19 +00:00
do_action ( 'woocommerce_pre_payment_complete' , $this -> get_id () );
2016-06-21 19:06:39 +00:00
2017-11-29 12:32:35 +00:00
if ( WC () -> session ) {
2016-08-24 15:02:19 +00:00
WC () -> session -> set ( 'order_awaiting_payment' , false );
}
2016-06-21 19:06:39 +00:00
2016-08-24 15:02:19 +00:00
if ( $this -> has_status ( apply_filters ( 'woocommerce_valid_order_statuses_for_payment_complete' , array ( 'on-hold' , 'pending' , 'failed' , 'cancelled' ), $this ) ) ) {
if ( ! empty ( $transaction_id ) ) {
$this -> set_transaction_id ( $transaction_id );
}
2017-03-07 14:37:45 +00:00
if ( ! $this -> get_date_paid ( 'edit' ) ) {
2017-03-10 16:29:52 +00:00
$this -> set_date_paid ( current_time ( 'timestamp' , true ) );
2017-03-07 14:37:45 +00:00
}
2017-03-07 14:39:07 +00:00
$this -> set_status ( apply_filters ( 'woocommerce_payment_complete_order_status' , $this -> needs_processing () ? 'processing' : 'completed' , $this -> get_id (), $this ) );
2016-08-24 15:02:19 +00:00
$this -> save ();
2016-06-21 19:06:39 +00:00
2016-08-24 15:02:19 +00:00
do_action ( 'woocommerce_payment_complete' , $this -> get_id () );
} else {
do_action ( 'woocommerce_payment_complete_order_status_' . $this -> get_status (), $this -> get_id () );
}
} catch ( Exception $e ) {
2018-06-14 13:39:18 +00:00
/**
* If there was an error completing the payment , log to a file and add an order note so the admin can take action .
*/
2017-10-17 12:23:54 +00:00
$logger = wc_get_logger ();
2018-03-21 03:44:44 +00:00
$logger -> error (
2019-04-17 11:47:35 +00:00
sprintf (
'Error completing payment for order #%d' ,
$this -> get_id ()
),
array (
2018-03-21 03:44:44 +00:00
'order' => $this ,
'error' => $e ,
)
);
2017-12-14 16:00:38 +00:00
$this -> add_order_note ( __ ( 'Payment complete event failed.' , 'woocommerce' ) . ' ' . $e -> getMessage () );
2016-08-24 15:02:19 +00:00
return false ;
2016-06-21 19:06:39 +00:00
}
2016-06-22 13:07:42 +00:00
return true ;
2016-06-21 19:06:39 +00:00
}
/**
* Gets order total - formatted for display .
2016-11-09 12:54:08 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $tax_display Type of tax display .
* @ param bool $display_refunded If should include refunded value .
2016-11-09 12:54:08 +00:00
*
2015-07-15 15:02:26 +00:00
* @ return string
*/
2015-08-13 13:45:49 +00:00
public function get_formatted_order_total ( $tax_display = '' , $display_refunded = true ) {
2016-06-21 19:06:39 +00:00
$formatted_total = wc_price ( $this -> get_total (), array ( 'currency' => $this -> get_currency () ) );
2017-11-28 13:04:38 +00:00
$order_total = $this -> get_total ();
$total_refunded = $this -> get_total_refunded ();
$tax_string = '' ;
2015-07-15 15:02:26 +00:00
2016-11-09 12:54:08 +00:00
// Tax for inclusive prices.
2017-11-28 13:04:38 +00:00
if ( wc_tax_enabled () && 'incl' === $tax_display ) {
2015-07-15 15:02:26 +00:00
$tax_string_array = array ();
2017-12-14 14:04:20 +00:00
$tax_totals = $this -> get_tax_totals ();
2015-07-15 15:02:26 +00:00
2017-11-28 13:04:38 +00:00
if ( 'itemized' === get_option ( 'woocommerce_tax_total_display' ) ) {
2017-12-14 14:04:20 +00:00
foreach ( $tax_totals as $code => $tax ) {
2016-06-21 19:06:39 +00:00
$tax_amount = ( $total_refunded && $display_refunded ) ? wc_price ( WC_Tax :: round ( $tax -> amount - $this -> get_total_tax_refunded_by_rate_id ( $tax -> rate_id ) ), array ( 'currency' => $this -> get_currency () ) ) : $tax -> formatted_amount ;
2015-07-15 15:02:26 +00:00
$tax_string_array [] = sprintf ( '%s %s' , $tax_amount , $tax -> label );
}
2017-12-14 14:04:20 +00:00
} elseif ( ! empty ( $tax_totals ) ) {
2015-08-13 13:45:49 +00:00
$tax_amount = ( $total_refunded && $display_refunded ) ? $this -> get_total_tax () - $this -> get_total_tax_refunded () : $this -> get_total_tax ();
2016-06-21 19:06:39 +00:00
$tax_string_array [] = sprintf ( '%s %s' , wc_price ( $tax_amount , array ( 'currency' => $this -> get_currency () ) ), WC () -> countries -> tax_or_vat () );
2015-07-15 15:02:26 +00:00
}
2017-12-14 14:04:20 +00:00
2015-07-15 15:02:26 +00:00
if ( ! empty ( $tax_string_array ) ) {
2017-11-28 13:04:38 +00:00
/* translators: %s: taxes */
2016-12-22 19:05:46 +00:00
$tax_string = ' <small class="includes_tax">' . sprintf ( __ ( '(includes %s)' , 'woocommerce' ), implode ( ', ' , $tax_string_array ) ) . '</small>' ;
2015-07-15 15:02:26 +00:00
}
}
2015-08-13 13:45:49 +00:00
if ( $total_refunded && $display_refunded ) {
2019-04-17 11:47:35 +00:00
$formatted_total = '<del>' . wp_strip_all_tags ( $formatted_total ) . '</del> <ins>' . wc_price ( $order_total - $total_refunded , array ( 'currency' => $this -> get_currency () ) ) . $tax_string . '</ins>' ;
2015-07-15 15:02:26 +00:00
} else {
$formatted_total .= $tax_string ;
}
2016-11-09 10:48:26 +00:00
/**
2016-11-09 12:54:08 +00:00
* Filter WooCommerce formatted order total .
2016-11-09 10:48:26 +00:00
*
2016-11-09 12:54:08 +00:00
* @ param string $formatted_total Total to display .
* @ param WC_Order $order Order data .
* @ param string $tax_display Type of tax display .
* @ param bool $display_refunded If should include refunded value .
2016-11-09 10:48:26 +00:00
*/
return apply_filters ( 'woocommerce_get_formatted_order_total' , $formatted_total , $this , $tax_display , $display_refunded );
2015-07-15 15:02:26 +00:00
}
2016-06-21 19:06:39 +00:00
/*
|--------------------------------------------------------------------------
| CRUD methods
|--------------------------------------------------------------------------
|
| Methods which create , read , update and delete orders from the database .
| Written in abstract fashion so that the way orders are stored can be
| changed more easily in the future .
|
| A save method is included for convenience ( chooses update or create based
| on if the order exists yet ) .
|
*/
/**
2016-11-17 14:37:29 +00:00
* Save data to the database .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-17 14:37:29 +00:00
* @ return int order ID
2016-06-21 19:06:39 +00:00
*/
2016-11-17 14:37:29 +00:00
public function save () {
2019-04-17 11:47:35 +00:00
$this -> maybe_set_user_billing_email ();
parent :: save ();
$this -> status_transition ();
2018-01-03 15:47:55 +00:00
2016-11-17 14:37:29 +00:00
return $this -> get_id ();
2016-06-21 19:06:39 +00:00
}
2019-04-17 12:07:52 +00:00
/**
* Log an error about this order is exception is encountered .
*
* @ param Exception $e Exception object .
* @ param string $message Message regarding exception thrown .
* @ since 3.7 . 0
*/
protected function handle_exception ( $e , $message = 'Error' ) {
wc_get_logger () -> error (
$message ,
array (
'order' => $this ,
'error' => $e ,
)
);
$this -> add_order_note ( $message . ' ' . $e -> getMessage () );
}
2016-06-21 19:06:39 +00:00
/**
* Set order status .
2017-05-12 08:48:46 +00:00
*
2017-11-28 13:04:38 +00:00
* @ since 3.0 . 0
* @ param string $new_status Status to change the order to . No internal wc - prefix is required .
* @ param string $note Optional note to add .
* @ param bool $manual_update Is this a manual order status change ? .
2017-05-12 08:48:46 +00:00
* @ return array
2016-06-21 19:06:39 +00:00
*/
public function set_status ( $new_status , $note = '' , $manual_update = false ) {
$result = parent :: set_status ( $new_status );
2017-03-10 20:20:07 +00:00
if ( true === $this -> object_read && ! empty ( $result [ 'from' ] ) && $result [ 'from' ] !== $result [ 'to' ] ) {
2016-09-09 12:34:49 +00:00
$this -> status_transition = array (
'from' => ! empty ( $this -> status_transition [ 'from' ] ) ? $this -> status_transition [ 'from' ] : $result [ 'from' ],
2016-06-21 19:06:39 +00:00
'to' => $result [ 'to' ],
'note' => $note ,
'manual' => ( bool ) $manual_update ,
);
2018-09-12 17:07:02 +00:00
if ( $manual_update ) {
do_action ( 'woocommerce_order_edit_status' , $this -> get_id (), $result [ 'to' ] );
}
2017-02-28 19:08:12 +00:00
$this -> maybe_set_date_paid ();
$this -> maybe_set_date_completed ();
}
2016-06-21 19:06:39 +00:00
2017-02-28 19:08:12 +00:00
return $result ;
}
2016-12-08 11:19:32 +00:00
2017-02-28 19:08:12 +00:00
/**
* Maybe set date paid .
*
* Sets the date paid variable when transitioning to the payment complete
2017-03-07 14:45:42 +00:00
* order status . This is either processing or completed . This is not filtered
* to avoid infinite loops e . g . if loading an order via the filter .
*
2017-02-28 19:08:12 +00:00
* Date paid is set once in this manner - only when it is not already set .
* This ensures the data exists even if a gateway does not use the
* `payment_complete` method .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-02-28 19:08:12 +00:00
*/
2017-03-09 14:40:19 +00:00
public function maybe_set_date_paid () {
2019-02-14 14:45:26 +00:00
// This logic only runs if the date_paid prop has not been set yet.
if ( ! $this -> get_date_paid ( 'edit' ) ) {
$payment_completed_status = apply_filters ( 'woocommerce_payment_complete_order_status' , $this -> needs_processing () ? 'processing' : 'completed' , $this -> get_id (), $this );
if ( $this -> has_status ( $payment_completed_status ) ) {
// If payment complete status is reached, set paid now.
$this -> set_date_paid ( current_time ( 'timestamp' , true ) );
} elseif ( 'processing' === $payment_completed_status && $this -> has_status ( 'completed' ) ) {
// If payment complete status was processing, but we've passed that and still have no date, set it now.
$this -> set_date_paid ( current_time ( 'timestamp' , true ) );
}
2016-06-21 19:06:39 +00:00
}
2017-02-28 19:08:12 +00:00
}
2016-06-21 19:06:39 +00:00
2017-02-28 19:08:12 +00:00
/**
* Maybe set date completed .
*
* Sets the date completed variable when transitioning to completed status .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-02-28 19:08:12 +00:00
*/
protected function maybe_set_date_completed () {
if ( $this -> has_status ( 'completed' ) ) {
2017-03-10 16:29:52 +00:00
$this -> set_date_completed ( current_time ( 'timestamp' , true ) );
2017-02-28 19:08:12 +00:00
}
2016-06-21 19:06:39 +00:00
}
/**
2018-01-03 15:47:55 +00:00
* Updates status of order immediately .
2017-05-15 11:50:52 +00:00
*
2017-11-28 13:04:38 +00:00
* @ uses WC_Order :: set_status ()
* @ param string $new_status Status to change the order to . No internal wc - prefix is required .
* @ param string $note Optional note to add .
* @ param bool $manual Is this a manual order status change ? .
* @ return bool
2016-06-21 19:06:39 +00:00
*/
public function update_status ( $new_status , $note = '' , $manual = false ) {
2018-01-03 15:47:55 +00:00
if ( ! $this -> get_id () ) { // Order must exist.
return false ;
}
try {
2016-08-24 15:02:19 +00:00
$this -> set_status ( $new_status , $note , $manual );
$this -> save ();
} catch ( Exception $e ) {
2017-10-17 12:23:54 +00:00
$logger = wc_get_logger ();
2018-03-21 03:44:44 +00:00
$logger -> error (
2019-04-17 11:47:35 +00:00
sprintf (
'Error updating status for order #%d' ,
$this -> get_id ()
),
array (
2018-03-21 03:44:44 +00:00
'order' => $this ,
'error' => $e ,
)
);
2017-12-14 16:00:38 +00:00
$this -> add_order_note ( __ ( 'Update status event failed.' , 'woocommerce' ) . ' ' . $e -> getMessage () );
2016-06-21 19:06:39 +00:00
return false ;
}
return true ;
}
/**
* Handle the status transition .
*/
protected function status_transition () {
2017-08-08 10:47:16 +00:00
$status_transition = $this -> status_transition ;
2016-12-15 13:08:25 +00:00
2017-11-28 13:04:38 +00:00
// Reset status transition variable.
2017-08-08 10:47:16 +00:00
$this -> status_transition = false ;
if ( $status_transition ) {
2018-01-03 15:47:55 +00:00
try {
do_action ( 'woocommerce_order_status_' . $status_transition [ 'to' ], $this -> get_id (), $this );
if ( ! empty ( $status_transition [ 'from' ] ) ) {
/* translators: 1: old order status 2: new order status */
$transition_note = sprintf ( __ ( 'Order status changed from %1$s to %2$s.' , 'woocommerce' ), wc_get_order_status_name ( $status_transition [ 'from' ] ), wc_get_order_status_name ( $status_transition [ 'to' ] ) );
do_action ( 'woocommerce_order_status_' . $status_transition [ 'from' ] . '_to_' . $status_transition [ 'to' ], $this -> get_id (), $this );
do_action ( 'woocommerce_order_status_changed' , $this -> get_id (), $status_transition [ 'from' ], $status_transition [ 'to' ], $this );
} else {
/* translators: %s: new order status */
$transition_note = sprintf ( __ ( 'Order status set to %s.' , 'woocommerce' ), wc_get_order_status_name ( $status_transition [ 'to' ] ) );
}
2016-06-21 19:06:39 +00:00
2018-01-03 15:47:55 +00:00
// Note the transition occurred.
$this -> add_order_note ( trim ( $status_transition [ 'note' ] . ' ' . $transition_note ), 0 , $status_transition [ 'manual' ] );
} catch ( Exception $e ) {
$logger = wc_get_logger ();
2018-03-21 03:44:44 +00:00
$logger -> error (
2019-04-17 11:47:35 +00:00
sprintf (
'Status transition of order #%d errored!' ,
$this -> get_id ()
),
array (
2018-03-21 03:44:44 +00:00
'order' => $this ,
'error' => $e ,
)
);
2018-01-03 15:47:55 +00:00
$this -> add_order_note ( __ ( 'Error during status transition.' , 'woocommerce' ) . ' ' . $e -> getMessage () );
2016-06-21 19:06:39 +00:00
}
}
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
|
| Methods for getting data from the order object .
|
*/
2016-08-22 12:04:57 +00:00
/**
* Get all class data in array format .
2017-11-28 13:04:38 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-22 12:04:57 +00:00
* @ return array
*/
public function get_data () {
return array_merge (
2016-12-22 14:52:37 +00:00
array (
'id' => $this -> get_id (),
),
2016-09-09 12:34:49 +00:00
$this -> data ,
2016-08-22 12:04:57 +00:00
array (
'number' => $this -> get_order_number (),
'meta_data' => $this -> get_meta_data (),
'line_items' => $this -> get_items ( 'line_item' ),
'tax_lines' => $this -> get_items ( 'tax' ),
'shipping_lines' => $this -> get_items ( 'shipping' ),
'fee_lines' => $this -> get_items ( 'fee' ),
'coupon_lines' => $this -> get_items ( 'coupon' ),
)
);
}
2017-01-23 21:08:55 +00:00
/**
* Expands the shipping and billing information in the changes array .
*/
public function get_changes () {
$changed_props = parent :: get_changes ();
$subs = array ( 'shipping' , 'billing' );
foreach ( $subs as $sub ) {
if ( ! empty ( $changed_props [ $sub ] ) ) {
foreach ( $changed_props [ $sub ] as $sub_prop => $value ) {
$changed_props [ $sub . '_' . $sub_prop ] = $value ;
}
}
}
2017-03-02 16:46:59 +00:00
if ( isset ( $changed_props [ 'customer_note' ] ) ) {
$changed_props [ 'post_excerpt' ] = $changed_props [ 'customer_note' ];
}
2017-01-23 21:08:55 +00:00
return $changed_props ;
}
2016-08-22 12:04:57 +00:00
/**
* Gets the order number for display ( by default , order ID ) .
*
* @ return string
*/
public function get_order_number () {
2016-12-16 11:49:00 +00:00
return ( string ) apply_filters ( 'woocommerce_order_number' , $this -> get_id (), $this );
2016-08-22 12:04:57 +00:00
}
2016-08-22 10:00:31 +00:00
/**
* Get order key .
2016-11-17 15:02:33 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-08-22 10:00:31 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_order_key ( $context = 'view' ) {
return $this -> get_prop ( 'order_key' , $context );
2016-08-22 10:00:31 +00:00
}
2016-08-22 12:04:57 +00:00
/**
2016-11-17 15:02:33 +00:00
* Get customer_id .
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-08-22 12:04:57 +00:00
* @ return int
*/
2016-11-17 15:02:33 +00:00
public function get_customer_id ( $context = 'view' ) {
return $this -> get_prop ( 'customer_id' , $context );
2016-08-22 12:04:57 +00:00
}
/**
* Alias for get_customer_id () .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-08-22 12:04:57 +00:00
* @ return int
*/
2016-11-17 15:02:33 +00:00
public function get_user_id ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_customer_id ( $context );
2016-08-22 12:04:57 +00:00
}
/**
* Get the user associated with the order . False for guests .
2016-11-17 15:02:33 +00:00
*
2016-08-22 12:04:57 +00:00
* @ return WP_User | false
*/
2016-11-17 15:17:23 +00:00
public function get_user () {
2016-08-22 12:04:57 +00:00
return $this -> get_user_id () ? get_user_by ( 'id' , $this -> get_user_id () ) : false ;
}
2016-11-17 15:17:23 +00:00
/**
* Gets a prop for a getter method .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-17 15:17:23 +00:00
* @ param string $prop Name of prop to get .
* @ param string $address billing or shipping .
* @ param string $context What the value is for . Valid values are view and edit .
* @ return mixed
*/
protected function get_address_prop ( $prop , $address = 'billing' , $context = 'view' ) {
$value = null ;
if ( array_key_exists ( $prop , $this -> data [ $address ] ) ) {
$value = isset ( $this -> changes [ $address ][ $prop ] ) ? $this -> changes [ $address ][ $prop ] : $this -> data [ $address ][ $prop ];
if ( 'view' === $context ) {
$value = apply_filters ( $this -> get_hook_prefix () . $address . '_' . $prop , $value , $this );
}
}
return $value ;
}
2016-06-21 19:06:39 +00:00
/**
2017-11-28 13:04:38 +00:00
* Get billing first name .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_first_name ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'first_name' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing last name .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_last_name ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'last_name' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing company .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_company ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'company' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing address line 1.
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_address_1 ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'address_1' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing address line 2.
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
* @ return string
2016-06-21 19:06:39 +00:00
*/
2016-11-17 15:02:33 +00:00
public function get_billing_address_2 ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'address_2' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing city .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
* @ return string
2016-06-21 19:06:39 +00:00
*/
2016-11-17 15:02:33 +00:00
public function get_billing_city ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'city' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing state .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_state ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'state' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing postcode .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_postcode ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'postcode' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing country .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_country ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'country' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing email .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_email ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'email' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get billing phone .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_billing_phone ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'phone' , 'billing' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping first name .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_first_name ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'first_name' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2016-11-17 15:02:33 +00:00
* Get shipping_last_name .
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_last_name ( $context = 'view' ) {
2017-11-28 13:04:38 +00:00
return $this -> get_address_prop ( 'last_name' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping company .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_company ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'company' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping address line 1.
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_address_1 ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'address_1' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping address line 2.
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_address_2 ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'address_2' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping city .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_city ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'city' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping state .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_state ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'state' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping postcode .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_postcode ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'postcode' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get shipping country .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_shipping_country ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_address_prop ( 'country' , 'shipping' , $context );
2016-06-21 19:06:39 +00:00
}
/**
* Get the payment method .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_payment_method ( $context = 'view' ) {
return $this -> get_prop ( 'payment_method' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get payment method title .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_payment_method_title ( $context = 'view' ) {
return $this -> get_prop ( 'payment_method_title' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get transaction d .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_transaction_id ( $context = 'view' ) {
return $this -> get_prop ( 'transaction_id' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get customer ip address .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_customer_ip_address ( $context = 'view' ) {
return $this -> get_prop ( 'customer_ip_address' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get customer user agent .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_customer_user_agent ( $context = 'view' ) {
return $this -> get_prop ( 'customer_user_agent' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get created via .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_created_via ( $context = 'view' ) {
return $this -> get_prop ( 'created_via' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get customer note .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-06-21 19:06:39 +00:00
* @ return string
*/
2016-11-17 15:02:33 +00:00
public function get_customer_note ( $context = 'view' ) {
return $this -> get_prop ( 'customer_note' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get date completed .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2017-03-10 14:25:40 +00:00
* @ return WC_DateTime | NULL object if the date is set or null if there is no date .
2016-06-21 19:06:39 +00:00
*/
2016-11-17 15:02:33 +00:00
public function get_date_completed ( $context = 'view' ) {
2016-11-17 15:17:23 +00:00
return $this -> get_prop ( 'date_completed' , $context );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Get date paid .
2016-11-17 15:02:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2017-03-10 14:25:40 +00:00
* @ return WC_DateTime | NULL object if the date is set or null if there is no date .
2016-06-21 19:06:39 +00:00
*/
2016-11-17 15:02:33 +00:00
public function get_date_paid ( $context = 'view' ) {
2017-03-09 14:40:19 +00:00
$date_paid = $this -> get_prop ( 'date_paid' , $context );
2017-04-26 07:02:46 +00:00
if ( 'view' === $context && ! $date_paid && version_compare ( $this -> get_version ( 'edit' ), '3.0' , '<' ) && $this -> has_status ( apply_filters ( 'woocommerce_payment_complete_order_status' , $this -> needs_processing () ? 'processing' : 'completed' , $this -> get_id (), $this ) ) ) {
2017-03-09 14:40:19 +00:00
// In view context, return a date if missing.
2017-03-09 14:53:52 +00:00
$date_paid = $this -> get_date_created ( 'edit' );
2017-03-09 14:40:19 +00:00
}
return $date_paid ;
2016-06-21 19:06:39 +00:00
}
2016-11-17 15:02:33 +00:00
/**
* Get cart hash .
*
2017-11-28 13:04:38 +00:00
* @ param string $context What the value is for . Valid values are view and edit .
2016-11-17 15:02:33 +00:00
* @ return string
*/
public function get_cart_hash ( $context = 'view' ) {
return $this -> get_prop ( 'cart_hash' , $context );
}
2016-06-21 19:06:39 +00:00
/**
* Returns the requested address in raw , non - formatted way .
2016-11-18 11:14:09 +00:00
* Note : Merges raw data with get_prop data so changes are returned too .
2016-11-17 15:17:23 +00:00
*
2016-06-21 19:06:39 +00:00
* @ since 2.4 . 0
* @ param string $type Billing or shipping . Anything else besides 'billing' will return shipping address .
* @ return array The stored address after filter .
*/
public function get_address ( $type = 'billing' ) {
2016-11-18 11:14:09 +00:00
return apply_filters ( 'woocommerce_get_order_address' , array_merge ( $this -> data [ $type ], $this -> get_prop ( $type , 'view' ) ), $type , $this );
2016-06-21 19:06:39 +00:00
}
/**
* Get a formatted shipping address for the order .
*
* @ return string
*/
public function get_shipping_address_map_url () {
2017-04-17 10:05:29 +00:00
$address = $this -> get_address ( 'shipping' );
2017-04-20 05:12:18 +00:00
// Remove name and company before generate the Google Maps URL.
unset ( $address [ 'first_name' ], $address [ 'last_name' ], $address [ 'company' ] );
2017-04-17 10:05:29 +00:00
$address = apply_filters ( 'woocommerce_shipping_address_map_url_parts' , $address , $this );
2017-11-28 13:04:38 +00:00
return apply_filters ( 'woocommerce_shipping_address_map_url' , 'https://maps.google.com/maps?&q=' . rawurlencode ( implode ( ', ' , $address ) ) . '&z=16' , $this );
2016-06-21 19:06:39 +00:00
}
/**
* Get a formatted billing full name .
2016-11-17 15:17:23 +00:00
*
2016-06-21 19:06:39 +00:00
* @ return string
*/
public function get_formatted_billing_full_name () {
2016-10-29 17:32:38 +00:00
/* translators: 1: first name 2: last name */
2016-06-21 19:06:39 +00:00
return sprintf ( _x ( '%1$s %2$s' , 'full name' , 'woocommerce' ), $this -> get_billing_first_name (), $this -> get_billing_last_name () );
}
/**
* Get a formatted shipping full name .
2016-11-17 15:17:23 +00:00
*
2016-06-21 19:06:39 +00:00
* @ return string
*/
public function get_formatted_shipping_full_name () {
2016-10-29 17:32:38 +00:00
/* translators: 1: first name 2: last name */
2016-06-21 19:06:39 +00:00
return sprintf ( _x ( '%1$s %2$s' , 'full name' , 'woocommerce' ), $this -> get_shipping_first_name (), $this -> get_shipping_last_name () );
}
/**
* Get a formatted billing address for the order .
2016-11-17 15:17:23 +00:00
*
2017-10-29 17:17:27 +00:00
* @ param string $empty_content Content to show if no address is present . @ since 3.3 . 0.
2016-06-21 19:06:39 +00:00
* @ return string
*/
2017-10-29 17:17:27 +00:00
public function get_formatted_billing_address ( $empty_content = '' ) {
2019-09-24 16:15:35 +00:00
$raw_address = apply_filters ( 'woocommerce_order_formatted_billing_address' , $this -> get_address ( 'billing' ), $this );
$address = WC () -> countries -> get_formatted_address ( $raw_address );
2017-10-16 15:12:07 +00:00
2019-09-24 16:15:35 +00:00
/**
* Filter orders formatterd billing address .
*
2019-09-30 12:42:06 +00:00
* @ since 3.8 . 0
2019-09-24 16:15:35 +00:00
* @ param string $address Formatted billing address string .
* @ param array $raw_address Raw billing address .
*/
return apply_filters ( 'woocommerce_order_get_formatted_billing_address' , $address ? $address : $empty_content , $raw_address );
2016-06-21 19:06:39 +00:00
}
/**
* Get a formatted shipping address for the order .
2016-11-17 15:17:23 +00:00
*
2017-10-29 17:17:27 +00:00
* @ param string $empty_content Content to show if no address is present . @ since 3.3 . 0.
2016-06-21 19:06:39 +00:00
* @ return string
*/
2017-10-29 17:17:27 +00:00
public function get_formatted_shipping_address ( $empty_content = '' ) {
2019-09-24 16:15:35 +00:00
$address = '' ;
$raw_address = $this -> get_address ( 'shipping' );
2017-10-16 15:12:07 +00:00
2017-04-17 14:15:08 +00:00
if ( $this -> has_shipping_address () ) {
2019-09-24 16:15:35 +00:00
$raw_address = apply_filters ( 'woocommerce_order_formatted_shipping_address' , $raw_address , $this );
$address = WC () -> countries -> get_formatted_address ( $raw_address );
2016-06-21 19:06:39 +00:00
}
2017-10-16 15:12:07 +00:00
2019-09-24 16:15:35 +00:00
/**
* Filter orders formatterd shipping address .
*
2019-09-30 12:42:06 +00:00
* @ since 3.8 . 0
2019-09-24 16:15:35 +00:00
* @ param string $address Formatted shipping address string .
* @ param array $raw_address Raw shipping address .
*/
return apply_filters ( 'woocommerce_order_get_formatted_shipping_address' , $address ? $address : $empty_content , $raw_address );
2017-10-16 15:12:07 +00:00
}
/**
* Returns true if the order has a billing address .
*
* @ since 3.0 . 4
* @ return boolean
*/
public function has_billing_address () {
2017-10-27 20:37:00 +00:00
return $this -> get_billing_address_1 () || $this -> get_billing_address_2 ();
2016-06-21 19:06:39 +00:00
}
2017-04-17 14:15:08 +00:00
/**
* Returns true if the order has a shipping address .
*
* @ since 3.0 . 4
* @ return boolean
*/
public function has_shipping_address () {
return $this -> get_shipping_address_1 () || $this -> get_shipping_address_2 ();
}
2016-06-21 19:06:39 +00:00
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
|
| Functions for setting order data . These should not update anything in the
| database itself and should only change what is stored in the class
2017-03-15 16:36:53 +00:00
| object . However , for backwards compatibility pre 3.0 . 0 some of these
2016-06-21 19:06:39 +00:00
| setters may handle both .
|
*/
2016-11-17 16:02:09 +00:00
/**
* Sets a prop for a setter method .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-17 16:02:09 +00:00
* @ param string $prop Name of prop to set .
* @ param string $address Name of address to set . billing or shipping .
* @ param mixed $value Value of the prop .
*/
protected function set_address_prop ( $prop , $address = 'billing' , $value ) {
if ( array_key_exists ( $prop , $this -> data [ $address ] ) ) {
if ( true === $this -> object_read ) {
if ( $value !== $this -> data [ $address ][ $prop ] || ( isset ( $this -> changes [ $address ] ) && array_key_exists ( $prop , $this -> changes [ $address ] ) ) ) {
$this -> changes [ $address ][ $prop ] = $value ;
}
} else {
$this -> data [ $address ][ $prop ] = $value ;
}
}
}
2016-08-22 10:00:31 +00:00
/**
2017-11-28 13:04:38 +00:00
* Set order key .
2016-11-17 15:17:23 +00:00
*
2017-03-31 11:29:19 +00:00
* @ param string $value Max length 22 chars .
2017-11-28 13:04:38 +00:00
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-08-22 10:00:31 +00:00
*/
public function set_order_key ( $value ) {
2017-03-31 11:29:19 +00:00
$this -> set_prop ( 'order_key' , substr ( $value , 0 , 22 ) );
2016-08-22 10:00:31 +00:00
}
2016-08-22 12:04:57 +00:00
/**
2017-11-28 13:04:38 +00:00
* Set customer id .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param int $value Customer ID .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-08-22 12:04:57 +00:00
*/
public function set_customer_id ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'customer_id' , absint ( $value ) );
}
2016-06-21 19:06:39 +00:00
/**
2017-11-28 13:04:38 +00:00
* Set billing first name .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing first name .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_first_name ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'first_name' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing last name .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing last name .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_last_name ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'last_name' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing company .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing company .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_company ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'company' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing address line 1.
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing address line 1.
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_address_1 ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'address_1' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing address line 2.
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing address line 2.
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_address_2 ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'address_2' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing city .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing city .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_city ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'city' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing state .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing state .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_state ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'state' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing postcode .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing postcode .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_postcode ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'postcode' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing country .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing country .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_country ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'country' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
2016-08-24 11:34:19 +00:00
/**
* Maybe set empty billing email to that of the user who owns the order .
*/
protected function maybe_set_user_billing_email () {
2017-11-28 13:04:38 +00:00
$user = $this -> get_user ();
if ( ! $this -> get_billing_email () && $user ) {
2016-08-24 11:34:19 +00:00
try {
$this -> set_billing_email ( $user -> user_email );
2017-11-28 13:04:38 +00:00
} catch ( WC_Data_Exception $e ) {
2016-08-24 11:34:19 +00:00
unset ( $e );
}
}
}
2016-06-21 19:06:39 +00:00
/**
2017-11-28 13:04:38 +00:00
* Set billing email .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing email .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_email ( $value ) {
2016-08-23 17:48:48 +00:00
if ( $value && ! is_email ( $value ) ) {
2016-11-17 15:17:23 +00:00
$this -> error ( 'order_invalid_billing_email' , __ ( 'Invalid billing email address' , 'woocommerce' ) );
2016-08-23 14:25:50 +00:00
}
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'email' , 'billing' , sanitize_email ( $value ) );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set billing phone .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Billing phone .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_billing_phone ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'phone' , 'billing' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping first name .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping first name .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_first_name ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'first_name' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping last name .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping last name .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_last_name ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'last_name' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping company .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping company .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_company ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'company' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping address line 1.
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping address line 1.
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_address_1 ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'address_1' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping address line 2.
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping address line 2.
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_address_2 ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'address_2' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping city .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping city .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_city ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'city' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping state .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping state .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_state ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'state' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping postcode .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping postcode .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_postcode ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'postcode' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set shipping country .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Shipping country .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_shipping_country ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_address_prop ( 'country' , 'shipping' , $value );
2016-06-21 19:06:39 +00:00
}
/**
* Set the payment method .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $payment_method Supports WC_Payment_Gateway for bw compatibility with < 3.0 .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_payment_method ( $payment_method = '' ) {
if ( is_object ( $payment_method ) ) {
$this -> set_payment_method ( $payment_method -> id );
$this -> set_payment_method_title ( $payment_method -> get_title () );
} elseif ( '' === $payment_method ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'payment_method' , '' );
$this -> set_prop ( 'payment_method_title' , '' );
2016-06-21 19:06:39 +00:00
} else {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'payment_method' , $payment_method );
2016-06-21 19:06:39 +00:00
}
}
/**
2017-11-28 13:04:38 +00:00
* Set payment method title .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Payment method title .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_payment_method_title ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'payment_method_title' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set transaction id .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Transaction id .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_transaction_id ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'transaction_id' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set customer ip address .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Customer ip address .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_customer_ip_address ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'customer_ip_address' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set customer user agent .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Customer user agent .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_customer_user_agent ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'customer_user_agent' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set created via .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Created via .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_created_via ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'created_via' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set customer note .
2016-11-17 15:17:23 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $value Customer note .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_customer_note ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'customer_note' , $value );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set date completed .
2016-11-17 15:17:23 +00:00
*
2017-03-10 14:25:40 +00:00
* @ param string | integer | null $date UTC timestamp , or ISO 8601 DateTime . If the DateTime string has no timezone or offset , WordPress site timezone will be assumed . Null if their is no date .
2017-11-28 13:04:38 +00:00
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
2017-03-10 14:25:40 +00:00
public function set_date_completed ( $date = null ) {
$this -> set_date_prop ( 'date_completed' , $date );
2016-06-21 19:06:39 +00:00
}
/**
2017-11-28 13:04:38 +00:00
* Set date paid .
2016-11-17 15:17:23 +00:00
*
2017-03-10 14:25:40 +00:00
* @ param string | integer | null $date UTC timestamp , or ISO 8601 DateTime . If the DateTime string has no timezone or offset , WordPress site timezone will be assumed . Null if their is no date .
2017-11-28 13:04:38 +00:00
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
2017-03-10 14:25:40 +00:00
public function set_date_paid ( $date = null ) {
$this -> set_date_prop ( 'date_paid' , $date );
2016-06-21 19:06:39 +00:00
}
/**
2016-11-17 15:17:23 +00:00
* Set cart hash .
*
2017-11-28 13:04:38 +00:00
* @ param string $value Cart hash .
* @ throws WC_Data_Exception Throws exception when invalid data is found .
2016-06-21 19:06:39 +00:00
*/
public function set_cart_hash ( $value ) {
2016-11-17 15:17:23 +00:00
$this -> set_prop ( 'cart_hash' , $value );
2016-06-21 19:06:39 +00:00
}
/*
|--------------------------------------------------------------------------
| Conditionals
|--------------------------------------------------------------------------
|
| Checks if a condition is true or false .
|
*/
2016-08-22 10:00:31 +00:00
/**
* Check if an order key is valid .
*
2017-11-28 13:04:38 +00:00
* @ param string $key Order key .
2016-08-22 10:00:31 +00:00
* @ return bool
*/
public function key_is_valid ( $key ) {
2019-01-21 16:02:28 +00:00
return hash_equals ( $this -> get_order_key (), $key );
2016-08-22 10:00:31 +00:00
}
2016-06-21 19:06:39 +00:00
/**
* See if order matches cart_hash .
2017-05-15 11:50:52 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $cart_hash Cart hash .
2016-06-21 19:06:39 +00:00
* @ return bool
*/
2016-06-22 14:38:28 +00:00
public function has_cart_hash ( $cart_hash = '' ) {
2017-11-28 13:04:38 +00:00
return hash_equals ( $this -> get_cart_hash (), $cart_hash ); // @codingStandardsIgnoreLine
2016-06-21 19:06:39 +00:00
}
/**
* Checks if an order can be edited , specifically for use on the Edit Order screen .
2017-11-28 13:04:38 +00:00
*
2016-06-21 19:06:39 +00:00
* @ return bool
*/
public function is_editable () {
2017-11-28 13:04:38 +00:00
return apply_filters ( 'wc_order_is_editable' , in_array ( $this -> get_status (), array ( 'pending' , 'on-hold' , 'auto-draft' ), true ), $this );
2016-06-21 19:06:39 +00:00
}
/**
* Returns if an order has been paid for based on the order status .
2017-11-28 13:04:38 +00:00
*
2016-06-21 19:06:39 +00:00
* @ since 2.5 . 0
* @ return bool
*/
public function is_paid () {
2016-11-03 11:27:03 +00:00
return apply_filters ( 'woocommerce_order_is_paid' , $this -> has_status ( wc_get_is_paid_statuses () ), $this );
2016-06-21 19:06:39 +00:00
}
/**
* Checks if product download is permitted .
*
* @ return bool
*/
public function is_download_permitted () {
return apply_filters ( 'woocommerce_order_is_download_permitted' , $this -> has_status ( 'completed' ) || ( 'yes' === get_option ( 'woocommerce_downloads_grant_access_after_payment' ) && $this -> has_status ( 'processing' ) ), $this );
}
/**
* Checks if an order needs display the shipping address , based on shipping method .
2017-11-28 13:04:38 +00:00
*
2016-06-21 19:06:39 +00:00
* @ return bool
*/
public function needs_shipping_address () {
if ( 'no' === get_option ( 'woocommerce_calc_shipping' ) ) {
return false ;
}
2017-11-28 13:04:38 +00:00
$hide = apply_filters ( 'woocommerce_order_hide_shipping_address' , array ( 'local_pickup' ), $this );
2016-06-21 19:06:39 +00:00
$needs_address = false ;
foreach ( $this -> get_shipping_methods () as $shipping_method ) {
2017-11-28 13:04:38 +00:00
// Remove any instance IDs after ":".
2016-08-03 11:32:27 +00:00
$shipping_method_id = current ( explode ( ':' , $shipping_method [ 'method_id' ] ) );
2017-11-28 13:04:38 +00:00
if ( ! in_array ( $shipping_method_id , $hide , true ) ) {
2016-06-21 19:06:39 +00:00
$needs_address = true ;
break ;
}
}
return apply_filters ( 'woocommerce_order_needs_shipping_address' , $needs_address , $hide , $this );
}
/**
* Returns true if the order contains a downloadable product .
2017-11-28 13:04:38 +00:00
*
2016-06-21 19:06:39 +00:00
* @ return bool
*/
public function has_downloadable_item () {
foreach ( $this -> get_items () as $item ) {
2017-11-28 13:04:38 +00:00
if ( $item -> is_type ( 'line_item' ) ) {
$product = $item -> get_product ();
2018-01-10 12:32:58 +00:00
if ( $product && $product -> has_file () ) {
2017-11-28 13:04:38 +00:00
return true ;
}
2016-06-21 19:06:39 +00:00
}
}
return false ;
}
2017-07-11 12:45:35 +00:00
/**
* Get downloads from all line items for this order .
*
* @ since 3.2 . 0
* @ return array
*/
public function get_downloadable_items () {
$downloads = array ();
foreach ( $this -> get_items () as $item ) {
2017-11-28 13:04:38 +00:00
if ( ! is_object ( $item ) ) {
continue ;
}
if ( $item -> is_type ( 'line_item' ) ) {
$item_downloads = $item -> get_item_downloads ();
$product = $item -> get_product ();
if ( $product && $item_downloads ) {
2017-07-11 12:45:35 +00:00
foreach ( $item_downloads as $file ) {
$downloads [] = array (
'download_url' => $file [ 'download_url' ],
'download_id' => $file [ 'id' ],
'product_id' => $product -> get_id (),
'product_name' => $product -> get_name (),
2017-11-28 12:18:43 +00:00
'product_url' => $product -> is_visible () ? $product -> get_permalink () : '' , // Since 3.3.0.
2017-07-11 12:45:35 +00:00
'download_name' => $file [ 'name' ],
'order_id' => $this -> get_id (),
'order_key' => $this -> get_order_key (),
'downloads_remaining' => $file [ 'downloads_remaining' ],
'access_expires' => $file [ 'access_expires' ],
2018-01-10 01:41:35 +00:00
'file' => array (
'name' => $file [ 'name' ],
'file' => $file [ 'file' ],
),
2017-07-11 12:45:35 +00:00
);
}
}
}
}
2017-11-28 13:04:38 +00:00
2017-12-07 15:33:26 +00:00
return apply_filters ( 'woocommerce_order_get_downloadable_items' , $downloads , $this );
2017-11-28 13:04:38 +00:00
}
2017-07-11 12:45:35 +00:00
2016-06-21 19:06:39 +00:00
/**
* Checks if an order needs payment , based on status and order total .
*
* @ return bool
*/
public function needs_payment () {
$valid_order_statuses = apply_filters ( 'woocommerce_valid_order_statuses_for_payment' , array ( 'pending' , 'failed' ), $this );
return apply_filters ( 'woocommerce_order_needs_payment' , ( $this -> has_status ( $valid_order_statuses ) && $this -> get_total () > 0 ), $this , $valid_order_statuses );
}
2017-02-28 19:08:12 +00:00
/**
* See if the order needs processing before it can be completed .
*
* Orders which only contain virtual , downloadable items do not need admin
* intervention .
*
2018-01-19 15:14:00 +00:00
* Uses a transient so these calls are not repeated multiple times , and because
* once the order is processed this code / transient does not need to persist .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-02-28 19:08:12 +00:00
* @ return bool
*/
2017-03-10 12:21:42 +00:00
public function needs_processing () {
2018-01-19 15:14:00 +00:00
$transient_name = 'wc_order_' . $this -> get_id () . '_needs_processing' ;
$needs_processing = get_transient ( $transient_name );
if ( false === $needs_processing ) {
$needs_processing = 0 ;
2017-11-28 13:04:38 +00:00
2018-01-19 15:14:00 +00:00
if ( count ( $this -> get_items () ) > 0 ) {
foreach ( $this -> get_items () as $item ) {
if ( $item -> is_type ( 'line_item' ) ) {
$product = $item -> get_product ();
2017-02-28 19:08:12 +00:00
2018-01-19 15:14:00 +00:00
if ( ! $product ) {
continue ;
}
$virtual_downloadable_item = $product -> is_downloadable () && $product -> is_virtual ();
if ( apply_filters ( 'woocommerce_order_item_needs_processing' , ! $virtual_downloadable_item , $product , $this -> get_id () ) ) {
$needs_processing = 1 ;
break ;
}
2017-02-28 19:08:12 +00:00
}
}
}
2018-01-19 15:14:00 +00:00
set_transient ( $transient_name , $needs_processing , DAY_IN_SECONDS );
2017-02-28 19:08:12 +00:00
}
2018-01-19 15:14:00 +00:00
return 1 === absint ( $needs_processing );
2017-02-28 19:08:12 +00:00
}
2016-06-21 19:06:39 +00:00
/*
|--------------------------------------------------------------------------
| URLs and Endpoints
|--------------------------------------------------------------------------
*/
/**
* 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 .
*
2017-11-28 13:04:38 +00:00
* @ param bool $on_checkout If on checkout .
2016-06-21 19:06:39 +00:00
* @ return string
*/
public function get_checkout_payment_url ( $on_checkout = false ) {
2019-08-30 16:06:50 +00:00
$pay_url = wc_get_endpoint_url ( 'order-pay' , $this -> get_id (), wc_get_checkout_url () );
2016-06-21 19:06:39 +00:00
if ( $on_checkout ) {
$pay_url = add_query_arg ( 'key' , $this -> get_order_key (), $pay_url );
} else {
2018-03-21 03:44:44 +00:00
$pay_url = add_query_arg (
array (
'pay_for_order' => 'true' ,
'key' => $this -> get_order_key (),
2019-04-17 11:47:35 +00:00
),
$pay_url
2018-03-21 03:44:44 +00:00
);
2016-06-21 19:06:39 +00:00
}
return apply_filters ( 'woocommerce_get_checkout_payment_url' , $pay_url , $this );
}
/**
* Generates a URL for the thanks page ( order received ) .
*
* @ return string
*/
public function get_checkout_order_received_url () {
2019-08-30 16:06:50 +00:00
$order_received_url = wc_get_endpoint_url ( 'order-received' , $this -> get_id (), wc_get_checkout_url () );
2016-06-21 19:06:39 +00:00
$order_received_url = add_query_arg ( 'key' , $this -> get_order_key (), $order_received_url );
return apply_filters ( 'woocommerce_get_checkout_order_received_url' , $order_received_url , $this );
}
/**
* Generates a URL so that a customer can cancel their ( unpaid - pending ) order .
*
2017-11-28 13:04:38 +00:00
* @ param string $redirect Redirect URL .
2016-06-21 19:06:39 +00:00
* @ return string
*/
public function get_cancel_order_url ( $redirect = '' ) {
2018-03-21 03:44:44 +00:00
return apply_filters (
2019-04-17 11:47:35 +00:00
'woocommerce_get_cancel_order_url' ,
wp_nonce_url (
2018-03-21 03:44:44 +00:00
add_query_arg (
array (
'cancel_order' => 'true' ,
'order' => $this -> get_order_key (),
'order_id' => $this -> get_id (),
'redirect' => $redirect ,
2019-04-17 11:47:35 +00:00
),
$this -> get_cancel_endpoint ()
),
'woocommerce-cancel_order'
2018-03-21 03:44:44 +00:00
)
);
2016-06-21 19:06:39 +00:00
}
/**
* Generates a raw ( unescaped ) cancel - order URL for use by payment gateways .
*
2017-11-28 13:04:38 +00:00
* @ param string $redirect Redirect URL .
2016-06-21 19:06:39 +00:00
* @ return string The unescaped cancel - order URL .
*/
public function get_cancel_order_url_raw ( $redirect = '' ) {
2018-03-21 03:44:44 +00:00
return apply_filters (
2019-04-17 11:47:35 +00:00
'woocommerce_get_cancel_order_url_raw' ,
add_query_arg (
2018-03-21 03:44:44 +00:00
array (
'cancel_order' => 'true' ,
'order' => $this -> get_order_key (),
'order_id' => $this -> get_id (),
'redirect' => $redirect ,
'_wpnonce' => wp_create_nonce ( 'woocommerce-cancel_order' ),
2019-04-17 11:47:35 +00:00
),
$this -> get_cancel_endpoint ()
2018-03-21 03:44:44 +00:00
)
);
2016-06-21 19:06:39 +00:00
}
/**
* Helper method to return the cancel endpoint .
*
* @ return string the cancel endpoint ; either the cart page or the home page .
*/
public function get_cancel_endpoint () {
2019-08-29 07:46:25 +00:00
$cancel_endpoint = wc_get_cart_url ();
2016-06-21 19:06:39 +00:00
if ( ! $cancel_endpoint ) {
$cancel_endpoint = home_url ();
}
if ( false === strpos ( $cancel_endpoint , '?' ) ) {
$cancel_endpoint = trailingslashit ( $cancel_endpoint );
}
return $cancel_endpoint ;
}
/**
* Generates a URL to view an order from the my account page .
*
* @ return string
*/
public function get_view_order_url () {
return apply_filters ( 'woocommerce_get_view_order_url' , wc_get_endpoint_url ( 'view-order' , $this -> get_id (), wc_get_page_permalink ( 'myaccount' ) ), $this );
}
2018-01-11 12:04:39 +00:00
/**
* Get ' s the URL to edit the order in the backend .
*
* @ since 3.3 . 0
* @ return string
*/
public function get_edit_order_url () {
return apply_filters ( 'woocommerce_get_edit_order_url' , get_admin_url ( null , 'post.php?post=' . $this -> get_id () . '&action=edit' ), $this );
}
2016-06-21 19:06:39 +00:00
/*
|--------------------------------------------------------------------------
| Order notes .
|--------------------------------------------------------------------------
*/
/**
2016-06-22 15:31:05 +00:00
* Adds a note ( comment ) to the order . Order must exist .
2016-06-21 19:06:39 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param string $note Note to add .
* @ param int $is_customer_note Is this a note for the customer ? .
* @ param bool $added_by_user Was the note added by a user ? .
* @ return int Comment ID .
2016-06-21 19:06:39 +00:00
*/
public function add_order_note ( $note , $is_customer_note = 0 , $added_by_user = false ) {
2016-06-22 15:31:05 +00:00
if ( ! $this -> get_id () ) {
return 0 ;
}
2016-06-21 19:06:39 +00:00
if ( is_user_logged_in () && current_user_can ( 'edit_shop_order' , $this -> get_id () ) && $added_by_user ) {
$user = get_user_by ( 'id' , get_current_user_id () );
$comment_author = $user -> display_name ;
$comment_author_email = $user -> user_email ;
} else {
2017-11-28 13:04:38 +00:00
$comment_author = __ ( 'WooCommerce' , 'woocommerce' );
$comment_author_email = strtolower ( __ ( 'WooCommerce' , 'woocommerce' ) ) . '@' ;
$comment_author_email .= isset ( $_SERVER [ 'HTTP_HOST' ] ) ? str_replace ( 'www.' , '' , sanitize_text_field ( wp_unslash ( $_SERVER [ 'HTTP_HOST' ] ) ) ) : 'noreply.com' ; // WPCS: input var ok.
$comment_author_email = sanitize_email ( $comment_author_email );
2016-06-21 19:06:39 +00:00
}
2018-03-21 03:44:44 +00:00
$commentdata = apply_filters (
'woocommerce_new_order_note_data' ,
2017-11-28 13:04:38 +00:00
array (
'comment_post_ID' => $this -> get_id (),
'comment_author' => $comment_author ,
'comment_author_email' => $comment_author_email ,
'comment_author_url' => '' ,
'comment_content' => $note ,
'comment_agent' => 'WooCommerce' ,
'comment_type' => 'order_note' ,
'comment_parent' => 0 ,
'comment_approved' => 1 ,
),
array (
'order_id' => $this -> get_id (),
'is_customer_note' => $is_customer_note ,
)
);
2016-06-21 19:06:39 +00:00
$comment_id = wp_insert_comment ( $commentdata );
if ( $is_customer_note ) {
add_comment_meta ( $comment_id , 'is_customer_note' , 1 );
2018-03-21 03:44:44 +00:00
do_action (
2019-04-17 11:47:35 +00:00
'woocommerce_new_customer_note' ,
array (
2018-03-21 03:44:44 +00:00
'order_id' => $this -> get_id (),
'customer_note' => $commentdata [ 'comment_content' ],
)
);
2016-06-21 19:06:39 +00:00
}
return $comment_id ;
}
/**
* List order notes ( public ) for the customer .
*
* @ return array
*/
public function get_customer_order_notes () {
$notes = array ();
$args = array (
'post_id' => $this -> get_id (),
'approve' => 'approve' ,
2016-08-27 01:46:45 +00:00
'type' => '' ,
2016-06-21 19:06:39 +00:00
);
remove_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_order_comments' ) );
$comments = get_comments ( $args );
foreach ( $comments as $comment ) {
if ( ! get_comment_meta ( $comment -> comment_ID , 'is_customer_note' , true ) ) {
continue ;
}
$comment -> comment_content = make_clickable ( $comment -> comment_content );
2017-11-28 13:04:38 +00:00
$notes [] = $comment ;
2016-06-21 19:06:39 +00:00
}
add_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_order_comments' ) );
return $notes ;
}
/*
|--------------------------------------------------------------------------
| Refunds
|--------------------------------------------------------------------------
*/
2014-07-08 18:23:22 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get order refunds .
2017-02-09 12:04:44 +00:00
*
2014-07-08 18:23:22 +00:00
* @ since 2.2
2015-11-30 15:25:48 +00:00
* @ return array of WC_Order_Refund objects
2014-07-08 18:23:22 +00:00
*/
public function get_refunds () {
2017-02-09 12:04:44 +00:00
$cache_key = WC_Cache_Helper :: get_cache_prefix ( 'orders' ) . 'refunds' . $this -> get_id ();
$cached_data = wp_cache_get ( $cache_key , $this -> cache_group );
if ( false !== $cached_data ) {
return $cached_data ;
2017-02-08 16:19:01 +00:00
}
2017-02-09 12:04:44 +00:00
2018-03-21 03:44:44 +00:00
$this -> refunds = wc_get_orders (
array (
'type' => 'shop_order_refund' ,
'parent' => $this -> get_id (),
'limit' => - 1 ,
)
);
2017-02-09 12:04:44 +00:00
wp_cache_set ( $cache_key , $this -> refunds , $this -> cache_group );
2014-07-25 15:48:19 +00:00
return $this -> refunds ;
2014-07-08 18:23:22 +00:00
}
2014-07-08 19:59:38 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get amount already refunded .
2014-07-08 19:59:38 +00:00
*
* @ since 2.2
2015-11-05 16:05:03 +00:00
* @ return string
2014-07-08 19:59:38 +00:00
*/
public function get_total_refunded () {
2017-02-09 12:04:44 +00:00
$cache_key = WC_Cache_Helper :: get_cache_prefix ( 'orders' ) . 'total_refunded' . $this -> get_id ();
$cached_data = wp_cache_get ( $cache_key , $this -> cache_group );
if ( false !== $cached_data ) {
return $cached_data ;
}
$total_refunded = $this -> data_store -> get_total_refunded ( $this );
wp_cache_set ( $cache_key , $total_refunded , $this -> cache_group );
return $total_refunded ;
2014-07-08 19:59:38 +00:00
}
2014-07-25 15:48:19 +00:00
2015-01-29 20:50:08 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get the total tax refunded .
2015-01-29 20:50:08 +00:00
*
* @ since 2.3
* @ return float
*/
public function get_total_tax_refunded () {
2017-02-09 12:04:44 +00:00
$cache_key = WC_Cache_Helper :: get_cache_prefix ( 'orders' ) . 'total_tax_refunded' . $this -> get_id ();
$cached_data = wp_cache_get ( $cache_key , $this -> cache_group );
if ( false !== $cached_data ) {
return $cached_data ;
}
$total_refunded = $this -> data_store -> get_total_tax_refunded ( $this );
wp_cache_set ( $cache_key , $total_refunded , $this -> cache_group );
return $total_refunded ;
2015-01-29 20:50:08 +00:00
}
2015-06-01 10:06:11 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get the total shipping refunded .
2015-06-01 10:06:11 +00:00
*
* @ since 2.4
* @ return float
*/
public function get_total_shipping_refunded () {
2017-02-09 12:04:44 +00:00
$cache_key = WC_Cache_Helper :: get_cache_prefix ( 'orders' ) . 'total_shipping_refunded' . $this -> get_id ();
$cached_data = wp_cache_get ( $cache_key , $this -> cache_group );
if ( false !== $cached_data ) {
return $cached_data ;
}
$total_refunded = $this -> data_store -> get_total_shipping_refunded ( $this );
wp_cache_set ( $cache_key , $total_refunded , $this -> cache_group );
return $total_refunded ;
2015-06-01 10:06:11 +00:00
}
2015-07-29 12:13:41 +00:00
/**
* Gets the count of order items of a certain type that have been refunded .
2017-11-28 13:04:38 +00:00
*
2015-07-29 12:13:41 +00:00
* @ since 2.4 . 0
2017-11-28 13:04:38 +00:00
* @ param string $item_type Item type .
2015-07-29 12:13:41 +00:00
* @ return string
*/
public function get_item_count_refunded ( $item_type = '' ) {
if ( empty ( $item_type ) ) {
$item_type = array ( 'line_item' );
}
if ( ! is_array ( $item_type ) ) {
$item_type = array ( $item_type );
}
$count = 0 ;
foreach ( $this -> get_refunds () as $refund ) {
foreach ( $refund -> get_items ( $item_type ) as $refunded_item ) {
2017-02-23 13:51:29 +00:00
$count += abs ( $refunded_item -> get_quantity () );
2015-07-29 12:13:41 +00:00
}
}
return apply_filters ( 'woocommerce_get_item_count_refunded' , $count , $item_type , $this );
}
/**
* Get the total number of items refunded .
*
* @ since 2.4 . 0
2017-11-28 13:04:38 +00:00
*
* @ param string $item_type Type of the item we ' re checking , if not a line_item .
* @ return int
2015-07-29 12:13:41 +00:00
*/
public function get_total_qty_refunded ( $item_type = 'line_item' ) {
$qty = 0 ;
foreach ( $this -> get_refunds () as $refund ) {
foreach ( $refund -> get_items ( $item_type ) as $refunded_item ) {
2016-08-16 11:36:38 +00:00
$qty += $refunded_item -> get_quantity ();
2015-07-29 12:13:41 +00:00
}
}
return $qty ;
}
2014-07-25 15:48:19 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get the refunded amount for a line item .
2014-09-20 18:54:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param int $item_id ID of the item we ' re checking .
* @ param string $item_type Type of the item we ' re checking , if not a line_item .
* @ return int
2014-07-25 15:48:19 +00:00
*/
public function get_qty_refunded_for_item ( $item_id , $item_type = 'line_item' ) {
$qty = 0 ;
foreach ( $this -> get_refunds () as $refund ) {
foreach ( $refund -> get_items ( $item_type ) as $refunded_item ) {
2016-06-21 19:06:39 +00:00
if ( absint ( $refunded_item -> get_meta ( '_refunded_item_id' ) ) === $item_id ) {
2016-08-16 11:36:38 +00:00
$qty += $refunded_item -> get_quantity ();
2014-07-25 15:48:19 +00:00
}
}
}
return $qty ;
}
/**
2015-11-03 13:31:20 +00:00
* Get the refunded amount for a line item .
2014-09-20 18:54:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param int $item_id ID of the item we ' re checking .
* @ param string $item_type Type of the item we ' re checking , if not a line_item .
* @ return int
2014-07-25 15:48:19 +00:00
*/
public function get_total_refunded_for_item ( $item_id , $item_type = 'line_item' ) {
$total = 0 ;
foreach ( $this -> get_refunds () as $refund ) {
foreach ( $refund -> get_items ( $item_type ) as $refunded_item ) {
2016-06-21 19:06:39 +00:00
if ( absint ( $refunded_item -> get_meta ( '_refunded_item_id' ) ) === $item_id ) {
$total += $refunded_item -> get_total ();
2014-07-25 15:48:19 +00:00
}
}
}
return $total * - 1 ;
}
/**
2017-03-21 11:20:39 +00:00
* Get the refunded tax amount for a line item .
2014-09-20 18:54:33 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param int $item_id ID of the item we ' re checking .
* @ param int $tax_id ID of the tax we ' re checking .
* @ param string $item_type Type of the item we ' re checking , if not a line_item .
2015-11-05 16:05:03 +00:00
* @ return double
2014-07-25 15:48:19 +00:00
*/
public function get_tax_refunded_for_item ( $item_id , $tax_id , $item_type = 'line_item' ) {
$total = 0 ;
foreach ( $this -> get_refunds () as $refund ) {
foreach ( $refund -> get_items ( $item_type ) as $refunded_item ) {
2017-03-21 11:20:39 +00:00
$refunded_item_id = ( int ) $refunded_item -> get_meta ( '_refunded_item_id' );
if ( $refunded_item_id === $item_id ) {
2017-11-28 13:04:38 +00:00
$taxes = $refunded_item -> get_taxes ();
2017-09-14 09:02:29 +00:00
$total += isset ( $taxes [ 'total' ][ $tax_id ] ) ? ( float ) $taxes [ 'total' ][ $tax_id ] : 0 ;
2017-03-21 11:20:39 +00:00
break ;
2014-07-25 15:48:19 +00:00
}
}
}
2015-05-01 13:57:56 +00:00
return wc_round_tax_total ( $total ) * - 1 ;
2014-09-20 18:54:33 +00:00
}
2015-01-29 20:50:08 +00:00
/**
* Get total tax refunded by rate ID .
*
2017-11-28 13:04:38 +00:00
* @ param int $rate_id Rate ID .
2015-01-29 20:50:08 +00:00
* @ return float
*/
public function get_total_tax_refunded_by_rate_id ( $rate_id ) {
$total = 0 ;
foreach ( $this -> get_refunds () as $refund ) {
foreach ( $refund -> get_items ( 'tax' ) as $refunded_item ) {
2016-06-21 19:06:39 +00:00
if ( absint ( $refunded_item -> get_rate_id () ) === $rate_id ) {
2016-08-22 12:04:57 +00:00
$total += abs ( $refunded_item -> get_tax_total () ) + abs ( $refunded_item -> get_shipping_tax_total () );
2015-01-29 20:50:08 +00:00
}
}
}
return $total ;
}
2015-10-06 13:43:33 +00:00
/**
* How much money is left to refund ?
2017-11-28 13:04:38 +00:00
*
2015-10-06 13:43:33 +00:00
* @ return string
*/
public function get_remaining_refund_amount () {
2016-01-13 11:07:05 +00:00
return wc_format_decimal ( $this -> get_total () - $this -> get_total_refunded (), wc_get_price_decimals () );
2015-10-06 13:43:33 +00:00
}
/**
* How many items are left to refund ?
2017-11-28 13:04:38 +00:00
*
2015-10-06 13:43:33 +00:00
* @ return int
*/
public function get_remaining_refund_items () {
return absint ( $this -> get_item_count () - $this -> get_item_count_refunded () );
}
2017-02-27 17:06:09 +00:00
/**
2017-03-06 13:44:52 +00:00
* Add total row for the payment method .
2017-02-27 17:06:09 +00:00
*
2017-11-28 13:04:38 +00:00
* @ param array $total_rows Total rows .
* @ param string $tax_display Tax to display .
2017-02-27 17:06:09 +00:00
*/
2017-03-06 13:44:52 +00:00
protected function add_order_item_totals_payment_method_row ( & $total_rows , $tax_display ) {
2019-03-11 16:59:38 +00:00
if ( $this -> get_total () > 0 && $this -> get_payment_method_title () && 'other' !== $this -> get_payment_method_title () ) {
2017-02-27 17:06:09 +00:00
$total_rows [ 'payment_method' ] = array (
'label' => __ ( 'Payment method:' , 'woocommerce' ),
'value' => $this -> get_payment_method_title (),
);
}
2017-03-06 13:44:52 +00:00
}
2017-02-27 17:06:09 +00:00
2017-03-06 13:44:52 +00:00
/**
* Add total row for refunds .
*
2017-11-28 13:04:38 +00:00
* @ param array $total_rows Total rows .
* @ param string $tax_display Tax to display .
2017-03-06 13:44:52 +00:00
*/
protected function add_order_item_totals_refund_rows ( & $total_rows , $tax_display ) {
2017-11-28 13:04:38 +00:00
$refunds = $this -> get_refunds ();
if ( $refunds ) {
2017-02-27 17:06:09 +00:00
foreach ( $refunds as $id => $refund ) {
$total_rows [ 'refund_' . $id ] = array (
'label' => $refund -> get_reason () ? $refund -> get_reason () : __ ( 'Refund' , 'woocommerce' ) . ':' ,
2017-11-28 13:04:38 +00:00
'value' => wc_price ( '-' . $refund -> get_amount (), array ( 'currency' => $this -> get_currency () ) ),
2017-02-27 17:06:09 +00:00
);
}
}
2017-03-06 13:44:52 +00:00
}
2017-02-27 17:06:09 +00:00
2017-03-06 13:44:52 +00:00
/**
* Get totals for display on pages and in emails .
*
2017-11-28 13:04:38 +00:00
* @ param string $tax_display Tax to display .
2017-03-06 13:44:52 +00:00
* @ return array
*/
public function get_order_item_totals ( $tax_display = '' ) {
$tax_display = $tax_display ? $tax_display : get_option ( 'woocommerce_tax_display_cart' );
$total_rows = array ();
$this -> add_order_item_totals_subtotal_row ( $total_rows , $tax_display );
2017-08-24 11:49:49 +00:00
$this -> add_order_item_totals_discount_row ( $total_rows , $tax_display );
2017-03-06 13:44:52 +00:00
$this -> add_order_item_totals_shipping_row ( $total_rows , $tax_display );
$this -> add_order_item_totals_fee_rows ( $total_rows , $tax_display );
$this -> add_order_item_totals_tax_rows ( $total_rows , $tax_display );
$this -> add_order_item_totals_payment_method_row ( $total_rows , $tax_display );
$this -> add_order_item_totals_refund_rows ( $total_rows , $tax_display );
$this -> add_order_item_totals_total_row ( $total_rows , $tax_display );
return apply_filters ( 'woocommerce_get_order_item_totals' , $total_rows , $this , $tax_display );
2017-02-27 17:06:09 +00:00
}
2014-07-08 18:23:22 +00:00
}