在 woocommerce 管理端搜索多个 sku [英] search multiple sku's on woocommerce admin side

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

问题描述

这听起来很简单,但是如果您有数百种产品并且需要在 woocommerce 管理端搜索多个 sku,则不可用.假设您需要验证 600 个产品,您必须:手动添加一个 sku 到搜索栏点击搜索得到结果重新开始不能用 、空格或破折号分隔.

我搜索过,没有人有答案,或者他们提出的问题没有得到解答.人们如何在 woocommerce 管理端搜索多个 sku 或产品名称?

解决方案

首先.. 你提到你必须验证许多产品 (600),需要多个 sku 搜索.在我看来,您要手动执行此操作?我建议在 PHP 中创建一个产品循环,您可以在其中进行验证.这可能会为您节省很多时间,而且您可以重复使用它.

<小时>

现在问题来了……使用多个 sku 搜索 woo 产品.

我同意互联网上的许多信息都具有误导性.我以前这样做过,我总是使用 pre_get_posts 钩子在运行之前更改 WP(搜索)查询.

经过测试,似乎没有将查询设置为搜索,并且搜索值为空...

因此 woocommerce 必须进行自定义搜索查询.幸运的是,我很快找到了 mircian 的帖子.>

WooCommerce 为其帖子类型(产品、订单等)使用数据存储,并且搜索也是使用自定义函数完成的.在这种情况下,它称为search_products",它执行自定义查询,该查询基本上返回用于结果的 id 数组.

我修改了他的功能以搜索多个 sku.在您的(子)主题的functions.php 中插入该函数.

使用|"作为 SKU 分隔符.示例:'1234|1235|1236'

测试对象:

  • WordPress 4.9.6
  • Woocommerce 3.3.5
<小时>

/*** 使用多个 sku 在 wp-admin 中查找 WOO 产品* 注意:使用|"作为搜索查询中的 sku 分隔符.示例:'1234|1235|1236'**/函数 woo_multiple_sku_search( $query_vars ) {全球 $typenow;全球 $wpdb;全球 $pagenow;if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {$search_term = esc_sql(sanitize_text_field($_GET['s']));如果 (strpos($search_term, '|') == false) 返回 $query_vars;$skus = expand('|',$search_term);$meta_query = 数组('关系' =>'或者');if(is_array($skus) && $skus) {foreach($skus 作为 $sku) {$meta_query[] = 数组('键' =>'_sku','价值' =>$sku,'比较' =>'=');}}$args = 数组('posts_per_page' =>-1,'post_type' =>'产品','meta_query' =>$meta_query);$posts = get_posts( $args );if ( ! $posts ) 返回 $query_vars;foreach($posts as $post){$query_vars['post__in'][] = $post->ID;}}返回 $query_vars;}add_filter( '请求', 'woo_multiple_sku_search', 20 );

This sounds simple but if you have hundreds of products and need to search multiple sku's on woocommerce admin side is not available. Lets say you need to verify 600 products, you have to: manually add one sku to search bar click search get results start again you can't separate by , or by space, or dash.

I searched and no one have an answer or they are questions made are not answered. How can people search multiple sku's, or products names on woocommerce admin side?

解决方案

First.. you mention you have to verify many products (600), needing a multiple sku search. It looks to me you're going to do this manually? I recommend creating a product loop in PHP where you do your verify stuff. This is probably gonna save you alot off time, and you can re-use it.


Now to the problem... Search woo products using multiple sku's.

I agree many info on the internet is kinda misleading. I've done this before, and i always used the pre_get_posts hook to change the WP (search) query before it runs.

After testing it appears the query is not being setup as a search, and the search value is empty...

So woocommerce has to do a custom search query. Luckily i quickly found mircian's post.

WooCommerce uses a Data Store for its post types ( products, orders, etc ) and the search is also done using a custom function. In this case it’s called ‘search_products’ and it does a custom query which basically returns an array of ids to be used for the results.

I modified his function to search with multiple sku's. Insert the function in your (child) theme's functions.php.

Use '|' as a SKU delimiter. Example: '1234|1235|1236'

Tested on:

  • Wordpress 4.9.6
  • Woocommerce 3.3.5

/**
 * 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 管理端搜索多个 sku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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