woocommerce管理产品搜索多个skus [英] woocommerce admin product search multiple skus

查看:71
本文介绍了woocommerce管理产品搜索多个skus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我为一家销售公司工作,该公司刚刚使用带有Woocommerce插件的Wordpress设置了一个网上商店.

So, I work for a sales business that has just setup a webstore using Wordpress with the Woocommerce plugin.

我希望能够进行的操作与保存产品所有信息的产品"标签上的管理员搜索有关.据我了解,普通搜索会对您在框中输入的字词进行mySQL查询.我在functions.php中有一个代码段,以使其也可以搜索元字段_sku.所以当我输入SKU号码说... 7026它将拉出标题,说明或sku中带有7026的所有项目.

What I want to be able to do deals with the admin search on the products tab where all the information for our products is held. From what I understand, the normal search does a mySQL query for the terms you put in the box. I have a code snippet in functions.php to allow it to search the meta field _sku as well. So when i enter a sku number of say... 7026 it will pull up any items with 7026 in its title, description or sku.

但是,我想输入一个....7026 4567 3883进入搜索框,它查询数据库以查找与这些数字中的任何一个都匹配的产品(OR关系搜索,而不是AND)现在,如果我在其中输入内容,我什么也不会得到,因为它会搜索标题,字词说明或sku中具有7026 AND 4567 AND 3883的任何产品,但是我的目标是让搜索返回至少我们产品中的3种产品具有skus 7026、4567和3883的数据库

However, I want to be able to enter say.... 7026 4567 3883 into the search box and it query the database to find any product that matches any of those numbers (an OR relational search instead of AND) right now if i type that in i get nothing because it searches for any product that has 7026 AND 4567 AND 3883 in the title, decription, or sku, but what my goal is to have the search return at the least the 3 products in our database that have the skus 7026, 4567, and 3883

任何帮助将不胜感激.我试图让一段代码运行几天没有运气.

Any help would be greatly appreciated. I have tried to get a code snippet working for days with no luck.

推荐答案

在这篇文章中找到了答案: mutli sku管理员搜索

Found the answer in this post: mutli sku admin search

帖子中的代码来自Bjorn,非常感谢.

The code in the post is from Bjorn, thank you so much.

代码如下:

/**
 * Use multiple sku's to find WOO products in wp-admin
 * NOTE: Use '|' as a sku delimiter in your search query. Example: '1234|1235|1236'
**/
function woo_multiple_sku_search( $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'] ) );

    if (strpos($search_term, '|') == false) return $query_vars;

    $skus = explode('|',$search_term);

    $meta_query = array(
        'relation' => 'OR'
    );
    if(is_array($skus) && $skus) {
        foreach($skus as $sku) {
            $meta_query[] = array(
                'key' => '_sku',
                'value' => $sku,
                'compare' => '='
            );
        }
    }

    $args = array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
        'meta_query'      => $meta_query
    );
    $posts = get_posts( $args );

    if ( ! $posts ) return $query_vars;

    foreach($posts as $post){
      $query_vars['post__in'][] = $post->ID;
    }
}

return $query_vars;
}
add_filter( 'request', 'woo_multiple_sku_search', 20 );

这篇关于woocommerce管理产品搜索多个skus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆