Automattic\WooCommerce\StoreApi\Utilities

ProductQuery::add_query_clauses()publicWC 1.0

Add in conditional search filters for products.

Method of the class: ProductQuery{}

No Hooks.

Return

Array.

Usage

$ProductQuery = new ProductQuery();
$ProductQuery->add_query_clauses( $args, $wp_query );
$args(array) (required)
Query args.
$wp_query(\WC_Query) (required)
WC_Query object.

ProductQuery::add_query_clauses() code WC 9.4.2

public function add_query_clauses( $args, $wp_query ) {
	global $wpdb;

	if ( $wp_query->get( 'search' ) ) {
		$search         = '%' . $wpdb->esc_like( $wp_query->get( 'search' ) ) . '%';
		$search_query   = wc_product_sku_enabled()
			? $wpdb->prepare( " AND ( $wpdb->posts.post_title LIKE %s OR wc_product_meta_lookup.sku LIKE %s ) ", $search, $search )
			: $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $search );
		$args['where'] .= $search_query;
		$args['join']   = $this->append_product_sorting_table_join( $args['join'] );
	}

	if ( $wp_query->get( 'sku' ) ) {
		$skus = explode( ',', $wp_query->get( 'sku' ) );
		// Include the current string as a SKU too.
		if ( 1 < count( $skus ) ) {
			$skus[] = $wp_query->get( 'sku' );
		}
		$args['join']   = $this->append_product_sorting_table_join( $args['join'] );
		$args['where'] .= ' AND wc_product_meta_lookup.sku IN ("' . implode( '","', array_map( 'esc_sql', $skus ) ) . '")';
	}

	if ( $wp_query->get( 'slug' ) ) {
		$slugs = explode( ',', $wp_query->get( 'slug' ) );
		// Include the current string as a slug too.
		if ( 1 < count( $slugs ) ) {
			$slugs[] = $wp_query->get( 'slug' );
		}
		$args['join']   = $this->append_product_sorting_table_join( $args['join'] );
		$post_name__in  = implode( '","', array_map( 'esc_sql', $slugs ) );
		$args['where'] .= " AND $wpdb->posts.post_name IN (\"$post_name__in\")";
	}

	if ( $wp_query->get( 'stock_status' ) ) {
		$args['join']   = $this->append_product_sorting_table_join( $args['join'] );
		$args['where'] .= ' AND wc_product_meta_lookup.stock_status IN ("' . implode( '","', array_map( 'esc_sql', $wp_query->get( 'stock_status' ) ) ) . '")';
	} elseif ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
		$args['join']   = $this->append_product_sorting_table_join( $args['join'] );
		$args['where'] .= ' AND wc_product_meta_lookup.stock_status NOT IN ("outofstock")';
	}

	if ( $wp_query->get( 'min_price' ) || $wp_query->get( 'max_price' ) ) {
		$args = $this->add_price_filter_clauses( $args, $wp_query );
	}

	return $args;
}