Can't find a product by SKU

时间:2019-06-21 作者:Shipso

我对管理面板产品搜索有一些问题。某些类别中的产品无法按SKU找到,只能按名称找到,而其他类别可以。我试着用SKU:, 但这并没有改变任何事情。WordPress v5.1.1,WooCommerce v3.6.4

1 个回复
SO网友:Rohit Verma

Add This Code In functions.php then save it.

更改后,您可以在屏幕截图中看到更改

enter image description here

enter image description here

function search_by_sku_for_admin( $search, &$query_vars ) {
    global $wpdb, $pagenow;

    if ( \'edit.php\' != $pagenow || empty($search) ) {
        return $search;
    }
    
    $args = array(
        \'posts_per_page\'  => -1,
        \'post_type\'       => \'product\',
        \'meta_query\' => array(
            array(
                \'key\' => \'_sku\',
                \'value\' => $query_vars->query[\'s\'],
                \'compare\' => \'LIKE\'
            )
        )
    );
    $posts = get_posts( $args );
    if ( empty( $posts ) ) return $search;
    $get_post_ids = array();
    foreach($posts as $post){
        $get_post_ids[] = $post->ID;
    }
    if ( sizeof( $get_post_ids ) > 0 ) {
        $search = str_replace( \'AND (((\', "AND ((({$wpdb->posts}.ID IN (" . implode( \',\', $get_post_ids ) . ")) OR (", $search);
    }
    return $search;
}
add_filter( \'posts_search\', \'search_by_sku_for_admin\', 999, 2 );

相关推荐

Search function not working

我正在为我的woocommerce商店使用SSHOP主题。当我从主页搜索任何内容时,搜索url工作正常,下面的搜索permalink显示。https://techcart.pk/?s=abc但当我从产品页面或分类页面搜索时,搜索结果并没有找到任何内容,下面的permalink显示。https://techcart.pk/product/8mm-led-10pcs/?s=abc我想重写上面的永久链接,如下所示https://techcart.pk/?s=abc请帮忙