Automattic\WooCommerce\Internal\ProductFilters

FilterData::get_filtered_pricepublicWC 1.0

Get price data for current products.

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

Возвращает

Объект.

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

$FilterData = new FilterData();
$FilterData->get_filtered_price( $query_vars );
$query_vars(массив) (обязательный)
The WP_Query arguments.

Код FilterData::get_filtered_price() WC 10.3.4

public function get_filtered_price( array $query_vars ) {
	/**
	 * Allows offloading the filter data to external services like Elasticsearch.
	 *
	 * @hook woocommerce_pre_product_filter_data
	 *
	 * @since 9.9.0
	 *
	 * @param array  $results      The results for current query.
	 * @param string $filter_type  The type of filter. Accepts price|stock|rating|attribute.
	 * @param array  $query_vars   The query arguments to calculate the filter data.
	 * @param array  $extra        Some filter types require extra arguments for calculation, like attribute.
	 * @return array The filtered results or null to continue with default processing.
	 */
	$pre_filter_counts = apply_filters( 'woocommerce_pre_product_filter_data', null, 'price', $query_vars, array() );

	if ( is_array( $pre_filter_counts ) ) {
		return $pre_filter_counts;
	}

	$transient_key = $this->get_transient_key( $query_vars, 'price' );
	$cached_data   = $this->get_cache( $transient_key );

	if ( ! empty( $cached_data ) ) {
		return $cached_data;
	}

	$results     = array();
	$product_ids = $this->get_cached_product_ids( $query_vars );

	if ( $product_ids ) {
		global $wpdb;

		$price_filter_sql = "
		SELECT min( min_price ) as min_price, MAX( max_price ) as max_price
		FROM {$wpdb->wc_product_meta_lookup}
		WHERE product_id IN ( {$product_ids} )
		";

		/**
		* We can't use $wpdb->prepare() here because using %s with
		* $wpdb->prepare() for a subquery won't work as it will escape the SQL
		* query.
		* We're using the query as is, same as Core does.
		*/
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$results = (array) $wpdb->get_row( $price_filter_sql );
	}

	/**
	 * Filters the product filter data before it is returned.
	 *
	 * @hook woocommerce_product_filter_data
	 * @since 9.9.0
	 *
	 * @param array  $results      The results for current query.
	 * @param string $filter_type  The type of filter. Accepts price|stock|rating|attribute.
	 * @param array  $query_vars   The query arguments to calculate the filter data.
	 * @param array  $extra        Some filter types require extra arguments for calculation, like attribute.
	 * @return array The filtered results
	 */
	$results = apply_filters( 'woocommerce_product_filter_data', $results, 'price', $query_vars, array() );

	$this->set_cache( $transient_key, $results );

	return $results;
}