All Products
Browse our complete collection of high-quality products for your research needs.
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.
Browse our complete collection of high-quality products for your research needs.