You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过URL参数编程实现WooCommerce商店页面AJAX加载下一页

无需插件的WooCommerce URL参数转产品查询方案

实现思路

直接解析URL中的查询参数,映射为WP_Query或wc_get_products支持的原生查询字段即可,无需额外安装插件,代码可直接放入主题functions.php文件或自定义功能插件中使用。

注:如果使用PHP8以下版本,将代码中的str_starts_with($key, 'filter_')替换为substr($key, 0, 7) === 'filter_'即可兼容。

版本1:WP_Query 实现(推荐用于前端产品循环)

// 解析URL参数生成产品查询参数
function get_wc_product_query_args_from_url() {
    $args = [
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 12, // 可自行修改每页展示数量
        'tax_query' => [],
        'meta_query' => [],
    ];
    // 处理排序参数
    if (isset($_GET['orderby'])) {
        $orderby_val = sanitize_text_field($_GET['orderby']);
        $orderby_arr = explode('-', $orderby_val);
        if (count($orderby_arr) === 2) {
            $args['orderby'] = $orderby_arr[0];
            $args['order'] = strtoupper($orderby_arr[1]);
        } else {
            $args['orderby'] = $orderby_val;
            $args['order'] = 'DESC';
        }
    }
    // 处理属性筛选参数
    $all_get_params = $_GET;
    foreach ($all_get_params as $key => $value) {
        if (str_starts_with($key, 'filter_')) {
            $attr_slug = substr($key, 7);
            $taxonomy = 'pa_' . $attr_slug;
            if (!taxonomy_exists($taxonomy)) continue;
            $attr_values = array_map('sanitize_text_field', explode(',', $value));
            // 读取对应属性的查询逻辑(AND/OR)
            $operator = 'IN';
            if (isset($all_get_params["query_type_{$attr_slug}"])) {
                $query_type = strtoupper(sanitize_text_field($all_get_params["query_type_{$attr_slug}"]));
                $operator = $query_type === 'AND' ? 'AND' : 'IN';
            }
            $args['tax_query'][] = [
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $attr_values,
                'operator' => $operator,
            ];
        }
    }
    // 处理价格区间参数
    if (isset($_GET['min_price']) && is_numeric($_GET['min_price'])) {
        $args['meta_query'][] = [
            'key' => '_price',
            'value' => floatval($_GET['min_price']),
            'compare' => '>=',
            'type' => 'DECIMAL',
        ];
    }
    if (isset($_GET['max_price']) && is_numeric($_GET['max_price'])) {
        $args['meta_query'][] = [
            'key' => '_price',
            'value' => floatval($_GET['max_price']),
            'compare' => '<=',
            'type' => 'DECIMAL',
        ];
    }
    // 如需支持产品分类、标签、库存筛选可在此处扩展逻辑
    return $args;
}

// 前端模板调用示例
$product_query = new WP_Query(get_wc_product_query_args_from_url());
if ($product_query->have_posts()) {
    while ($product_query->have_posts()) {
        $product_query->the_post();
        wc_get_template_part('content', 'product');
    }
    woocommerce_pagination();
    wp_reset_postdata();
} else {
    wc_get_template('loop/no-products-found.php');
}

版本2:wc_get_products 实现(推荐用于后台/接口数据查询)

function get_wc_products_from_url() {
    $args = [
        'status' => 'publish',
        'limit' => 12,
        'orderby' => 'date',
        'order' => 'DESC',
        'meta_query' => [],
        'tax_query' => [],
    ];
    // 处理排序
    if (isset($_GET['orderby'])) {
        $orderby_val = sanitize_text_field($_GET['orderby']);
        $orderby_arr = explode('-', $orderby_val);
        if (count($orderby_arr) === 2) {
            $args['orderby'] = $orderby_arr[0];
            $args['order'] = strtoupper($orderby_arr[1]);
        }
    }
    // 处理属性筛选
    $all_get_params = $_GET;
    foreach ($all_get_params as $key => $value) {
        if (str_starts_with($key, 'filter_')) {
            $attr_slug = substr($key, 7);
            $taxonomy = 'pa_' . $attr_slug;
            if (!taxonomy_exists($taxonomy)) continue;
            $attr_values = array_map('sanitize_text_field', explode(',', $value));
            $operator = 'IN';
            if (isset($all_get_params["query_type_{$attr_slug}"])) {
                $query_type = strtoupper(sanitize_text_field($all_get_params["query_type_{$attr_slug}"]));
                $operator = $query_type === 'AND' ? 'AND' : 'IN';
            }
            $args['tax_query'][] = [
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $attr_values,
                'operator' => $operator,
            ];
        }
    }
    // 处理价格区间
    if (isset($_GET['min_price']) && is_numeric($_GET['min_price'])) {
        $args['meta_query'][] = [
            'key' => '_price',
            'value' => floatval($_GET['min_price']),
            'compare' => '>=',
            'type' => 'DECIMAL',
        ];
    }
    if (isset($_GET['max_price']) && is_numeric($_GET['max_price'])) {
        $args['meta_query'][] = [
            'key' => '_price',
            'value' => floatval($_GET['max_price']),
            'compare' => '<=',
            'type' => 'DECIMAL',
        ];
    }
    return wc_get_products($args);
}

支持的筛选参数说明

  • 排序规则:支持WooCommerce原生所有排序字段,包括date(日期)、price(价格)、popularity(销量)、rating(评分)等,order支持ASC(升序)、DESC(降序)
  • 属性筛选:支持所有自定义产品属性,filter_{属性别名}传入多个属性值用逗号分隔,query_type_{属性别名}指定查询逻辑为OR/AND
  • 价格区间:通过min_price和max_price指定价格上下限,支持浮点数
  • 可扩展支持产品分类、产品标签、库存状态、是否在售等其他常用WooCommerce筛选参数

内容的提问来源于stack exchange,提问作者quarky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 18:36:04