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

修改WooCommerce价格排序规则:价格相同时按名称排序

WooCommerce价格排序优化需求

在WooCommerce中,按价格排序时,价格相同的产品默认会按产品ID排序,而非按名称字母排序,这对用户来说不够直观(产品创建时间仅应适用于「新品」排序)。我希望修改默认价格排序规则,让价格相同时按名称字母排序,但尝试了以下4段代码均未成功,恳请提供解决方案。

尝试1:替换默认价格降序为自定义排序

function replace_default_sorting_option($options) {
    $options['custom_sort'] = 'Custom Sort'; // Replace 'custom_sort' with your key and 'Custom Sort' with your label
    unset($options['price-desc']); // Remove the default price descending option
    return $options;
}
add_filter('woocommerce_catalog_orderby', 'replace_default_sorting_option');

function custom_product_sort($query) {
    if (is_admin() || !$query->is_main_query())
        return;

    if (is_shop() || is_product_category() || is_product()) {
        $orderby = $query->get('orderby');

        if ($orderby === 'custom_sort') {
            $query->set('orderby', 'meta_value_num title'); // Sort by price and title
            $query->set('order', 'DESC ASC'); // Price in descending order, title in ascending order
        }
    }
}
add_action('pre_get_posts', 'custom_product_sort');

尝试2:仅按标题排序(自定义排序选项)

function replace_default_sorting_option($options) {
    $options['custom_sort'] = 'Custom Sort'; // Replace 'custom_sort' with your key and 'Custom Sort' with your label
    unset($options['price-desc']); // Remove the default price descending option
    return $options;
}
add_filter('woocommerce_catalog_orderby', 'replace_default_sorting_option');

function custom_product_sort($query) {
    if (is_admin() || !$query->is_main_query())
        return;

    if (is_shop() || is_product_category() || is_product()) {
        $orderby = $query->get('orderby');

        if ($orderby === 'custom_sort') {
            $query->set('orderby', array('meta_value_num' => 'DESC', 'title' => 'ASC')); // Sort by price (numeric) and then by title
        }
    }
}
add_action('pre_get_posts', 'custom_product_sort');

尝试3:使用meta_query定义排序规则(排序混乱)

function custom_woocommerce_catalog_orderby($orderby) {
    // Add your custom sorting option
    $orderby['custom_sort'] = 'Custom Sorting';
    return $orderby;
}
add_filter('woocommerce_default_catalog_orderby', 'custom_woocommerce_catalog_orderby');
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');

function custom_product_sorting($query) {
    if (is_admin() || !$query->is_main_query())
        return;

    if (is_shop() || is_product_category() || is_product()) {
        $orderby = $query->get('orderby');

        if ($orderby === 'custom_sort') {
            $query->set('meta_query', array(
                'relation' => 'OR',
                'price_clause' => array(
                    'key' => '_price',
                    'compare' => 'EXISTS',
                ),
                'title_clause' => array(
                    'key' => 'post_title',
                    'compare' => 'EXISTS',
                ),
            ));
            $query->set('orderby', array(
                'price_clause' => 'DESC',
                'title_clause' => 'ASC',
            ));
        }
    }
}
add_action('pre_get_posts', 'custom_product_sorting');

尝试4:直接指定price和title排序(仅按标题排序?)

function custom_woocommerce_catalog_orderby($orderby) {
    // Add your custom sorting option
    $orderby['custom_sort'] = 'Custom Sorting';
    return $orderby;
}
add_filter('woocommerce_default_catalog_orderby', 'custom_woocommerce_catalog_orderby');
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');

function custom_product_sorting($query) {
    if (is_admin() || !$query->is_main_query())
        return;

    if (is_shop() || is_product_category() || is_product()) {
        $orderby = $query->get('orderby');

        if ($orderby === 'custom_sort') {
            $query->set('orderby', array(
                'price' => 'DESC', // Sort by price in descending order
                'title' => 'ASC',  // Then sort by title in ascending order
            ));
        }
    }
}
add_action('pre_get_posts', 'custom_product_sorting');

解决方案:修改默认价格排序的二级规则

无需新增自定义排序选项,直接修改WooCommerce默认的「价格升序」和「价格降序」规则,让价格相同时按产品名称字母排序。使用woocommerce_get_catalog_orderby_args过滤器更精准,这是WooCommerce专门用于处理排序参数的钩子:

add_filter( 'woocommerce_get_catalog_orderby_args', 'custom_price_sort_with_title_secondary', 10, 2 );
function custom_price_sort_with_title_secondary( $args, $orderby ) {
    // 针对价格升序排序
    if ( 'price' === $orderby ) {
        $args['orderby'] = array(
            'meta_value_num' => 'ASC', // 价格升序
            'title'          => 'ASC'  // 价格相同时标题升序
        );
        $args['meta_key'] = '_price';
    }
    // 针对价格降序排序
    elseif ( 'price-desc' === $orderby ) {
        $args['orderby'] = array(
            'meta_value_num' => 'DESC', // 价格降序
            'title'          => 'ASC'   // 价格相同时标题升序
        );
        $args['meta_key'] = '_price';
    }
    return $args;
}

代码说明:

  • 直接修改WooCommerce排序参数数组,用户使用原有「价格」和「价格从高到低」选项即可生效,无需额外操作。
  • 指定meta_key为_price,确保按产品价格的数值类型排序,避免字符串排序误差。
  • 当价格相同时,添加title作为二级排序规则,按字母升序排列,符合用户直观需求。
  • 同时兼容价格升序和降序两种场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:05:12