function chaogege_ajax_filter_products() { // 验证 nonce if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'chaogege_filter_nonce')) { wp_send_json_error(array('message' => 'Invalid security token')); return; } $paged = isset($_POST['paged']) ? max(1, intval($_POST['paged'])) : 1; $per_page = isset($_POST['per_page']) ? intval($_POST['per_page']) : 12; $orderby = isset($_POST['orderby']) ? sanitize_text_field($_POST['orderby']) : 'menu_order'; $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $per_page, 'paged' => $paged, 'tax_query' => array('relation' => 'AND'), 'meta_query' => array('relation' => 'AND'), ); // 处理当前产品分类(从 JS 传入) if (isset($_POST['current_category']) && !empty($_POST['current_category'])) { $current_category = sanitize_text_field($_POST['current_category']); $category_parts = explode('/', $current_category); $category_slug = end($category_parts); $args['tax_query'][] = array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $category_slug, 'operator' => 'IN', ); } // 处理属性筛选 foreach ($_POST as $key => $values) { if (strpos($key, 'filter_') === 0 && !empty($values)) { $taxonomy = str_replace('filter_', '', $key); if (taxonomy_exists($taxonomy)) { $args['tax_query'][] = array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => (array)$values, 'operator' => 'IN', ); } } } // 搜索关键词 if (!empty($_POST['search_term'])) { $args['s'] = sanitize_text_field($_POST['search_term']); } // 排序处理 switch ($orderby) { case 'price': $args['meta_key'] = '_price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'ASC'; break; case 'price-desc': $args['meta_key'] = '_price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; break; case 'date': $args['orderby'] = 'date'; $args['order'] = 'DESC'; break; default: $args['orderby'] = 'menu_order title'; $args['order'] = 'ASC'; } // 主查询(分页) $query = new WP_Query($args); ob_start(); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $product = wc_get_product(get_the_ID()); if ($product) { chaogege_product_card_html($product); } } } else { echo '

No products found matching your criteria.

'; } $products_html = ob_get_clean(); wp_reset_postdata(); // 分页 $pagination = ''; if ($query->max_num_pages > 1) { $pagination = chaogege_pagination_html($query->max_num_pages, $paged); } // 结果计数 $total = $query->found_posts; if ($total > 0) { $start = ($paged - 1) * $per_page + 1; $end = min($paged * $per_page, $total); $result_count = sprintf('Showing %d–%d of %d results', $start, $end, $total); } else { $result_count = 'No products found'; } // ================================================================ // 修正:统计筛选器数量 —— 使用无分页的基础参数 // ================================================================ // 准备用于计数的基础参数(移除分页,获取全部匹配产品) $base_count_args = $args; unset($base_count_args['paged']); $base_count_args['posts_per_page'] = -1; // 保证 tax_query 存在且为数组 if (!isset($base_count_args['tax_query']) || !is_array($base_count_args['tax_query'])) { $base_count_args['tax_query'] = array('relation' => 'AND'); } $filter_counts = array(); $filters = chaogege_get_available_filters(); if (!empty($filters)) { foreach ($filters as $key => $filter) { if ($filter['type'] === 'taxonomy' && !empty($filter['terms'])) { $filter_counts[$key] = array(); foreach ($filter['terms'] as $term) { // 每次统计从基础参数复制一份,避免互相影响 $count_args = $base_count_args; // 移除当前属性组自身的筛选条件(避免同组互斥) if (isset($count_args['tax_query']) && is_array($count_args['tax_query'])) { $count_args['tax_query'] = array_filter( $count_args['tax_query'], function($item) use ($key) { return !(isset($item['taxonomy']) && $item['taxonomy'] === $key); } ); // 重新索引 $count_args['tax_query'] = array_values($count_args['tax_query']); } // 添加当前 term 的筛选条件 $count_args['tax_query'][] = array( 'taxonomy' => $key, 'field' => 'slug', 'terms' => array($term->slug), 'operator' => 'IN', ); // 执行计数查询(无分页限制) $count_query = new WP_Query($count_args); $count = $count_query->found_posts; // 存储所有选项的计数(包括 0) $filter_counts[$key][$term->slug] = $count; } } } } wp_send_json_success(array( 'products_html' => $products_html, 'pagination' => $pagination, 'result_count' => $result_count, 'total_count' => $query->found_posts, 'total_pages' => $query->max_num_pages, 'current_page' => $paged, 'filter_counts' => $filter_counts, )); wp_die(); } Products Products – Page 2042 – ovaryresearch.com

All Products

Browse our complete collection of high-quality products for your research needs.

Show per page

Showing 32657–32672 of 45698 results