Woocommerce - 产品属性查询

Does anyone know a way to query category archive page products by a range for a product attribute. The following works but this is because this is a custom field for a product as apposed to a product attribute which is stored as a taxonomy.

$meta_query[] = array(
    'key' => '_height',
    'value' => $_GET['min_height'],
    'compare' => '>='
  );
 $meta_query[] = array(
    'key' => '_height',
    'value' => $_GET['max_height'],
    'compare' => '<='
  );

Is it possible to achieve the above but with product attributes e.g pa_capacity etc.

I could create custom fields for all products from a selection of product attributes for a work around , but i would rather not do this as it is duplicate of content in attributes and custom fields. Although this would work as it has been tested.

Looks like your on the correct path to the solution. A smiliar question has been answered here.

Looks like WooCommerce works with taxonomies and terms instead of meta_keys. Therefore, your query should look more like this-

$query_args = array(
 'post_type' => 'product',
 'tax_query' => array(
      array(
          'taxonomy' => 'product_type',
          'field'    => 'slug',
          'terms'    => 'custom_type', 
      ),
      array(
          'taxonomy' => 'pa_color',
          'field'    => 'slug',
          'terms'    => 'red', 
      ),
      array(
          'taxonomy' => 'pa_size',
          'field'    => 'slug',
          'terms'    => 'large', 
      ),
  ),
);

You can use PHP/MySQL query to implement the required filtrations

SELECT p.ID AS product_id,
       p.post_title AS product_name,
       t.term_id,
       tt.taxonomy AS attribute_name,
       t.`name` AS value_name,
       t.slug AS vlaue_slug
FROM wp_posts AS p,
     wp_terms AS t,
     wp_term_taxonomy AS tt,
     wp_term_relationships AS tr
WHERE tt.term_id = t.term_id
  AND p.ID = tr.`object_id`
  AND tr.term_taxonomy_id = tt.term_taxonomy_id
  AND tt.taxonomy IN ('pa_height', 'pa_capacity')
  AND ((tt.taxonomy = 'pa_height'
        AND t.`name` >= 10
        AND t.`name` <= 100)
       OR (tt.taxonomy = 'pa_capacity'
           AND t.`name` >= 23))
  AND p.post_type = 'product'
  AND p.post_status = 'publish'
ORDER BY p.post_title;

You can edit ('pa_height', 'pa_capacity') with PHP and also it related values in where clause. The above query will return product_id(s) which can be passed through wc_get_product() and do your stuff.

Hope this helps!