WC_Order_Data_Store_CPT::search_orders()publicWC 1.0

Search order data for a term and return ids.

Method of the class: WC_Order_Data_Store_CPT{}

Return

Array. of ids

Usage

$WC_Order_Data_Store_CPT = new WC_Order_Data_Store_CPT();
$WC_Order_Data_Store_CPT->search_orders( $term );
$term(string) (required)
Searched term.

WC_Order_Data_Store_CPT::search_orders() code WC 9.4.2

public function search_orders( $term ) {
	global $wpdb;

	/**
	 * Searches on meta data can be slow - this lets you choose what fields to search.
	 * 3.0.0 added _billing_address and _shipping_address meta which contains all address data to make this faster.
	 * This however won't work on older orders unless updated, so search a few others (expand this using the filter if needed).
	 *
	 * @var array
	 */
	$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',
				'_billing_phone',
			)
		)
	);
	$order_ids     = array();

	if ( is_numeric( $term ) ) {
		$order_ids[] = absint( $term );
	}

	if ( ! empty( $search_fields ) ) {
		$order_ids = array_unique(
			array_merge(
				$order_ids,
				$wpdb->get_col(
					$wpdb->prepare(
						"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
						'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
					)
				),
				$wpdb->get_col(
					$wpdb->prepare(
						"SELECT order_id
						FROM {$wpdb->prefix}woocommerce_order_items as order_items
						WHERE order_item_name LIKE %s",
						'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
					)
				),
				$wpdb->get_col(
					$wpdb->prepare(
						"SELECT DISTINCT os.order_id FROM {$wpdb->prefix}wc_order_stats os
						INNER JOIN {$wpdb->prefix}wc_customer_lookup cl ON os.customer_id = cl.customer_id
						INNER JOIN {$wpdb->usermeta} um ON cl.user_id = um.user_id
						WHERE (um.meta_key = 'billing_phone' OR um.meta_key = 'shipping_phone')
						AND um.meta_value = %s",
						wc_clean( $term )
					)
				)
			)
		);
	}

	return apply_filters( 'woocommerce_shop_order_search_results', $order_ids, $term, $search_fields );
}