2016-11-17 10:53:22 +00:00
< ? php
2017-11-23 14:34:33 +00:00
/**
* WC_Order_Data_Store_CPT class file .
*
* @ package WooCommerce / Classes
*/
2016-11-17 10:53:22 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* WC Order Data Store : Stored in CPT .
*
2017-03-15 16:36:53 +00:00
* @ version 3.0 . 0
2016-11-17 10:53:22 +00:00
*/
2016-11-22 13:54:51 +00:00
class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implements WC_Object_Data_Store_Interface , WC_Order_Data_Store_Interface {
2016-11-17 10:53:22 +00:00
2016-11-21 23:48:49 +00:00
/**
* Data stored in meta keys , but not considered " meta " for an order .
2017-11-23 12:41:31 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-21 23:48:49 +00:00
* @ var array
*/
protected $internal_meta_keys = array (
'_customer_user' ,
'_order_key' ,
'_order_currency' ,
'_billing_first_name' ,
'_billing_last_name' ,
'_billing_company' ,
'_billing_address_1' ,
'_billing_address_2' ,
'_billing_city' ,
'_billing_state' ,
'_billing_postcode' ,
'_billing_country' ,
'_billing_email' ,
'_billing_phone' ,
'_shipping_first_name' ,
'_shipping_last_name' ,
'_shipping_company' ,
'_shipping_address_1' ,
'_shipping_address_2' ,
'_shipping_city' ,
'_shipping_state' ,
'_shipping_postcode' ,
'_shipping_country' ,
'_completed_date' ,
'_paid_date' ,
'_edit_lock' ,
'_edit_last' ,
'_cart_discount' ,
'_cart_discount_tax' ,
'_order_shipping' ,
'_order_shipping_tax' ,
'_order_tax' ,
'_order_total' ,
'_payment_method' ,
'_payment_method_title' ,
'_transaction_id' ,
'_customer_ip_address' ,
'_customer_user_agent' ,
'_created_via' ,
'_order_version' ,
'_prices_include_tax' ,
'_date_completed' ,
'_date_paid' ,
'_payment_tokens' ,
'_billing_address_index' ,
2016-11-23 01:47:13 +00:00
'_shipping_address_index' ,
2016-12-19 14:51:56 +00:00
'_recorded_sales' ,
2017-02-14 10:33:32 +00:00
'_recorded_coupon_usage_counts' ,
2016-11-21 23:48:49 +00:00
);
2016-11-17 10:53:22 +00:00
/**
* Method to create a new order in the database .
2017-11-23 12:41:31 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
2016-11-17 10:53:22 +00:00
*/
public function create ( & $order ) {
2016-11-17 14:37:29 +00:00
$order -> set_order_key ( 'wc_' . apply_filters ( 'woocommerce_generate_order_key' , uniqid ( 'order_' ) ) );
parent :: create ( $order );
do_action ( 'woocommerce_new_order' , $order -> get_id () );
2016-11-17 10:53:22 +00:00
}
/**
* Read order data . Can be overridden by child classes to load other props .
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
* @ param object $post_object Post object .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-17 10:53:22 +00:00
*/
2016-11-17 14:37:29 +00:00
protected function read_order_data ( & $order , $post_object ) {
parent :: read_order_data ( $order , $post_object );
2017-03-10 14:48:18 +00:00
$id = $order -> get_id ();
$date_completed = get_post_meta ( $id , '_date_completed' , true );
$date_paid = get_post_meta ( $id , '_date_paid' , true );
if ( ! $date_completed ) {
$date_completed = get_post_meta ( $id , '_completed_date' , true );
}
if ( ! $date_paid ) {
$date_paid = get_post_meta ( $id , '_paid_date' , true );
}
2016-11-17 10:53:22 +00:00
2017-12-08 17:55:53 +00:00
// Make sure post_author is used only after WC 3.3.0 DB upgrade routine is executed.
if ( version_compare ( get_option ( 'woocommerce_db_version' ), '3.3.0' , '>=' ) ) {
$customer_id = $post_object -> post_author ;
} else {
$customer_id = get_post_meta ( $id , '_customer_user' , true );
}
2017-11-23 12:41:31 +00:00
$order -> set_props (
array (
'order_key' => get_post_meta ( $id , '_order_key' , true ),
2017-12-08 17:55:53 +00:00
'customer_id' => $customer_id ,
2017-11-23 12:41:31 +00:00
'billing_first_name' => get_post_meta ( $id , '_billing_first_name' , true ),
'billing_last_name' => get_post_meta ( $id , '_billing_last_name' , true ),
'billing_company' => get_post_meta ( $id , '_billing_company' , true ),
'billing_address_1' => get_post_meta ( $id , '_billing_address_1' , true ),
'billing_address_2' => get_post_meta ( $id , '_billing_address_2' , true ),
'billing_city' => get_post_meta ( $id , '_billing_city' , true ),
'billing_state' => get_post_meta ( $id , '_billing_state' , true ),
'billing_postcode' => get_post_meta ( $id , '_billing_postcode' , true ),
'billing_country' => get_post_meta ( $id , '_billing_country' , true ),
'billing_email' => get_post_meta ( $id , '_billing_email' , true ),
'billing_phone' => get_post_meta ( $id , '_billing_phone' , true ),
'shipping_first_name' => get_post_meta ( $id , '_shipping_first_name' , true ),
'shipping_last_name' => get_post_meta ( $id , '_shipping_last_name' , true ),
'shipping_company' => get_post_meta ( $id , '_shipping_company' , true ),
'shipping_address_1' => get_post_meta ( $id , '_shipping_address_1' , true ),
'shipping_address_2' => get_post_meta ( $id , '_shipping_address_2' , true ),
'shipping_city' => get_post_meta ( $id , '_shipping_city' , true ),
'shipping_state' => get_post_meta ( $id , '_shipping_state' , true ),
'shipping_postcode' => get_post_meta ( $id , '_shipping_postcode' , true ),
'shipping_country' => get_post_meta ( $id , '_shipping_country' , true ),
'payment_method' => get_post_meta ( $id , '_payment_method' , true ),
'payment_method_title' => get_post_meta ( $id , '_payment_method_title' , true ),
'transaction_id' => get_post_meta ( $id , '_transaction_id' , true ),
'customer_ip_address' => get_post_meta ( $id , '_customer_ip_address' , true ),
'customer_user_agent' => get_post_meta ( $id , '_customer_user_agent' , true ),
'created_via' => get_post_meta ( $id , '_created_via' , true ),
'date_completed' => $date_completed ,
'date_paid' => $date_paid ,
'cart_hash' => get_post_meta ( $id , '_cart_hash' , true ),
'customer_note' => $post_object -> post_excerpt ,
)
);
2016-11-17 10:53:22 +00:00
}
2016-12-20 15:44:37 +00:00
/**
* Method to update an order in the database .
2017-11-23 12:41:31 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
2016-12-20 15:44:37 +00:00
*/
public function update ( & $order ) {
2017-03-09 14:40:19 +00:00
// Before updating, ensure date paid is set if missing.
2017-04-26 10:08:47 +00:00
if ( ! $order -> get_date_paid ( 'edit' ) && version_compare ( $order -> get_version ( 'edit' ), '3.0' , '<' ) && $order -> has_status ( apply_filters ( 'woocommerce_payment_complete_order_status' , $order -> needs_processing () ? 'processing' : 'completed' , $order -> get_id (), $order ) ) ) {
2017-03-09 14:40:19 +00:00
$order -> set_date_paid ( $order -> get_date_created ( 'edit' ) );
}
// Update the order.
2016-12-20 15:44:37 +00:00
parent :: update ( $order );
2017-03-09 14:40:19 +00:00
2016-12-20 15:44:37 +00:00
do_action ( 'woocommerce_update_order' , $order -> get_id () );
}
2016-11-17 10:53:22 +00:00
/**
* Helper method that updates all the post meta for an order based on it ' s settings in the WC_Order class .
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-11-17 10:53:22 +00:00
*/
2017-01-23 20:20:29 +00:00
protected function update_post_meta ( & $order ) {
2016-11-17 10:53:22 +00:00
$updated_props = array ();
2017-02-01 00:44:16 +00:00
$id = $order -> get_id ();
2016-11-17 10:53:22 +00:00
$meta_key_to_props = array (
2016-11-17 14:37:29 +00:00
'_order_key' => 'order_key' ,
2016-11-18 11:14:09 +00:00
'_customer_user' => 'customer_id' ,
2016-11-17 14:37:29 +00:00
'_payment_method' => 'payment_method' ,
'_payment_method_title' => 'payment_method_title' ,
'_transaction_id' => 'transaction_id' ,
'_customer_ip_address' => 'customer_ip_address' ,
'_customer_user_agent' => 'customer_user_agent' ,
'_created_via' => 'created_via' ,
2017-03-10 14:44:00 +00:00
'_date_completed' => 'date_completed' ,
'_date_paid' => 'date_paid' ,
2016-11-17 14:37:29 +00:00
'_cart_hash' => 'cart_hash' ,
2016-11-17 10:53:22 +00:00
);
2016-11-17 15:32:52 +00:00
2017-01-23 20:20:29 +00:00
$props_to_update = $this -> get_props_to_update ( $order , $meta_key_to_props );
2017-03-10 14:44:00 +00:00
2017-01-23 20:20:29 +00:00
foreach ( $props_to_update as $meta_key => $prop ) {
2016-11-17 15:32:52 +00:00
$value = $order -> { " get_ $prop " }( 'edit' );
2017-03-10 14:44:00 +00:00
if ( 'date_paid' === $prop ) {
2017-03-15 16:36:53 +00:00
// In 3.0.x we store this as a UTC timestamp.
2017-03-15 13:33:24 +00:00
update_post_meta ( $id , $meta_key , ! is_null ( $value ) ? $value -> getTimestamp () : '' );
2017-03-10 14:44:00 +00:00
// In 2.6.x date_paid was stored as _paid_date in local mysql format.
2017-03-15 13:33:24 +00:00
update_post_meta ( $id , '_paid_date' , ! is_null ( $value ) ? $value -> date ( 'Y-m-d H:i:s' ) : '' );
2017-03-10 14:44:00 +00:00
} elseif ( 'date_completed' === $prop ) {
2017-03-15 16:36:53 +00:00
// In 3.0.x we store this as a UTC timestamp.
2017-03-15 13:33:24 +00:00
update_post_meta ( $id , $meta_key , ! is_null ( $value ) ? $value -> getTimestamp () : '' );
2017-03-10 14:44:00 +00:00
// In 2.6.x date_paid was stored as _paid_date in local mysql format.
2017-03-15 13:33:24 +00:00
update_post_meta ( $id , '_completed_date' , ! is_null ( $value ) ? $value -> date ( 'Y-m-d H:i:s' ) : '' );
2017-03-10 14:44:00 +00:00
} else {
update_post_meta ( $id , $meta_key , $value );
}
2017-01-23 20:20:29 +00:00
$updated_props [] = $prop ;
2016-11-17 15:32:52 +00:00
}
2016-11-17 15:05:40 +00:00
2016-12-05 16:08:51 +00:00
$address_props = array (
2017-11-23 12:41:31 +00:00
'billing' => array (
2016-12-05 16:08:51 +00:00
'_billing_first_name' => 'billing_first_name' ,
'_billing_last_name' => 'billing_last_name' ,
'_billing_company' => 'billing_company' ,
'_billing_address_1' => 'billing_address_1' ,
'_billing_address_2' => 'billing_address_2' ,
'_billing_city' => 'billing_city' ,
'_billing_state' => 'billing_state' ,
'_billing_postcode' => 'billing_postcode' ,
'_billing_country' => 'billing_country' ,
'_billing_email' => 'billing_email' ,
'_billing_phone' => 'billing_phone' ,
),
'shipping' => array (
'_shipping_first_name' => 'shipping_first_name' ,
'_shipping_last_name' => 'shipping_last_name' ,
'_shipping_company' => 'shipping_company' ,
'_shipping_address_1' => 'shipping_address_1' ,
'_shipping_address_2' => 'shipping_address_2' ,
'_shipping_city' => 'shipping_city' ,
'_shipping_state' => 'shipping_state' ,
'_shipping_postcode' => 'shipping_postcode' ,
'_shipping_country' => 'shipping_country' ,
),
2016-11-17 15:32:52 +00:00
);
2016-11-17 15:05:40 +00:00
2016-12-05 16:08:51 +00:00
foreach ( $address_props as $props_key => $props ) {
2017-01-23 20:20:29 +00:00
$props_to_update = $this -> get_props_to_update ( $order , $props );
foreach ( $props_to_update as $meta_key => $prop ) {
2016-12-05 16:08:51 +00:00
$value = $order -> { " get_ $prop " }( 'edit' );
2017-02-01 00:44:16 +00:00
update_post_meta ( $id , $meta_key , $value );
2017-01-23 20:20:29 +00:00
$updated_props [] = $prop ;
$updated_props [] = $props_key ;
2016-11-17 10:53:22 +00:00
}
}
2017-01-23 20:20:29 +00:00
parent :: update_post_meta ( $order );
2016-11-17 14:37:29 +00:00
2017-03-20 10:03:14 +00:00
// If address changed, store concatenated version to make searches faster.
2017-02-01 00:44:16 +00:00
if ( in_array ( 'billing' , $updated_props ) || ! metadata_exists ( 'post' , $id , '_billing_address_index' ) ) {
update_post_meta ( $id , '_billing_address_index' , implode ( ' ' , $order -> get_address ( 'billing' ) ) );
2016-11-18 14:07:21 +00:00
}
2017-02-01 00:44:16 +00:00
if ( in_array ( 'shipping' , $updated_props ) || ! metadata_exists ( 'post' , $id , '_shipping_address_index' ) ) {
update_post_meta ( $id , '_shipping_address_index' , implode ( ' ' , $order -> get_address ( 'shipping' ) ) );
2016-11-18 14:07:21 +00:00
}
2017-11-23 14:47:53 +00:00
// If customer email changed, update any downloadable permissions.
if ( in_array ( 'billing_email' , $updated_props ) ) {
$this -> update_downloadable_permissions ( $order );
2016-11-17 10:53:22 +00:00
}
2017-01-24 21:38:02 +00:00
do_action ( 'woocommerce_order_object_updated_props' , $order , $updated_props );
2016-11-17 10:53:22 +00:00
}
2016-11-25 21:46:45 +00:00
/**
* Excerpt for post .
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
2016-11-25 21:46:45 +00:00
* @ return string
*/
protected function get_post_excerpt ( $order ) {
return $order -> get_customer_note ();
}
2016-11-17 10:53:22 +00:00
/**
2016-11-17 14:37:29 +00:00
* Get amount already refunded .
2016-11-17 10:53:22 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
2016-11-17 14:37:29 +00:00
* @ return string
2016-11-17 10:53:22 +00:00
*/
2016-11-17 14:37:29 +00:00
public function get_total_refunded ( $order ) {
global $wpdb ;
2017-11-23 12:41:31 +00:00
$total = $wpdb -> get_var (
$wpdb -> prepare (
" SELECT SUM( postmeta.meta_value )
FROM $wpdb -> postmeta AS postmeta
INNER JOIN $wpdb -> posts AS posts ON ( posts . post_type = 'shop_order_refund' AND posts . post_parent = % d )
WHERE postmeta . meta_key = '_refund_amount'
AND postmeta . post_id = posts . ID " ,
$order -> get_id ()
)
);
2016-11-17 14:37:29 +00:00
return $total ;
2016-11-17 10:53:22 +00:00
}
2016-11-17 12:34:39 +00:00
/**
2016-11-17 14:37:29 +00:00
* Get the total tax refunded .
2016-11-17 13:18:24 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
2016-11-17 14:37:29 +00:00
* @ return float
2016-11-17 12:34:39 +00:00
*/
2016-11-17 14:37:29 +00:00
public function get_total_tax_refunded ( $order ) {
2016-11-17 12:34:39 +00:00
global $wpdb ;
2017-11-23 12:41:31 +00:00
$total = $wpdb -> get_var (
$wpdb -> prepare (
" SELECT SUM( order_itemmeta.meta_value )
FROM { $wpdb -> prefix } woocommerce_order_itemmeta AS order_itemmeta
INNER JOIN $wpdb -> posts AS posts ON ( posts . post_type = 'shop_order_refund' AND posts . post_parent = % d )
INNER JOIN { $wpdb -> prefix } woocommerce_order_items AS order_items ON ( order_items . order_id = posts . ID AND order_items . order_item_type = 'tax' )
WHERE order_itemmeta . order_item_id = order_items . order_item_id
AND order_itemmeta . meta_key IN ( 'tax_amount' , 'shipping_tax_amount' ) " ,
$order -> get_id ()
)
);
2016-11-17 12:34:39 +00:00
2016-11-17 14:37:29 +00:00
return abs ( $total );
2016-11-17 12:34:39 +00:00
}
/**
2016-11-17 14:37:29 +00:00
* Get the total shipping refunded .
2016-11-17 12:34:39 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order $order Order object .
2016-11-17 14:37:29 +00:00
* @ return float
2016-11-17 12:34:39 +00:00
*/
2016-11-17 14:37:29 +00:00
public function get_total_shipping_refunded ( $order ) {
2016-11-17 12:34:39 +00:00
global $wpdb ;
2017-11-23 12:41:31 +00:00
$total = $wpdb -> get_var (
$wpdb -> prepare (
" SELECT SUM( order_itemmeta.meta_value )
FROM { $wpdb -> prefix } woocommerce_order_itemmeta AS order_itemmeta
INNER JOIN $wpdb -> posts AS posts ON ( posts . post_type = 'shop_order_refund' AND posts . post_parent = % d )
INNER JOIN { $wpdb -> prefix } woocommerce_order_items AS order_items ON ( order_items . order_id = posts . ID AND order_items . order_item_type = 'shipping' )
WHERE order_itemmeta . order_item_id = order_items . order_item_id
AND order_itemmeta . meta_key IN ( 'cost' ) " ,
$order -> get_id ()
)
);
2016-11-17 12:34:39 +00:00
2016-11-17 14:37:29 +00:00
return abs ( $total );
2016-11-17 12:34:39 +00:00
}
2016-11-18 14:07:21 +00:00
/**
* Finds an Order ID based on an order key .
*
2017-11-23 14:34:33 +00:00
* @ param string $order_key An order key has generated by .
2016-11-18 14:07:21 +00:00
* @ return int The ID of an order , or 0 if the order could not be found
*/
2016-11-21 14:30:56 +00:00
public function get_order_id_by_order_key ( $order_key ) {
2016-11-18 14:07:21 +00:00
global $wpdb ;
return $wpdb -> get_var ( $wpdb -> prepare ( " SELECT post_id FROM { $wpdb -> prefix } postmeta WHERE meta_key = '_order_key' AND meta_value = %s " , $order_key ) );
}
/**
2017-01-19 23:36:26 +00:00
* Return count of orders with a specific status .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param string $status Order status . Function wc_get_order_statuses () returns a list of valid statuses .
2016-11-18 14:07:21 +00:00
* @ return int
*/
2017-01-19 23:36:26 +00:00
public function get_order_count ( $status ) {
2016-11-18 14:07:21 +00:00
global $wpdb ;
2017-01-19 23:36:26 +00:00
return absint ( $wpdb -> get_var ( $wpdb -> prepare ( " SELECT COUNT( * ) FROM { $wpdb -> posts } WHERE post_type = 'shop_order' AND post_status = %s " , $status ) ) );
2016-11-18 14:07:21 +00:00
}
/**
* Get all orders matching the passed in args .
*
2017-05-12 20:06:17 +00:00
* @ deprecated 3.1 . 0 - Use wc_get_orders instead .
2016-11-18 14:07:21 +00:00
* @ see wc_get_orders ()
2017-05-15 11:50:52 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param array $args List of args passed to wc_get_orders () .
2017-05-15 11:50:52 +00:00
*
* @ return array | object
2016-11-18 14:07:21 +00:00
*/
public function get_orders ( $args = array () ) {
2017-05-12 20:06:17 +00:00
wc_deprecated_function ( 'WC_Order_Data_Store_CPT::get_orders' , '3.1.0' , 'Use wc_get_orders instead.' );
return wc_get_orders ( $args );
2016-11-18 14:07:21 +00:00
}
/**
* Generate meta query for wc_get_orders .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param array $values List of customers ids or emails .
* @ param string $relation 'or' or 'and' relation used to build the WP meta_query .
2016-11-18 14:07:21 +00:00
* @ return array
*/
private function get_orders_generate_customer_meta_query ( $values , $relation = 'or' ) {
$meta_query = array (
2017-11-23 12:41:31 +00:00
'relation' => strtoupper ( $relation ),
2016-11-18 14:07:21 +00:00
'customer_emails' => array (
'key' => '_billing_email' ,
'value' => array (),
'compare' => 'IN' ,
),
2017-11-23 12:41:31 +00:00
'customer_ids' => array (
2016-11-18 14:07:21 +00:00
'key' => '_customer_user' ,
'value' => array (),
'compare' => 'IN' ,
),
);
foreach ( $values as $value ) {
if ( is_array ( $value ) ) {
2017-06-13 20:32:21 +00:00
$query_part = $this -> get_orders_generate_customer_meta_query ( $value , 'and' );
if ( is_wp_error ( $query_part ) ) {
return $query_part ;
}
$meta_query [] = $query_part ;
2016-11-18 14:07:21 +00:00
} elseif ( is_email ( $value ) ) {
$meta_query [ 'customer_emails' ][ 'value' ][] = sanitize_email ( $value );
2017-06-13 20:32:21 +00:00
} elseif ( is_numeric ( $value ) ) {
2016-11-18 14:07:21 +00:00
$meta_query [ 'customer_ids' ][ 'value' ][] = strval ( absint ( $value ) );
2017-06-13 20:32:21 +00:00
} else {
return new WP_Error ( 'woocommerce_query_invalid' , __ ( 'Invalid customer query.' , 'woocommerce' ), $values );
2016-11-18 14:07:21 +00:00
}
}
if ( empty ( $meta_query [ 'customer_emails' ][ 'value' ] ) ) {
unset ( $meta_query [ 'customer_emails' ] );
unset ( $meta_query [ 'relation' ] );
}
if ( empty ( $meta_query [ 'customer_ids' ][ 'value' ] ) ) {
unset ( $meta_query [ 'customer_ids' ] );
unset ( $meta_query [ 'relation' ] );
}
return $meta_query ;
}
/**
* Get unpaid orders after a certain date ,
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param int $date Timestamp .
2016-11-18 14:07:21 +00:00
* @ return array
*/
public function get_unpaid_orders ( $date ) {
global $wpdb ;
2017-11-23 12:41:31 +00:00
$unpaid_orders = $wpdb -> get_col (
$wpdb -> prepare (
2017-11-27 13:00:42 +00:00
// @codingStandardsIgnoreStart
2017-11-23 12:41:31 +00:00
" SELECT posts.ID
FROM { $wpdb -> posts } AS posts
WHERE posts . post_type IN ( '" . implode( "' , '", wc_get_order_types() ) . "' )
AND posts . post_status = 'wc-pending'
AND posts . post_modified < % s " ,
2017-11-27 13:00:42 +00:00
// @codingStandardsIgnoreEnd
2017-11-23 12:41:31 +00:00
date ( 'Y-m-d H:i:s' , absint ( $date ) )
)
);
2016-11-18 14:07:21 +00:00
return $unpaid_orders ;
}
/**
* Search order data for a term and return ids .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param string $term Searched term .
2016-11-18 14:07:21 +00:00
* @ return array of ids
*/
public function search_orders ( $term ) {
global $wpdb ;
/**
* Searches on meta data can be slow - this lets you choose what fields to search .
2017-03-15 16:36:53 +00:00
* 3.0 . 0 added _billing_address and _shipping_address meta which contains all address data to make this faster .
2016-11-18 14:07:21 +00:00
* This however won ' t work on older orders unless updated , so search a few others ( expand this using the filter if needed ) .
2017-11-23 12:41:31 +00:00
*
2016-11-18 14:07:21 +00:00
* @ var array
*/
2017-11-23 12:41:31 +00:00
$search_fields = array_map (
'wc_clean' , apply_filters (
'woocommerce_shop_order_search_fields' , array (
'_billing_address_index' ,
'_shipping_address_index' ,
'_billing_last_name' ,
'_billing_email' ,
)
)
);
$order_ids = array ();
2016-11-18 14:07:21 +00:00
if ( is_numeric ( $term ) ) {
$order_ids [] = absint ( $term );
}
if ( ! empty ( $search_fields ) ) {
2017-11-23 12:41:31 +00:00
$order_ids = array_unique (
array_merge (
$order_ids ,
$wpdb -> get_col (
2017-11-23 14:34:33 +00:00
$wpdb -> prepare (
2017-11-27 13:00:42 +00:00
" SELECT DISTINCT p1.post_id FROM { $wpdb -> postmeta } p1 WHERE p1.meta_value LIKE %s AND p1.meta_key IN (' " . implode ( " ',' " , array_map ( 'esc_sql' , $search_fields ) ) . " ') " , // @codingStandardsIgnoreLine
2017-11-23 14:34:33 +00:00
'%' . $wpdb -> esc_like ( wc_clean ( $term ) ) . '%'
)
2017-11-23 12:41:31 +00:00
),
$wpdb -> get_col (
$wpdb -> prepare (
" SELECT order_id
FROM { $wpdb -> prefix } woocommerce_order_items as order_items
2017-11-23 14:34:33 +00:00
WHERE order_item_name LIKE % s " ,
'%' . $wpdb -> esc_like ( wc_clean ( $term ) ) . '%'
2017-11-23 12:41:31 +00:00
)
2016-11-18 14:07:21 +00:00
)
)
2017-11-23 12:41:31 +00:00
);
2016-11-18 14:07:21 +00:00
}
2017-05-03 21:14:41 +00:00
return apply_filters ( 'woocommerce_shop_order_search_results' , $order_ids , $term , $search_fields );
2016-11-18 14:07:21 +00:00
}
/**
* Gets information about whether permissions were generated yet .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
2016-11-21 14:30:56 +00:00
* @ return bool
2016-11-18 14:07:21 +00:00
*/
public function get_download_permissions_granted ( $order ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2016-11-18 19:29:37 +00:00
return wc_string_to_bool ( get_post_meta ( $order_id , '_download_permissions_granted' , true ) );
2016-11-18 14:07:21 +00:00
}
/**
* Stores information about whether permissions were generated yet .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
* @ param bool $set True or false .
2016-11-18 14:07:21 +00:00
*/
public function set_download_permissions_granted ( $order , $set ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2016-11-18 19:29:37 +00:00
update_post_meta ( $order_id , '_download_permissions_granted' , wc_bool_to_string ( $set ) );
2016-11-18 14:07:21 +00:00
}
/**
* Gets information about whether sales were recorded .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
2016-11-21 14:30:56 +00:00
* @ return bool
2016-11-18 14:07:21 +00:00
*/
public function get_recorded_sales ( $order ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2016-11-18 19:29:37 +00:00
return wc_string_to_bool ( get_post_meta ( $order_id , '_recorded_sales' , true ) );
2016-11-18 14:07:21 +00:00
}
/**
* Stores information about whether sales were recorded .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
* @ param bool $set True or false .
2016-11-18 14:07:21 +00:00
*/
public function set_recorded_sales ( $order , $set ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2016-11-18 19:29:37 +00:00
update_post_meta ( $order_id , '_recorded_sales' , wc_bool_to_string ( $set ) );
2016-11-18 14:07:21 +00:00
}
/**
* Gets information about whether coupon counts were updated .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
2016-11-21 14:30:56 +00:00
* @ return bool
2016-11-18 14:07:21 +00:00
*/
public function get_recorded_coupon_usage_counts ( $order ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2017-02-14 10:33:32 +00:00
return wc_string_to_bool ( get_post_meta ( $order_id , '_recorded_coupon_usage_counts' , true ) );
2016-11-18 14:07:21 +00:00
}
/**
* Stores information about whether coupon counts were updated .
2016-11-18 19:29:37 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
* @ param bool $set True or false .
2016-11-18 14:07:21 +00:00
*/
public function set_recorded_coupon_usage_counts ( $order , $set ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2017-02-14 10:33:32 +00:00
update_post_meta ( $order_id , '_recorded_coupon_usage_counts' , wc_bool_to_string ( $set ) );
2016-11-18 19:29:37 +00:00
}
/**
* Gets information about whether stock was reduced .
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
2016-11-21 14:30:56 +00:00
* @ return bool
2016-11-18 19:29:37 +00:00
*/
public function get_stock_reduced ( $order ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2016-11-18 19:29:37 +00:00
return wc_string_to_bool ( get_post_meta ( $order_id , '_order_stock_reduced' , true ) );
}
/**
* Stores information about whether stock was reduced .
*
2017-11-23 14:34:33 +00:00
* @ param WC_Order | int $order Order ID or order object .
* @ param bool $set True or false .
2016-11-18 19:29:37 +00:00
*/
public function set_stock_reduced ( $order , $set ) {
2016-11-21 14:30:56 +00:00
$order_id = WC_Order_Factory :: get_order_id ( $order );
2016-11-18 19:29:37 +00:00
update_post_meta ( $order_id , '_order_stock_reduced' , wc_bool_to_string ( $set ) );
2016-11-18 14:07:21 +00:00
}
2017-02-08 19:18:39 +00:00
/**
* Get the order type based on Order ID .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-11-23 14:34:33 +00:00
* @ param int $order_id Order ID .
2017-02-08 19:18:39 +00:00
* @ return string
*/
public function get_order_type ( $order_id ) {
return get_post_type ( $order_id );
}
2017-04-25 21:52:17 +00:00
2017-04-26 17:49:19 +00:00
/**
* Get valid WP_Query args from a WC_Order_Query ' s query variables .
*
* @ since 3.1 . 0
2017-11-23 14:34:33 +00:00
* @ param array $query_vars query vars from a WC_Order_Query .
2017-04-26 17:49:19 +00:00
* @ return array
*/
2017-04-27 18:07:03 +00:00
protected function get_wp_query_args ( $query_vars ) {
2017-04-26 17:49:19 +00:00
2017-05-24 01:44:37 +00:00
// Map query vars to ones that get_wp_query_args or WP_Query recognize.
2017-04-26 17:49:19 +00:00
$key_mapping = array (
2017-05-24 01:44:37 +00:00
'customer_id' => 'customer_user' ,
'status' => 'post_status' ,
'currency' => 'order_currency' ,
'version' => 'order_version' ,
'discount_total' => 'cart_discount' ,
'discount_tax' => 'cart_discount_tax' ,
'shipping_total' => 'order_shipping' ,
'shipping_tax' => 'order_shipping_tax' ,
'cart_tax' => 'order_tax' ,
'total' => 'order_total' ,
2017-07-03 11:24:30 +00:00
'page' => 'paged' ,
2017-04-26 17:49:19 +00:00
);
2017-05-09 21:55:39 +00:00
foreach ( $key_mapping as $query_key => $db_key ) {
2017-04-26 17:49:19 +00:00
if ( isset ( $query_vars [ $query_key ] ) ) {
$query_vars [ $db_key ] = $query_vars [ $query_key ];
unset ( $query_vars [ $query_key ] );
}
}
2017-05-24 01:44:37 +00:00
// Add the 'wc-' prefix to status if needed.
if ( ! empty ( $query_vars [ 'post_status' ] ) ) {
if ( is_array ( $query_vars [ 'post_status' ] ) ) {
foreach ( $query_vars [ 'post_status' ] as & $status ) {
$status = wc_is_order_status ( 'wc-' . $status ) ? 'wc-' . $status : $status ;
}
} else {
2017-05-24 01:52:03 +00:00
$query_vars [ 'post_status' ] = wc_is_order_status ( 'wc-' . $query_vars [ 'post_status' ] ) ? 'wc-' . $query_vars [ 'post_status' ] : $query_vars [ 'post_status' ];
2017-05-24 01:44:37 +00:00
}
}
2017-04-27 21:09:10 +00:00
$wp_query_args = parent :: get_wp_query_args ( $query_vars );
2017-04-27 21:46:00 +00:00
if ( ! isset ( $wp_query_args [ 'date_query' ] ) ) {
2017-04-27 21:09:10 +00:00
$wp_query_args [ 'date_query' ] = array ();
}
if ( ! isset ( $wp_query_args [ 'meta_query' ] ) ) {
$wp_query_args [ 'meta_query' ] = array ();
}
2017-05-09 18:37:45 +00:00
$date_queries = array (
'date_created' => 'post_date' ,
'date_modified' => 'post_modified' ,
'date_completed' => '_date_completed' ,
'date_paid' => '_date_paid' ,
);
foreach ( $date_queries as $query_var_key => $db_key ) {
if ( isset ( $query_vars [ $query_var_key ] ) && '' !== $query_vars [ $query_var_key ] ) {
2017-05-09 21:49:45 +00:00
// Remove any existing meta queries for the same keys to prevent conflicts.
$existing_queries = wp_list_pluck ( $wp_query_args [ 'meta_query' ], 'key' , true );
2017-10-24 20:07:21 +00:00
$meta_query_index = array_search ( $db_key , $existing_queries );
if ( false !== $meta_query_index ) {
unset ( $wp_query_args [ 'meta_query' ][ $meta_query_index ] );
2017-05-09 21:49:45 +00:00
}
2017-04-27 21:09:10 +00:00
2017-05-09 21:49:45 +00:00
$wp_query_args = $this -> parse_date_for_wp_query ( $query_vars [ $query_var_key ], $db_key , $wp_query_args );
}
2017-04-27 21:09:10 +00:00
}
2017-05-15 00:05:46 +00:00
if ( isset ( $query_vars [ 'customer' ] ) && '' !== $query_vars [ 'customer' ] && array () !== $query_vars [ 'customer' ] ) {
2017-11-23 12:41:31 +00:00
$values = is_array ( $query_vars [ 'customer' ] ) ? $query_vars [ 'customer' ] : array ( $query_vars [ 'customer' ] );
2017-06-13 20:32:21 +00:00
$customer_query = $this -> get_orders_generate_customer_meta_query ( $values );
2017-06-13 21:00:12 +00:00
if ( is_wp_error ( $customer_query ) ) {
2017-06-13 20:32:21 +00:00
$wp_query_args [ 'errors' ][] = $customer_query ;
} else {
$wp_query_args [ 'meta_query' ][] = $customer_query ;
}
2017-05-12 18:45:01 +00:00
}
2017-04-28 02:14:48 +00:00
if ( ! isset ( $query_vars [ 'paginate' ] ) || ! $query_vars [ 'paginate' ] ) {
$wp_query_args [ 'no_found_rows' ] = true ;
}
2017-05-12 20:14:52 +00:00
return apply_filters ( 'woocommerce_order_data_store_cpt_get_orders_query' , $wp_query_args , $query_vars , $this );
2017-04-26 17:49:19 +00:00
}
/**
* Query for Orders matching specific criteria .
*
* @ since 3.1 . 0
2017-05-15 11:50:52 +00:00
*
2017-11-23 14:34:33 +00:00
* @ param array $query_vars query vars from a WC_Order_Query .
2017-05-15 11:50:52 +00:00
*
* @ return array | object
2017-04-26 17:49:19 +00:00
*/
2017-04-25 21:52:17 +00:00
public function query ( $query_vars ) {
$args = $this -> get_wp_query_args ( $query_vars );
2017-06-13 20:32:21 +00:00
if ( ! empty ( $args [ 'errors' ] ) ) {
$query = ( object ) array (
2017-11-23 12:41:31 +00:00
'posts' => array (),
'found_posts' => 0 ,
2017-06-13 20:32:21 +00:00
'max_num_pages' => 0 ,
);
} else {
$query = new WP_Query ( $args );
}
2017-06-12 20:26:29 +00:00
$orders = ( isset ( $query_vars [ 'return' ] ) && 'ids' === $query_vars [ 'return' ] ) ? $query -> posts : array_filter ( array_map ( 'wc_get_order' , $query -> posts ) );
2017-04-28 02:14:48 +00:00
if ( isset ( $query_vars [ 'paginate' ] ) && $query_vars [ 'paginate' ] ) {
return ( object ) array (
'orders' => $orders ,
'total' => $query -> found_posts ,
'max_num_pages' => $query -> max_num_pages ,
);
}
return $orders ;
2017-04-25 21:52:17 +00:00
}
2017-08-10 13:38:02 +00:00
/**
* Return the order type of a given item which belongs to WC_Order .
*
* @ since 3.2 . 0
* @ param WC_Order $order Order Object .
* @ param int $order_item_id Order item id .
* @ return string Order Item type
*/
2017-08-10 14:33:01 +00:00
public function get_order_item_type ( $order , $order_item_id ) {
2017-08-10 13:38:02 +00:00
global $wpdb ;
2017-08-10 14:33:01 +00:00
return $wpdb -> get_var ( $wpdb -> prepare ( " SELECT DISTINCT order_item_type FROM { $wpdb -> prefix } woocommerce_order_items WHERE order_id = %d and order_item_id = %d; " , $order -> get_id (), $order_item_id ) );
2017-08-10 13:38:02 +00:00
}
2016-11-17 10:53:22 +00:00
}