【问题标题】:Woocommerce Admin Product Search Not workingWoocommerce 管理员产品搜索不起作用
【发布时间】:2019-04-27 05:47:11
【问题描述】:

我遇到了管理员中的 woocommerce 产品搜索不起作用并给出结果为 No product found 的问题,尽管我的搜索输入有很多产品。

另外我还没有使用relevancy search插件所以不会有任何问题。

我在wp-congig.php 中将调试设置为 true,并在打印整个查询时抛出一个与 wp_meta 相关的错误。

还尝试禁用主题并尝试使用默认主题还尝试逐个禁用每个插件,但仍然没有运气。

【问题讨论】:

    标签: wordpress search woocommerce


    【解决方案1】:

    我遇到了同样的事情,并将以下代码块添加到我的 functions.php 文件中立即修复了它:

    function m_request_query( $query_vars ) {
        global $typenow;
        global $wpdb;
        global $pagenow;
    
        if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
            $search_term  = esc_sql( sanitize_text_field( $_GET['s'] ) );
        // Split the search term by comma.
            $search_terms = explode( ',', $search_term );
        // If there are more terms make sure we also search for the whole thing, maybe it's not a list of terms.
            if ( count( $search_terms ) > 1 ) {
                $search_terms[] = $search_term;
            }
        // Cleanup the array manually to avoid issues with quote escaping.
            array_walk( $search_terms, 'trim' );
            array_walk( $search_terms, 'esc_sql' );
            $meta_key               = '_sku';
            $post_types             = array( 'product', 'product_variation' );
            $query                  = "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id WHERE postmeta.meta_key = '{$meta_key}' AND postmeta.meta_value IN  ('" . implode( "','", $search_terms ) . "') AND posts.post_type IN ('" . implode( "','", $post_types ) . "') ORDER BY posts.post_parent ASC, posts.post_title ASC";
            $search_results         = $wpdb->get_results( $query );
            $product_ids            = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
            $query_vars['post__in'] = array_merge( $product_ids, $query_vars['post__in'] );
        }
    
        return $query_vars;
    }
    add_filter( 'request', 'm_request_query', 20 );
    

    向mircian 大声疾呼,为sn-p。

    【讨论】:

    • 增加了能够搜索以逗号分隔的多个 SKU 的好处(不包括空格,只包括逗号)。示例:12345,ABCDEF,othersku
    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2016-08-07
    • 2018-03-03
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多