在 WooCommerce 管理订单列表页面中按产品帖子类型过滤订单 [英] Filter orders by product post type in WooCommerce admin orders list page

查看:67
本文介绍了在 WooCommerce 管理订单列表页面中按产品帖子类型过滤订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WooCommerce 的新手,我想自定义管理订单列表页面.所以基本上我创建了新的产品类型.现在我还限制客户可以一次购买我的自定义产品类型的产品或简单产品类型的产品.

I am new to WooCommerce, And I want to customize admin order list page. So basically I created new product type. Now I also made restriction that customer can purchase my custom product type's product or simple product type's product at a time.

现在在管理订单列表页面中,我想显示具有简单产品类型产品的订单.我怎么能做到这一点?

Now in admin order listing page, I want to show order which has simple product type's product. How I can do this?

我搜索了很多,我找到了一个插件,它具有过滤器,可以按产品而不是产品类型显示订单.在研究该插件时,我知道我可以使用 posts_where 操作来做到这一点.但我没有正确的想法.

I searched a lot, I found an plugin which has filter to show order by product not product type. While studying that plugin I got to know that may be I can do this using posts_where action. But I don't have proper idea.

推荐答案

以下功能将在管理订单列表中添加一个额外的过滤下拉列表,允许您根据订单项目产品帖子类型过滤订单:

The following functions will add an additional filter dropdown to admin order list, allowing you to filter orders based on order items product post type:

add_action( 'restrict_manage_posts', 'admin_shop_order_by_product_type_filter' );
function admin_shop_order_by_product_type_filter(){
    global $pagenow, $post_type;

    if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
        $domain     = 'woocommerce';
        $filter_id  = 'filter_product_type';
        $current    = isset($_GET[$filter_id])? $_GET[$filter_id] : '';
        $query_args = ['taxonomy' => 'product_type', 'fields' => 'names', 'orderby' => 'order'];

        echo '<select name="'.$filter_id.'">
        <option value="">' . __('Filter by Product post type', $domain) . '</option>';

        foreach ( get_terms($query_args) as $term_name ) {
            printf( '<option value="%s"%s>%s</option>', $term_name,
                $term_name === $current ? '" selected="selected"' : '', ucfirst($term_name) );
        }
        echo '</select>';
    }
}

add_action( 'pre_get_posts', 'process_admin_shop_order_product_type_filter' );
function process_admin_shop_order_product_type_filter( $query ) {
    global $pagenow, $post_type, $wpdb;

    $filter_id = 'filter_product_type';

    if ( $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $post_type
         && isset( $_GET[$filter_id] ) && $_GET[$filter_id] != '' ) {

        $order_ids = $wpdb->get_col( "
            SELECT DISTINCT o.ID
            FROM {$wpdb->prefix}posts o
            INNER JOIN {$wpdb->prefix}woocommerce_order_items oi
                ON oi.order_id = o.ID
            INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim
                ON oi.order_item_id = oim.order_item_id
            INNER JOIN {$wpdb->prefix}term_relationships tr
                ON oim.meta_value = tr.object_id
            INNER JOIN {$wpdb->prefix}term_taxonomy tt
                ON tr.term_taxonomy_id = tt.term_taxonomy_id
            INNER JOIN {$wpdb->prefix}terms t
                ON tt.term_id = t.term_id
            WHERE o.post_type = '$post_type'
            AND oim.meta_key = '_product_id'
            AND tt.taxonomy = 'product_type'
            AND t.name = '{$_GET[$filter_id]}'
        ");

        $query->set( 'post__in', $order_ids ); // Set the new "meta query"

        $query->set( 'posts_per_page', 25 ); // Set "posts per page"

        $query->set( 'paged', ( get_query_var('paged') ? get_query_var('paged') : 1 ) ); // Set "paged"
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经过测试和工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

这篇关于在 WooCommerce 管理订单列表页面中按产品帖子类型过滤订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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