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

实现仅在分类与搜索结果页优先显示WooCommerce特色产品

解决WooCommerce“Recommended”排序仅在分类/搜索页面生效的问题

要让“Recommended”排序选项仅在产品分类页面、产品标签页面及搜索结果页面显示,同时特色产品的排序逻辑也仅在这些页面触发,需要对现有代码添加页面条件判断,具体修改如下:

修改后的完整代码

// 添加新排序选项(仅在目标页面显示)
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    // 仅在产品分类、产品标签或搜索结果页添加该排序选项
    if ( is_product_category() || is_product_tag() || is_search() ) {
        $sortby['recommended'] = 'Recommended';
    }
    return $sortby;
}

// 为新排序选项设置参数(仅在目标页面生效)
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    
    // 仅在目标页面且选中"recommended"排序时修改参数
    if ( (is_product_category() || is_product_tag() || is_search()) && 'recommended' == $orderby_value ) {
        $args['orderby'] = 'date';
        $args['order'] = 'DESC';
        $args['meta_key'] = '';
    }
    return $args;
}

// 调整特色产品排序逻辑(仅在目标页面生效)
add_filter('posts_orderby', 'show_featured_products_orderby', 10, 2);
function show_featured_products_orderby($order_by, $query){
    global $wpdb;
    // 仅在前台目标页面执行逻辑
    if( !is_admin() && (is_product_category() || is_product_tag() || is_search()) ){
        $orderby_value = ( isset( $_GET['orderby'] ) ? wc_clean( (string) $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ) );
        $orderby_value_array = explode( '-', $orderby_value );
        $orderby = esc_attr( $orderby_value_array[0] );
        $order = ( !empty($orderby_value_array[1]) ? $orderby_value_array[1] : 'ASC' );
        $feture_product_id = wc_get_featured_product_ids();
        
        // 仅在选中"recommended"排序且有特色产品时调整排序
        if ( $orderby == "recommended" && is_array( $feture_product_id ) && !empty($feture_product_id) ) {
            if ( empty($order_by) ) {
                $order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC ";
            } else {
                $order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC, " . $order_by;
            }
        }  
    }
    return $order_by;
}

关键改动说明

  • 限制排序选项显示:在添加排序选项的函数中,通过is_product_category()(产品分类页)、is_product_tag()(产品标签页)、is_search()(搜索结果页)三个条件判断,仅在目标页面添加“Recommended”排序选项。
  • 限制排序参数生效范围:在设置排序参数的函数中,同样添加页面条件判断,确保只有在目标页面且选中该排序选项时,才修改查询参数。
  • 限制特色产品排序逻辑:在调整产品排序的函数中,除了排除后台页面,额外添加目标页面判断,避免排序逻辑在店铺首页等其他页面触发。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 02:34:58