|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
* The WooCommerce order class handles order data.
|
|
|
|
|
*
|
|
|
|
|
* @class WC_Order
|
|
|
|
|
* @version 1.6.4
|
|
|
|
|
* @version 2.1.0
|
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
|
* @category Class
|
|
|
|
|
* @author WooThemes
|
|
|
|
@ -29,8 +29,9 @@ class WC_Order {
|
|
|
|
|
$this->display_totals_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
|
|
|
|
|
$this->display_cart_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
|
|
|
|
|
|
|
|
|
|
if ( $id > 0 )
|
|
|
|
|
if ( $id > 0 ) {
|
|
|
|
|
$this->get_order( $id );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -42,8 +43,9 @@ class WC_Order {
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function get_order( $id = 0 ) {
|
|
|
|
|
if ( ! $id )
|
|
|
|
|
if ( ! $id ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ( $result = get_post( $id ) ) {
|
|
|
|
|
$this->populate( $result );
|
|
|
|
|
return true;
|
|
|
|
@ -81,8 +83,9 @@ class WC_Order {
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function __isset( $key ) {
|
|
|
|
|
if ( ! $this->id )
|
|
|
|
|
if ( ! $this->id ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metadata_exists( 'post', $this->id, '_' . $key );
|
|
|
|
|
}
|
|
|
|
@ -96,12 +99,13 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function __get( $key ) {
|
|
|
|
|
// Get values or default if not set
|
|
|
|
|
if ( 'completed_date' == $key )
|
|
|
|
|
if ( 'completed_date' == $key ) {
|
|
|
|
|
$value = ( $value = get_post_meta( $this->id, '_completed_date', true ) ) ? $value : $this->modified_date;
|
|
|
|
|
elseif ( 'user_id' == $key )
|
|
|
|
|
} elseif ( 'user_id' == $key ) {
|
|
|
|
|
$value = ( $value = get_post_meta( $this->id, '_customer_user', true ) ) ? absint( $value ) : '';
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
$value = get_post_meta( $this->id, '_' . $key, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
@ -114,7 +118,10 @@ class WC_Order {
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function key_is_valid( $key ) {
|
|
|
|
|
if ( $key == $this->order_key ) return true;
|
|
|
|
|
if ( $key == $this->order_key ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -175,8 +182,12 @@ class WC_Order {
|
|
|
|
|
'country' => $this->billing_country
|
|
|
|
|
);
|
|
|
|
|
$joined_address = array();
|
|
|
|
|
foreach ($address as $part) if (!empty($part)) $joined_address[] = $part;
|
|
|
|
|
$this->billing_address = implode(', ', $joined_address);
|
|
|
|
|
foreach ( $address as $part ) {
|
|
|
|
|
if ( ! empty( $part ) ) {
|
|
|
|
|
$joined_address[] = $part;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->billing_address = implode( ', ', $joined_address );
|
|
|
|
|
}
|
|
|
|
|
return $this->billing_address;
|
|
|
|
|
}
|
|
|
|
@ -230,8 +241,12 @@ class WC_Order {
|
|
|
|
|
'country' => $this->shipping_country
|
|
|
|
|
);
|
|
|
|
|
$joined_address = array();
|
|
|
|
|
foreach ($address as $part) if (!empty($part)) $joined_address[] = $part;
|
|
|
|
|
$this->shipping_address = implode(', ', $joined_address);
|
|
|
|
|
foreach ( $address as $part ) {
|
|
|
|
|
if ( ! empty( $part ) ) {
|
|
|
|
|
$joined_address[] = $part;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->shipping_address = implode( ', ', $joined_address );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->shipping_address;
|
|
|
|
@ -247,11 +262,13 @@ class WC_Order {
|
|
|
|
|
public function get_items( $type = '' ) {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
|
|
if ( empty( $type ) )
|
|
|
|
|
if ( empty( $type ) ) {
|
|
|
|
|
$type = array( 'line_item' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! is_array( $type ) )
|
|
|
|
|
if ( ! is_array( $type ) ) {
|
|
|
|
|
$type = array( $type );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$type = array_map( 'esc_attr', $type );
|
|
|
|
|
|
|
|
|
@ -273,7 +290,7 @@ class WC_Order {
|
|
|
|
|
$items[ $item->order_item_id ]['item_meta'] = $this->get_item_meta( $item->order_item_id );
|
|
|
|
|
|
|
|
|
|
// Put meta into item array
|
|
|
|
|
foreach( $items[ $item->order_item_id ]['item_meta'] as $name => $value ) {
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
@ -291,21 +308,24 @@ class WC_Order {
|
|
|
|
|
public function get_item_count( $type = '' ) {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
|
|
if ( empty( $type ) )
|
|
|
|
|
if ( empty( $type ) ) {
|
|
|
|
|
$type = array( 'line_item' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! is_array( $type ) )
|
|
|
|
|
if ( ! is_array( $type ) ) {
|
|
|
|
|
$type = array( $type );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$items = $this->get_items( $type );
|
|
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
|
|
foreach ( $items as $item ) {
|
|
|
|
|
if ( ! empty( $item['qty'] ) )
|
|
|
|
|
if ( ! empty( $item['qty'] ) ) {
|
|
|
|
|
$count += $item['qty'];
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
$count ++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_item_count', $count, $type, $this );
|
|
|
|
@ -348,13 +368,15 @@ class WC_Order {
|
|
|
|
|
public function has_shipping_method( $method_id ) {
|
|
|
|
|
$shipping_methods = $this->get_shipping_methods();
|
|
|
|
|
$has_method = false;
|
|
|
|
|
|
|
|
|
|
if ( !$shipping_methods )
|
|
|
|
|
|
|
|
|
|
if ( ! $shipping_methods ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $shipping_methods as $shipping_method ) {
|
|
|
|
|
if ( $shipping_method['method_id'] == $method_id )
|
|
|
|
|
if ( $shipping_method['method_id'] == $method_id ) {
|
|
|
|
|
$has_method = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $has_method;
|
|
|
|
@ -397,7 +419,7 @@ class WC_Order {
|
|
|
|
|
public function has_meta( $order_item_id ) {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
|
|
return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, order_item_id
|
|
|
|
|
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_id", absint( $order_item_id ) ), ARRAY_A );
|
|
|
|
|
}
|
|
|
|
@ -475,7 +497,7 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function get_total_tax() {
|
|
|
|
|
return apply_filters( 'woocommerce_order_amount_total_tax', wc_round_tax_total( $this->get_cart_tax() + $this->get_shipping_tax() ), $this );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets shipping total.
|
|
|
|
@ -507,10 +529,11 @@ class WC_Order {
|
|
|
|
|
* @return float
|
|
|
|
|
*/
|
|
|
|
|
public function get_item_subtotal( $item, $inc_tax = false, $round = true ) {
|
|
|
|
|
if ( $inc_tax )
|
|
|
|
|
if ( $inc_tax ) {
|
|
|
|
|
$price = ( $item['line_subtotal'] + $item['line_subtotal_tax'] ) / $item['qty'];
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
$price = ( $item['line_subtotal'] / $item['qty'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$price = $round ? round( $price, 2 ) : $price;
|
|
|
|
|
|
|
|
|
@ -527,10 +550,11 @@ class WC_Order {
|
|
|
|
|
* @return float
|
|
|
|
|
*/
|
|
|
|
|
public function get_line_subtotal( $item, $inc_tax = false, $round = true ) {
|
|
|
|
|
if ( $inc_tax )
|
|
|
|
|
if ( $inc_tax ) {
|
|
|
|
|
$price = $item['line_subtotal'] + $item['line_subtotal_tax'];
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
$price = $item['line_subtotal'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$price = $round ? round( $price, 2 ) : $price;
|
|
|
|
|
|
|
|
|
@ -547,10 +571,11 @@ class WC_Order {
|
|
|
|
|
* @return float
|
|
|
|
|
*/
|
|
|
|
|
public function get_item_total( $item, $inc_tax = false, $round = true ) {
|
|
|
|
|
if ( $inc_tax )
|
|
|
|
|
if ( $inc_tax ) {
|
|
|
|
|
$price = ( $item['line_total'] + $item['line_tax'] ) / $item['qty'];
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
$price = $item['line_total'] / $item['qty'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$price = $round ? round( $price, 2 ) : $price;
|
|
|
|
|
|
|
|
|
@ -630,14 +655,16 @@ class WC_Order {
|
|
|
|
|
$labels = array();
|
|
|
|
|
|
|
|
|
|
// Backwards compat < 2.1 - get shipping title stored in meta
|
|
|
|
|
if ( $this->shipping_method_title )
|
|
|
|
|
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 )
|
|
|
|
|
foreach ( $shipping_methods as $shipping ) {
|
|
|
|
|
$labels[] = $shipping['name'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_order_shipping_method', implode( ', ', $labels ), $this );
|
|
|
|
|
}
|
|
|
|
@ -646,19 +673,22 @@ class WC_Order {
|
|
|
|
|
* Gets line subtotal - formatted for display.
|
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
* @param mixed $item
|
|
|
|
|
* @param array $item
|
|
|
|
|
* @param string $tax_display
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function get_formatted_line_subtotal( $item, $tax_display = '' ) {
|
|
|
|
|
if ( ! $tax_display )
|
|
|
|
|
if ( ! $tax_display ) {
|
|
|
|
|
$tax_display = $this->tax_display_cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subtotal = 0;
|
|
|
|
|
|
|
|
|
|
if ( ! isset( $item['line_subtotal'] ) || ! isset( $item['line_subtotal_tax'] ) )
|
|
|
|
|
if ( ! isset( $item['line_subtotal'] ) || ! isset( $item['line_subtotal_tax'] ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $tax_display == 'excl' ) {
|
|
|
|
|
if ( 'excl' == $tax_display ) {
|
|
|
|
|
$ex_tax_label = $this->prices_include_tax ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
$subtotal = wc_price( $this->get_line_subtotal( $item ), array( 'ex_tax_label' => $ex_tax_label, 'currency' => $this->get_order_currency() ) );
|
|
|
|
@ -678,7 +708,7 @@ class WC_Order {
|
|
|
|
|
public function get_order_currency() {
|
|
|
|
|
|
|
|
|
|
$currency = $this->order_currency;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_order_currency', $currency, $this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -704,33 +734,37 @@ class WC_Order {
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function get_subtotal_to_display( $compound = false, $tax_display = '' ) {
|
|
|
|
|
if ( ! $tax_display )
|
|
|
|
|
if ( ! $tax_display ) {
|
|
|
|
|
$tax_display = $this->tax_display_cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subtotal = 0;
|
|
|
|
|
|
|
|
|
|
if ( ! $compound ) {
|
|
|
|
|
foreach ( $this->get_items() as $item ) {
|
|
|
|
|
|
|
|
|
|
if ( ! isset( $item['line_subtotal'] ) || ! isset( $item['line_subtotal_tax'] ) )
|
|
|
|
|
if ( ! isset( $item['line_subtotal'] ) || ! isset( $item['line_subtotal_tax'] ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subtotal += $item['line_subtotal'];
|
|
|
|
|
|
|
|
|
|
if ( $tax_display == 'incl' ) {
|
|
|
|
|
if ( 'incl' == $tax_display ) {
|
|
|
|
|
$subtotal += $item['line_subtotal_tax'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subtotal = wc_price( $subtotal, array('currency' => $this->get_order_currency()) );
|
|
|
|
|
|
|
|
|
|
if ( $tax_display == 'excl' && $this->prices_include_tax )
|
|
|
|
|
if ( $tax_display == 'excl' && $this->prices_include_tax ) {
|
|
|
|
|
$subtotal .= ' <small>' . WC()->countries->ex_tax_or_vat() . '</small>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
if ( $tax_display == 'incl' )
|
|
|
|
|
if ( 'incl' == $tax_display ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $this->get_items() as $item ) {
|
|
|
|
|
|
|
|
|
@ -744,7 +778,9 @@ class WC_Order {
|
|
|
|
|
// Remove non-compound taxes
|
|
|
|
|
foreach ( $this->get_taxes() as $tax ) {
|
|
|
|
|
|
|
|
|
|
if ( ! empty( $tax['compound'] ) ) continue;
|
|
|
|
|
if ( ! empty( $tax['compound'] ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subtotal = $subtotal + $tax['tax_amount'] + $tax['shipping_tax_amount'];
|
|
|
|
|
|
|
|
|
@ -767,8 +803,9 @@ class WC_Order {
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function get_shipping_to_display( $tax_display = '' ) {
|
|
|
|
|
if ( ! $tax_display )
|
|
|
|
|
if ( ! $tax_display ) {
|
|
|
|
|
$tax_display = $this->tax_display_cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->order_shipping > 0 ) {
|
|
|
|
|
|
|
|
|
@ -779,16 +816,18 @@ class WC_Order {
|
|
|
|
|
// Show shipping excluding tax
|
|
|
|
|
$shipping = wc_price( $this->order_shipping, array('currency' => $this->get_order_currency()) );
|
|
|
|
|
|
|
|
|
|
if ( $this->order_shipping_tax > 0 && $this->prices_include_tax )
|
|
|
|
|
if ( $this->order_shipping_tax > 0 && $this->prices_include_tax ) {
|
|
|
|
|
$tax_text = WC()->countries->ex_tax_or_vat() . ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// Show shipping including tax
|
|
|
|
|
$shipping = wc_price( $this->order_shipping + $this->order_shipping_tax, array('currency' => $this->get_order_currency()) );
|
|
|
|
|
|
|
|
|
|
if ( $this->order_shipping_tax > 0 && ! $this->prices_include_tax )
|
|
|
|
|
if ( $this->order_shipping_tax > 0 && ! $this->prices_include_tax ) {
|
|
|
|
|
$tax_text = WC()->countries->inc_tax_or_vat() . ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -811,7 +850,7 @@ class WC_Order {
|
|
|
|
|
* @return string.
|
|
|
|
|
*/
|
|
|
|
|
public function get_cart_discount_to_display() {
|
|
|
|
|
return apply_filters( 'woocommerce_order_cart_discount_to_display', wc_price( $this->get_cart_discount(), array('currency' => $this->get_order_currency()) ), $this );
|
|
|
|
|
return apply_filters( 'woocommerce_order_cart_discount_to_display', wc_price( $this->get_cart_discount(), array( 'currency' => $this->get_order_currency() ) ), $this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -822,7 +861,7 @@ class WC_Order {
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function get_order_discount_to_display() {
|
|
|
|
|
return apply_filters( 'woocommerce_order_discount_to_display', wc_price( $this->get_order_discount(), array('currency' => $this->get_order_currency()) ), $this );
|
|
|
|
|
return apply_filters( 'woocommerce_order_discount_to_display', wc_price( $this->get_order_discount(), array( 'currency' => $this->get_order_currency() ) ), $this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -835,7 +874,8 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function get_product_from_item( $item ) {
|
|
|
|
|
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
|
|
|
|
|
return apply_filters( 'woocommerce_get_product_from_item', $_product, $item, $this );
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_product_from_item', $_product, $item, $this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -846,35 +886,40 @@ class WC_Order {
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function get_order_item_totals( $tax_display = '' ) {
|
|
|
|
|
if ( ! $tax_display )
|
|
|
|
|
if ( ! $tax_display ) {
|
|
|
|
|
$tax_display = $this->tax_display_cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$total_rows = array();
|
|
|
|
|
|
|
|
|
|
if ( $subtotal = $this->get_subtotal_to_display() )
|
|
|
|
|
if ( $subtotal = $this->get_subtotal_to_display() ) {
|
|
|
|
|
$total_rows['cart_subtotal'] = array(
|
|
|
|
|
'label' => __( 'Cart Subtotal:', 'woocommerce' ),
|
|
|
|
|
'value' => $subtotal
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->get_cart_discount() > 0 )
|
|
|
|
|
if ( $this->get_cart_discount() > 0 ) {
|
|
|
|
|
$total_rows['cart_discount'] = array(
|
|
|
|
|
'label' => __( 'Cart Discount:', 'woocommerce' ),
|
|
|
|
|
'value' => '-' . $this->get_cart_discount_to_display()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->get_shipping_method() )
|
|
|
|
|
if ( $this->get_shipping_method() ) {
|
|
|
|
|
$total_rows['shipping'] = array(
|
|
|
|
|
'label' => __( 'Shipping:', 'woocommerce' ),
|
|
|
|
|
'value' => $this->get_shipping_to_display()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $fees = $this->get_fees() )
|
|
|
|
|
foreach( $fees as $id => $fee ) {
|
|
|
|
|
if ( $fee['line_total'] + $fee['line_tax'] == 0 )
|
|
|
|
|
if ( $fee['line_total'] + $fee['line_tax'] == 0 ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $tax_display == 'excl' ) {
|
|
|
|
|
if ( 'excl' == $tax_display ) {
|
|
|
|
|
|
|
|
|
|
$total_rows[ 'fee_' . $id ] = array(
|
|
|
|
|
'label' => $fee['name'],
|
|
|
|
@ -892,7 +937,7 @@ class WC_Order {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tax for tax exclusive prices
|
|
|
|
|
if ( $tax_display == 'excl' ) {
|
|
|
|
|
if ( 'excl' == $tax_display ) {
|
|
|
|
|
if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
|
|
|
|
|
foreach ( $this->get_tax_totals() as $code => $tax ) {
|
|
|
|
|
$total_rows[ sanitize_title( $code ) ] = array(
|
|
|
|
@ -908,11 +953,12 @@ class WC_Order {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->get_order_discount() > 0 )
|
|
|
|
|
if ( $this->get_order_discount() > 0 ) {
|
|
|
|
|
$total_rows['order_discount'] = array(
|
|
|
|
|
'label' => __( 'Order Discount:', 'woocommerce' ),
|
|
|
|
|
'value' => '-' . $this->get_order_discount_to_display()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$total_rows['order_total'] = array(
|
|
|
|
|
'label' => __( 'Order Total:', 'woocommerce' ),
|
|
|
|
@ -920,11 +966,11 @@ class WC_Order {
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Tax for inclusive prices
|
|
|
|
|
if ( get_option( 'woocommerce_calc_taxes' ) == 'yes' && $tax_display == 'incl' ) {
|
|
|
|
|
if ( 'yes' == get_option( 'woocommerce_calc_taxes' ) && 'incl' == $tax_display ) {
|
|
|
|
|
|
|
|
|
|
$tax_string_array = array();
|
|
|
|
|
|
|
|
|
|
if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
|
|
|
|
|
if ( 'itemized' == get_option( 'woocommerce_tax_total_display' ) ) {
|
|
|
|
|
foreach ( $this->get_tax_totals() as $code => $tax ) {
|
|
|
|
|
$tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
|
|
|
|
|
}
|
|
|
|
@ -932,8 +978,9 @@ class WC_Order {
|
|
|
|
|
$tax_string_array[] = sprintf( '%s %s', wc_price( $this->get_total_tax(), array('currency' => $this->get_order_currency()) ), WC()->countries->tax_or_vat() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! empty( $tax_string_array ) )
|
|
|
|
|
if ( ! empty( $tax_string_array ) ) {
|
|
|
|
|
$total_rows['order_total']['value'] .= ' ' . sprintf( __( '(Includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_order_item_totals', $total_rows, $this );
|
|
|
|
@ -992,15 +1039,15 @@ class WC_Order {
|
|
|
|
|
public function has_downloadable_item() {
|
|
|
|
|
$has_downloadable_item = false;
|
|
|
|
|
|
|
|
|
|
foreach($this->get_items() as $item) :
|
|
|
|
|
foreach ( $this->get_items() as $item ) {
|
|
|
|
|
|
|
|
|
|
$_product = $this->get_product_from_item( $item );
|
|
|
|
|
|
|
|
|
|
if ( $_product && $_product->exists() && $_product->is_downloadable() ) :
|
|
|
|
|
if ( $_product && $_product->exists() && $_product->is_downloadable() ) {
|
|
|
|
|
$has_downloadable_item = true;
|
|
|
|
|
endif;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $has_downloadable_item;
|
|
|
|
|
}
|
|
|
|
@ -1017,8 +1064,9 @@ class WC_Order {
|
|
|
|
|
|
|
|
|
|
$pay_url = wc_get_endpoint_url( 'order-pay', $this->id, get_permalink( wc_get_page_id( 'checkout' ) ) );
|
|
|
|
|
|
|
|
|
|
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' || is_ssl() )
|
|
|
|
|
if ( 'yes' == get_option( 'woocommerce_force_ssl_checkout' ) || is_ssl() ) {
|
|
|
|
|
$pay_url = str_replace( 'http:', 'https:', $pay_url );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $on_checkout ) {
|
|
|
|
|
$pay_url = add_query_arg( 'key', $this->order_key, $pay_url );
|
|
|
|
@ -1040,8 +1088,9 @@ class WC_Order {
|
|
|
|
|
|
|
|
|
|
$order_received_url = wc_get_endpoint_url( 'order-received', $this->id, get_permalink( wc_get_page_id( 'checkout' ) ) );
|
|
|
|
|
|
|
|
|
|
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' || is_ssl() )
|
|
|
|
|
if ( 'yes' == get_option( 'woocommerce_force_ssl_checkout' ) || 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 );
|
|
|
|
|
|
|
|
|
@ -1087,10 +1136,10 @@ class WC_Order {
|
|
|
|
|
|
|
|
|
|
_deprecated_function( 'get_downloadable_file_urls', '2.1', 'get_item_downloads' );
|
|
|
|
|
|
|
|
|
|
$download_file = $variation_id > 0 ? $variation_id : $product_id;
|
|
|
|
|
$download_file = $variation_id > 0 ? $variation_id : $product_id;
|
|
|
|
|
$_product = get_product( $download_file );
|
|
|
|
|
|
|
|
|
|
$user_email = $this->billing_email;
|
|
|
|
|
$user_email = $this->billing_email;
|
|
|
|
|
|
|
|
|
|
$results = $wpdb->get_results( $wpdb->prepare("
|
|
|
|
|
SELECT download_id
|
|
|
|
@ -1192,8 +1241,9 @@ class WC_Order {
|
|
|
|
|
|
|
|
|
|
add_comment_meta( $comment_id, 'is_customer_note', $is_customer_note );
|
|
|
|
|
|
|
|
|
|
if ( $is_customer_note )
|
|
|
|
|
if ( $is_customer_note ) {
|
|
|
|
|
do_action( 'woocommerce_new_customer_note', array( 'order_id' => $this->id, 'customer_note' => $note ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $comment_id;
|
|
|
|
|
}
|
|
|
|
@ -1209,8 +1259,9 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function update_status( $new_status_slug, $note = '' ) {
|
|
|
|
|
|
|
|
|
|
if ( $note )
|
|
|
|
|
if ( $note ) {
|
|
|
|
|
$note .= ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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' );
|
|
|
|
@ -1226,14 +1277,16 @@ class WC_Order {
|
|
|
|
|
do_action( 'woocommerce_order_status_' . $this->status . '_to_' . $new_status->slug, $this->id );
|
|
|
|
|
do_action( 'woocommerce_order_status_changed', $this->id, $this->status, $new_status->slug );
|
|
|
|
|
|
|
|
|
|
if ( $old_status )
|
|
|
|
|
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' ) ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Record the completed date of the order
|
|
|
|
|
if ( $new_status->slug == 'completed' )
|
|
|
|
|
if ( 'completed' == $new_status->slug ) {
|
|
|
|
|
update_post_meta( $this->id, '_completed_date', current_time('mysql') );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $new_status->slug == 'processing' || $new_status->slug == 'completed' || $new_status->slug == 'on-hold' ) {
|
|
|
|
|
if ( 'processing' == $new_status->slug || 'completed' == $new_status->slug || 'on-hold' == $new_status->slug ) {
|
|
|
|
|
|
|
|
|
|
// Record the sales
|
|
|
|
|
$this->record_product_sales();
|
|
|
|
@ -1243,11 +1296,12 @@ class WC_Order {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the order is cancelled, restore used coupons
|
|
|
|
|
if ( $new_status->slug == 'cancelled' )
|
|
|
|
|
if ( 'cancelled' == $new_status->slug ) {
|
|
|
|
|
$this->decrease_coupon_usage_counts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update last modified
|
|
|
|
|
wp_update_post( array( 'ID' => $this->id ) );
|
|
|
|
|
wp_update_post( array( 'ID' => $this->id ) );
|
|
|
|
|
|
|
|
|
|
$this->status = $new_status->slug;
|
|
|
|
|
}
|
|
|
|
@ -1268,7 +1322,7 @@ class WC_Order {
|
|
|
|
|
public function cancel_order( $note = '' ) {
|
|
|
|
|
unset( WC()->session->order_awaiting_payment );
|
|
|
|
|
|
|
|
|
|
$this->update_status('cancelled', $note);
|
|
|
|
|
$this->update_status( 'cancelled', $note );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1288,10 +1342,11 @@ class WC_Order {
|
|
|
|
|
|
|
|
|
|
do_action( 'woocommerce_pre_payment_complete', $this->id );
|
|
|
|
|
|
|
|
|
|
if ( ! empty( WC()->session->order_awaiting_payment ) )
|
|
|
|
|
if ( ! empty( WC()->session->order_awaiting_payment ) ) {
|
|
|
|
|
unset( WC()->session->order_awaiting_payment );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->id && ( $this->status == 'on-hold' || $this->status == 'pending' || $this->status == 'failed' ) ) {
|
|
|
|
|
if ( $this->id && ( 'on-hold' == $this->status || 'pending' == $this->status || 'failed' == $this->status ) ) {
|
|
|
|
|
|
|
|
|
|
$order_needs_processing = true;
|
|
|
|
|
|
|
|
|
@ -1329,8 +1384,9 @@ class WC_Order {
|
|
|
|
|
);
|
|
|
|
|
wp_update_post( $this_order );
|
|
|
|
|
|
|
|
|
|
if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', true, $this->id ) )
|
|
|
|
|
if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', true, $this->id ) ) {
|
|
|
|
|
$this->reduce_order_stock(); // Payment is complete so reduce stock levels
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do_action( 'woocommerce_payment_complete', $this->id );
|
|
|
|
|
|
|
|
|
@ -1350,16 +1406,18 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function record_product_sales() {
|
|
|
|
|
|
|
|
|
|
if ( get_post_meta( $this->id, '_recorded_sales', true ) == 'yes' )
|
|
|
|
|
if ( 'yes' == get_post_meta( $this->id, '_recorded_sales', true ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( sizeof( $this->get_items() ) > 0 ) {
|
|
|
|
|
foreach ( $this->get_items() as $item ) {
|
|
|
|
|
if ( $item['product_id'] > 0 ) {
|
|
|
|
|
$sales = (int) get_post_meta( $item['product_id'], 'total_sales', true );
|
|
|
|
|
$sales += (int) $item['qty'];
|
|
|
|
|
if ( $sales )
|
|
|
|
|
if ( $sales ) {
|
|
|
|
|
update_post_meta( $item['product_id'], 'total_sales', $sales );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -1395,19 +1453,22 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function increase_coupon_usage_counts() {
|
|
|
|
|
|
|
|
|
|
if ( get_post_meta( $this->id, '_recorded_coupon_usage_counts', true ) == 'yes' )
|
|
|
|
|
if ( 'yes' == get_post_meta( $this->id, '_recorded_coupon_usage_counts', true ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( sizeof( $this->get_used_coupons() ) > 0 ) {
|
|
|
|
|
foreach ( $this->get_used_coupons() as $code ) {
|
|
|
|
|
if ( ! $code )
|
|
|
|
|
if ( ! $code ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coupon = new WC_Coupon( $code );
|
|
|
|
|
|
|
|
|
|
$used_by = $this->user_id;
|
|
|
|
|
if ( ! $used_by )
|
|
|
|
|
if ( ! $used_by ) {
|
|
|
|
|
$used_by = $this->billing_email;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coupon->inc_usage_count( $used_by );
|
|
|
|
|
}
|
|
|
|
@ -1425,19 +1486,22 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function decrease_coupon_usage_counts() {
|
|
|
|
|
|
|
|
|
|
if ( get_post_meta( $this->id, '_recorded_coupon_usage_counts', true ) != 'yes' )
|
|
|
|
|
if ( 'yes' != get_post_meta( $this->id, '_recorded_coupon_usage_counts', true ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( sizeof( $this->get_used_coupons() ) > 0 ) {
|
|
|
|
|
foreach ( $this->get_used_coupons() as $code ) {
|
|
|
|
|
if ( ! $code )
|
|
|
|
|
if ( ! $code ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coupon = new WC_Coupon( $code );
|
|
|
|
|
|
|
|
|
|
$used_by = $this->user_id;
|
|
|
|
|
if ( ! $used_by )
|
|
|
|
|
if ( ! $used_by ) {
|
|
|
|
|
$used_by = $this->billing_email;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coupon->dcr_usage_count( $used_by );
|
|
|
|
|
}
|
|
|
|
@ -1455,12 +1519,12 @@ class WC_Order {
|
|
|
|
|
*/
|
|
|
|
|
public function reduce_order_stock() {
|
|
|
|
|
|
|
|
|
|
if ( get_option('woocommerce_manage_stock') == 'yes' && sizeof( $this->get_items() ) > 0 ) {
|
|
|
|
|
if ( 'yes' == get_option('woocommerce_manage_stock') && sizeof( $this->get_items() ) > 0 ) {
|
|
|
|
|
|
|
|
|
|
// Reduce stock levels and do any other actions with products in the cart
|
|
|
|
|
foreach ( $this->get_items() as $item ) {
|
|
|
|
|
|
|
|
|
|
if ($item['product_id']>0) {
|
|
|
|
|
if ( $item['product_id'] > 0 ) {
|
|
|
|
|
$_product = $this->get_product_from_item( $item );
|
|
|
|
|
|
|
|
|
|
if ( $_product && $_product->exists() && $_product->managing_stock() ) {
|
|
|
|
@ -1501,17 +1565,18 @@ class WC_Order {
|
|
|
|
|
public function send_stock_notifications( $product, $new_stock, $qty_ordered ) {
|
|
|
|
|
|
|
|
|
|
// Backorders
|
|
|
|
|
if ( $new_stock < 0 )
|
|
|
|
|
if ( $new_stock < 0 ) {
|
|
|
|
|
do_action( 'woocommerce_product_on_backorder', array( 'product' => $product, 'order_id' => $this->id, 'quantity' => $qty_ordered ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stock status notifications
|
|
|
|
|
$notification_sent = false;
|
|
|
|
|
|
|
|
|
|
if ( get_option( 'woocommerce_notify_no_stock' ) == 'yes' && get_option('woocommerce_notify_no_stock_amount') >= $new_stock ) {
|
|
|
|
|
if ( 'yes' == get_option( 'woocommerce_notify_no_stock' ) && 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 ) {
|
|
|
|
|
if ( ! $notification_sent && 'yes' == get_option( 'woocommerce_notify_low_stock' ) && get_option('woocommerce_notify_low_stock_amount') >= $new_stock ) {
|
|
|
|
|
do_action( 'woocommerce_low_stock', $product );
|
|
|
|
|
$notification_sent = true;
|
|
|
|
|
}
|
|
|
|
@ -1535,36 +1600,38 @@ class WC_Order {
|
|
|
|
|
'type' => ''
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments') );
|
|
|
|
|
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
|
|
|
|
|
|
|
|
|
|
$comments = get_comments( $args );
|
|
|
|
|
|
|
|
|
|
foreach ($comments as $comment) :
|
|
|
|
|
$is_customer_note = get_comment_meta($comment->comment_ID, 'is_customer_note', true);
|
|
|
|
|
$comment->comment_content = make_clickable($comment->comment_content);
|
|
|
|
|
if ($is_customer_note)
|
|
|
|
|
foreach ( $comments as $comment ) {
|
|
|
|
|
$is_customer_note = get_comment_meta( $comment->comment_ID, 'is_customer_note', true );
|
|
|
|
|
$comment->comment_content = make_clickable( $comment->comment_content );
|
|
|
|
|
if ( $is_customer_note ) {
|
|
|
|
|
$notes[] = $comment;
|
|
|
|
|
endforeach;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments') );
|
|
|
|
|
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
|
|
|
|
|
|
|
|
|
|
return (array) $notes;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if an order needs payment, based on status and order total
|
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
* 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 )
|
|
|
|
|
if ( in_array( $this->status, $valid_order_statuses ) && $this->get_total() > 0 ) {
|
|
|
|
|
$needs_payment = true;
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
$needs_payment = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_order_needs_payment', $needs_payment, $this, $valid_order_statuses );
|
|
|
|
|
}
|
|
|
|
|