【问题标题】:WooCommerce search for multiple ordersWooCommerce 搜索多个订单
【发布时间】:2020-08-29 11:46:22
【问题描述】:

在这篇文章中获得灵感 - woocommerce admin product search multiple skus 我正在尝试向 WooCommerce 添加对多个订单的搜索 是否可以调整此代码以使其适用于订单而不是产品?

function woo_multiple_order_search( $query_vars ) {

global $typenow;
global $wpdb;
global $pagenow;

if ( 'shop_order' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
    $search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );

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

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

    $meta_query = array(
        'relation' => 'OR'
    );

    if(is_array($order_ids) && $order_ids) {
        foreach($order_ids as $order_id) {
            $meta_query[] = array(
                'key' => '_order_id',
                'value' => $order_id,
                'compare' => '='
            );
        }
    }

    $args = array(
        'posts_per_page'  => -1,
        'post_type'       => 'shop_order',
    'post_status'     => 'publish',
        '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_order_search', 20 );

似乎一直工作到 foreach($posts as $post){

【问题讨论】:

  • 到目前为止你尝试过什么?你放在这里的代码和你分享的url一模一样
  • 代码更改为我使用的代码。
  • 有人吗?有点卡在这个上,我想它应该是一些小的调整,只是无法弄清楚要调整什么才能让它工作。

标签: php wordpress search woocommerce code-snippets


【解决方案1】:

如果您在这部分通过正确的字段名称“_order_number”更改“_order_id”,此代码将完美运行:

        foreach($order_ids as $order_id) {
            $meta_query[] = array(
                'key' => '_order_number', // correct field name
                'value' => $order_id,
                'compare' => '='
            );
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多