WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Получай пассивный доход от сайта
функция не описана

Automattic\WooCommerce\Internal\ProductAttributesLookup

Filterer::get_product_counts_query_using_lookup_table() private WC 1.0

Get the query for counting products by terms using the product attributes lookup table.

{} Это метод класса: Filterer{}

Хуков нет.

Возвращает

Массив. An array of SQL query parts.

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

// private - только в коде основоного (родительского) класса
$result = $this->get_product_counts_query_using_lookup_table( $tax_query, $meta_query, $taxonomy, $term_ids );
$tax_query(\WP_Tax_Query) (обязательный)
The current main tax query.
$meta_query(\WP_Meta_Query) (обязательный)
The current main meta query.
$taxonomy(строка) (обязательный)
The attribute name to get the term counts for.
$term_ids(строка) (обязательный)
The term ids to include in the search.

Код Filterer::get_product_counts_query_using_lookup_table() WC 5.5.2

<?php
private function get_product_counts_query_using_lookup_table( $tax_query, $meta_query, $taxonomy, $term_ids ) {
	global $wpdb;

	$meta_query_sql    = $meta_query->get_sql( 'post', $this->lookup_table_name, 'product_or_parent_id' );
	$tax_query_sql     = $tax_query->get_sql( $this->lookup_table_name, 'product_or_parent_id' );
	$hide_out_of_stock = 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' );
	$in_stock_clause   = $hide_out_of_stock ? ' AND in_stock = 1' : '';

	$query['select'] = 'SELECT COUNT(DISTINCT product_or_parent_id) as term_count, term_id as term_count_id';
	$query['from']   = "FROM {$this->lookup_table_name}";
	$query['join']   = "INNER JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$this->lookup_table_name}.product_or_parent_id";

	$term_ids_sql   = $this->get_term_ids_sql( $term_ids );
	$query['where'] = "
		WHERE {$wpdb->posts}.post_type IN ( 'product' )
		AND {$wpdb->posts}.post_status = 'publish'
		{$tax_query_sql['where']} {$meta_query_sql['where']}
		AND {$this->lookup_table_name}.taxonomy='{$taxonomy}'
		AND {$this->lookup_table_name}.term_id IN $term_ids_sql
		{$in_stock_clause}";

	if ( ! empty( $term_ids ) ) {
		$attributes_to_filter_by = \WC_Query::get_layered_nav_chosen_attributes();

		if ( ! empty( $attributes_to_filter_by ) ) {
			$all_terms_to_filter_by = array();
			foreach ( $attributes_to_filter_by as $taxonomy => $data ) {
				$all_terms                  = get_terms( $taxonomy, array( 'hide_empty' => false ) );
				$term_ids_by_slug           = wp_list_pluck( $all_terms, 'term_id', 'slug' );
				$term_ids_to_filter_by      = array_values( array_intersect_key( $term_ids_by_slug, array_flip( $data['terms'] ) ) );
				$all_terms_to_filter_by     = array_merge( $all_terms_to_filter_by, $term_ids_to_filter_by );
				$term_ids_to_filter_by_list = '(' . join( ',', $term_ids_to_filter_by ) . ')';

				$count = count( $term_ids_to_filter_by );
				if ( 0 !== $count ) {
					$query['where'] .= ' AND product_or_parent_id IN (';
					if ( 'and' === $attributes_to_filter_by[ $taxonomy ]['query_type'] ) {
						$query['where'] .= "
							SELECT product_or_parent_id
							FROM {$this->lookup_table_name} lt
							WHERE is_variation_attribute=0
							{$in_stock_clause}
							AND term_id in {$term_ids_to_filter_by_list}
							GROUP BY product_id
							HAVING COUNT(product_id)={$count}
							UNION
							SELECT product_or_parent_id
							FROM {$this->lookup_table_name} lt
							WHERE is_variation_attribute=1
							{$in_stock_clause}
							AND term_id in {$term_ids_to_filter_by_list}
						)";
					} else {
						$query['where'] .= "
							SELECT product_or_parent_id FROM {$this->lookup_table_name}
							WHERE term_id in {$term_ids_to_filter_by_list}
							{$in_stock_clause}
						)";
					}
				}
			}
		} else {
			$query['where'] .= $in_stock_clause;
		}
	} elseif ( $hide_out_of_stock ) {
		$query['where'] .= " AND {$this->lookup_table_name}.in_stock=1";
	}

	$search_query_sql = \WC_Query::get_main_search_query_sql();
	if ( $search_query_sql ) {
		$query['where'] .= ' AND ' . $search_query_sql;
	}

	$query['group_by'] = 'GROUP BY terms.term_id';
	$query['group_by'] = "GROUP BY {$this->lookup_table_name}.term_id";

	return $query;
}