流程图示如下:

函数如下:
\\ \woocommerce\src\Internal\ProductAttributesLookup\Filterer.php
get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type )
根据属性、属性值以及查询类型,获取符合条件的产品数量。
例如:
$term_ids为51, 52, 53, 54, 44, 45
$taxonomy为pa_size
且当$query_type为and时,sql如下:
#根据根据术语id获取产品数量(AND)
SELECT COUNT(DISTINCT xs_posts.ID) AS term_count, terms.term_id AS term_count_id
FROM xs_posts
INNER JOIN xs_term_relationships AS term_relationships ON xs_posts.ID = term_relationships.object_id
INNER JOIN xs_term_taxonomy AS term_taxonomy USING (term_taxonomy_id)
INNER JOIN xs_terms AS terms USING (term_id)
WHERE xs_posts.post_type IN ('product')
AND xs_posts.post_status = 'publish'
# 下面两个and的sql语句,是核心功能:意思如下:
# 如:51尺码的产品id是1328,然后通过术语联系表,查询产品id是1328的全部术语,然后匹配符合筛选条件,即term_taxonomy_id IN (51, 52)
# 其中=2,可以理解为,用户勾选尺码的选项checked个数。如何1328产品同时有51和51的尺码术语。则说明该产品符合用户筛选条件。
# 以此类推,剩余尺码,即(52, 53, 54, 44, 45),也逐一进行筛选判断。最后筛选出符合条件的产品。
AND ((SELECT COUNT(1) FROM xs_term_relationships WHERE term_taxonomy_id IN (51, 52) AND object_id = xs_posts.ID) =
2 AND xs_posts.ID NOT IN (SELECT object_id FROM xs_term_relationships WHERE term_taxonomy_id IN (28)))
AND terms.term_id IN (51, 52, 53, 54, 44, 45)
GROUP BY terms.term_id;
查询结果如下:

当$query_type为or时,sql如下:
#根据根据术语id获取产品数量(OR)
SELECT COUNT(DISTINCT xs_posts.ID) AS term_count, terms.term_id AS term_count_id
FROM xs_posts
INNER JOIN xs_term_relationships AS term_relationships ON xs_posts.ID = term_relationships.object_id
INNER JOIN xs_term_taxonomy AS term_taxonomy USING (term_taxonomy_id)
INNER JOIN xs_terms AS terms USING (term_id)
WHERE xs_posts.post_type IN ('product')
AND xs_posts.post_status = 'publish'
AND (xs_posts.ID NOT IN (SELECT object_id FROM xs_term_relationships WHERE term_taxonomy_id IN (28)))
AND terms.term_id IN (51, 52, 53, 54, 44, 45)
GROUP BY terms.term_id;
查询结果如下:
