WooCommerce 使用 ACF 和产品类别过滤产品

发布于 2025-01-18 01:26:31 字数 3572 浏览 0 评论 0原文

我正在使用WooCommerce显示产品循环,并为同一循环构建了过滤器。该过滤器使用ACF元数据,该值直接从查询字符串中拉出。 meta_query正在按预期工作,但是Tax_Query似乎并不无效。我需要一些帮助,说明为什么我的tax_query没有产生正确的结果。

function custom_pre_get_posts_query( $q ) {

  $selected_seats = array();
  if (isset($_GET['seats']) && $_GET['seats'] != 'all') {
    $selected_seats = $_GET['seats'];
  }  

  $selected_event_type = array();
  if (isset($_GET['event_type']) && $_GET['event_type'] != 'event_all') {
    $selected_event_type = $_GET['event_type'];
  }
  
  $selected_location = array();
  if (isset($_GET['location']) && $_GET['location'] != 'location_all') {
    $selected_location = $_GET['location'];
  }
  
  $tax_query = (array) $q->get( 'meta_query' );
  $tax_query[] = array(
        'tax_query' => array(
          'taxonomy' => 'product_category',
          'field'     => 'slug',
          'operator' => 'IN',
          'terms' => $selected_location 
          //terms' => array( $selected_location )
        ),
        'meta_query'    => array(
          'relation'        => 'AND',
          // Select in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'seats',
            'value'     => $selected_seats,
            'compare'   => 'IN',
          ),
          // Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'event_type',
            'value'     => $selected_event_type,
            'compare'   => 'LIKE',
          ),
          array(
            'key'       => 'car_categories',
            'value'     => $selected_category,
            'compare'   => 'LIKE',
          ),
        ),
        
  );

  // Special query for category
  $selected_category = array();
  if (isset($_GET['category'])) {
    $selected_category = $_GET['category'];
  }  
  $selected_category_query= array(
    'relation'      => 'OR',
  );
  foreach($selected_category as $category){
    array_push($selected_category_query,array(
      'key'     => 'car_categories',
      'value'       =>$category,
      'compare'     => 'LIKE',
    ));
  }
  $tax_query[] = array(
    'meta_query'    => $selected_category_query,
  );
  $q->set( 'meta_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );  

$ tax_query的var_dump导致:

array(2) {
  [0]=>
  array(2) {
    ["tax_query"]=>
    array(4) {
      ["taxonomy"]=>
      string(11) "product_cat"
      ["field"]=>
      string(2) "id"
      ["terms"]=>
      array(1) {
        [0]=>
        int(75)
      }
      ["operator"]=>
      string(2) "IN"
    }
    ["meta_query"]=>
    array(4) {
      ["relation"]=>
      string(3) "AND"
      [0]=>
      array(3) {
        ["key"]=>
        string(5) "seats"
        ["value"]=>
        array(0) {
        }
        ["compare"]=>
        string(2) "IN"
      }
      [1]=>
      array(3) {
        ["key"]=>
        string(10) "event_type"
        ["value"]=>
        array(0) {
        }
        ["compare"]=>
        string(4) "LIKE"
      }
      [2]=>
      array(3) {
        ["key"]=>
        string(14) "car_categories"
        ["value"]=>
        NULL
        ["compare"]=>
        string(4) "LIKE"
      }
    }
  }
  [1]=>
  array(1) {
    ["meta_query"]=>
    array(1) {
      ["relation"]=>
      string(2) "OR"
    }
  }
}

I am using WooCommerce to display a product loop and have built out a filter for the same loop. The filter uses acf meta which the values are pulled directly from the query string.
The meta_query is working as expected, however the tax_query seems to be not ineffective. I need some assistance as to why my tax_query is not producing the correct results.

function custom_pre_get_posts_query( $q ) {

  $selected_seats = array();
  if (isset($_GET['seats']) && $_GET['seats'] != 'all') {
    $selected_seats = $_GET['seats'];
  }  

  $selected_event_type = array();
  if (isset($_GET['event_type']) && $_GET['event_type'] != 'event_all') {
    $selected_event_type = $_GET['event_type'];
  }
  
  $selected_location = array();
  if (isset($_GET['location']) && $_GET['location'] != 'location_all') {
    $selected_location = $_GET['location'];
  }
  
  $tax_query = (array) $q->get( 'meta_query' );
  $tax_query[] = array(
        'tax_query' => array(
          'taxonomy' => 'product_category',
          'field'     => 'slug',
          'operator' => 'IN',
          'terms' => $selected_location 
          //terms' => array( $selected_location )
        ),
        'meta_query'    => array(
          'relation'        => 'AND',
          // Select in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'seats',
            'value'     => $selected_seats,
            'compare'   => 'IN',
          ),
          // Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'event_type',
            'value'     => $selected_event_type,
            'compare'   => 'LIKE',
          ),
          array(
            'key'       => 'car_categories',
            'value'     => $selected_category,
            'compare'   => 'LIKE',
          ),
        ),
        
  );

  // Special query for category
  $selected_category = array();
  if (isset($_GET['category'])) {
    $selected_category = $_GET['category'];
  }  
  $selected_category_query= array(
    'relation'      => 'OR',
  );
  foreach($selected_category as $category){
    array_push($selected_category_query,array(
      'key'     => 'car_categories',
      'value'       =>$category,
      'compare'     => 'LIKE',
    ));
  }
  $tax_query[] = array(
    'meta_query'    => $selected_category_query,
  );
  $q->set( 'meta_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );  

a var_dump of $tax_query results in:

array(2) {
  [0]=>
  array(2) {
    ["tax_query"]=>
    array(4) {
      ["taxonomy"]=>
      string(11) "product_cat"
      ["field"]=>
      string(2) "id"
      ["terms"]=>
      array(1) {
        [0]=>
        int(75)
      }
      ["operator"]=>
      string(2) "IN"
    }
    ["meta_query"]=>
    array(4) {
      ["relation"]=>
      string(3) "AND"
      [0]=>
      array(3) {
        ["key"]=>
        string(5) "seats"
        ["value"]=>
        array(0) {
        }
        ["compare"]=>
        string(2) "IN"
      }
      [1]=>
      array(3) {
        ["key"]=>
        string(10) "event_type"
        ["value"]=>
        array(0) {
        }
        ["compare"]=>
        string(4) "LIKE"
      }
      [2]=>
      array(3) {
        ["key"]=>
        string(14) "car_categories"
        ["value"]=>
        NULL
        ["compare"]=>
        string(4) "LIKE"
      }
    }
  }
  [1]=>
  array(1) {
    ["meta_query"]=>
    array(1) {
      ["relation"]=>
      string(2) "OR"
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2025-01-25 01:26:31

我想在这里尽可能清楚地说明:

您将在不同的键中设置 $meta_query_args 和 $tax_query_args :

您的 meta_query 将设置为:

$meta_query_args = array(
     'relation'        => 'AND',
          // Select in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'seats',
            'value'     => $selected_seats,
            'compare'   => 'IN',
          ),
          // Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'event_type',
            'value'     => $selected_event_type,
            'compare'   => 'LIKE',
          ),
          array(
            'key'       => 'car_categories',
            'value'     => $selected_category,
            'compare'   => 'LIKE',
          ),
  );
$q->set( 'meta_query', $meta_query_args );

您的tax_query args将设置为:(根据您的需要进行相应修改。)

$tax_query_args = array(
     'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'action', 'comedy' ),
        ),
        //optionally if you are trying to query more than one taxonomy
        array(
            'taxonomy' => 'custom_tax_whatever',
            'field'    => 'term_id',
            'terms'    => array( 103, 115, 206 ),
        ),
  );
$q->set( 'tax_query', $tax_query_args );

您的最终结果对于meta_query 和tax_query 键,查询应该或多或少像这样:

array(
  'tax_query' => array(
     'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'action', 'comedy' ),
        ),
        //optionally if you are trying to query more than one taxonomy
        array(
            'taxonomy' => 'custom_tax_whatever',
            'field'    => 'term_id',
            'terms'    => array( 103, 115, 206 ),
        ),
  ),
  'meta_query' => array(
     'relation'        => 'AND',
          // Select in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'seats',
            'value'     => $selected_seats,
            'compare'   => 'IN',
          ),
          // Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'event_type',
            'value'     => $selected_event_type,
            'compare'   => 'LIKE',
          ),
          array(
            'key'       => 'car_categories',
            'value'     => $selected_category,
            'compare'   => 'LIKE',
          ),
  )
);

PD:我还没有测试过它,但应该适合您想要实现的目标。

I wanna try to be clear as possible here:

You are gonna set your $meta_query_args and your $tax_query_args in different keys:

Your meta_query will be set like:

$meta_query_args = array(
     'relation'        => 'AND',
          // Select in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'seats',
            'value'     => $selected_seats,
            'compare'   => 'IN',
          ),
          // Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'event_type',
            'value'     => $selected_event_type,
            'compare'   => 'LIKE',
          ),
          array(
            'key'       => 'car_categories',
            'value'     => $selected_category,
            'compare'   => 'LIKE',
          ),
  );
$q->set( 'meta_query', $meta_query_args );

Your tax_query args will be set like: (Modify accordingly to your needs.)

$tax_query_args = array(
     'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'action', 'comedy' ),
        ),
        //optionally if you are trying to query more than one taxonomy
        array(
            'taxonomy' => 'custom_tax_whatever',
            'field'    => 'term_id',
            'terms'    => array( 103, 115, 206 ),
        ),
  );
$q->set( 'tax_query', $tax_query_args );

your final query should look like this more or less for the meta_query and tax_query keys:

array(
  'tax_query' => array(
     'relation' => 'AND', // AND , OR (Do not use with a single inner taxonomy array)
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'action', 'comedy' ),
        ),
        //optionally if you are trying to query more than one taxonomy
        array(
            'taxonomy' => 'custom_tax_whatever',
            'field'    => 'term_id',
            'terms'    => array( 103, 115, 206 ),
        ),
  ),
  'meta_query' => array(
     'relation'        => 'AND',
          // Select in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'seats',
            'value'     => $selected_seats,
            'compare'   => 'IN',
          ),
          // Checkbox in ACF, doesn't need to do array cause only one filter will apply at one time
          array(
            'key'       => 'event_type',
            'value'     => $selected_event_type,
            'compare'   => 'LIKE',
          ),
          array(
            'key'       => 'car_categories',
            'value'     => $selected_category,
            'compare'   => 'LIKE',
          ),
  )
);

PD: I have not tested it, but should be good for what you want to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文