Automattic\WooCommerce\StoreApi\Utilities

ProductQuery::add_query_clauses()publicWC 1.0

Add in conditional search filters for products.

Метод класса: ProductQuery{}

Хуков нет.

Возвращает

Массив.

Использование

$ProductQuery = new ProductQuery();
$ProductQuery->add_query_clauses( $args, $wp_query );
$args(массив) (обязательный)
Query args.
$wp_query(\WC_Query) (обязательный)
WC_Query object.

Код ProductQuery::add_query_clauses() WC 7.1.0

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( '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;
}