首页 文章

Woocommerce - 产品属性查询

提问于
浏览
0

有没有人知道按产品属性的范围查询类别存档页面产品的方法 . 以下是有效的,但这是因为这是产品的自定义字段,与产品属性相关联,存储为分类 .

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

是否有可能实现上述目标,但具有产品属性,例如pa_capacity等 .

我可以从一系列产品属性中为所有产品创建自定义字段,但我宁愿不这样做,因为它与属性和自定义字段中的内容重复 . 虽然这可以用,因为它已经过测试 .

2 回答

  • 0

    看起来你正确的解决方案的路径 . 一个熟悉的问题已被回答here .

    看起来WooCommerce使用分类法和术语而不是meta_keys . 因此,您的查询应该更像这样 -

    $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', 
          ),
      ),
    );
    
  • 0

    您可以使用PHP / MySQL查询来实现所需的过滤

    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;
    

    您可以使用PHP编辑 ('pa_height', 'pa_capacity') ,也可以在where子句中编辑相关值 . 上面的查询将返回product_id(s),可以通过 wc_get_product() 传递并执行您的操作 .

    希望这可以帮助!

相关问题