根据 WooCommerce 编辑订单页面中的订单状态在下拉菜单中隐藏订单状态 [英] Hide order statuses in dropdown based on order status in WooCommerce edit order page

查看:74
本文介绍了根据 WooCommerce 编辑订单页面中的订单状态在下拉菜单中隐藏订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定场景下隐藏 WooCommerce 订单状态下拉菜单中的订单状态:

I want to hide order statuses in the WooCommerce order status dropdown under specific scenarios:

  • 如果状态是待付款隐藏完成
  • 如果状态正在处理隐藏待付款

我仍然想在订单概览列表中显示所有这些订单状态.

I still want to display all these order statuses in the order overview list.

我所能找到的就是完全取消订单状态:

All I can find is to unset an order status completely:

function so_39252649_remove_processing_status ($statuses) {
    if (isset($statuses['wc-processing'])) {
        unset($statuses['wc-processing']);
    }
    return $statuses;
}
add_filter('wc_order_statuses', 'so_39252649_remove_processing_status');


但这当然也会将其从订单概览列表中删除,我只是想将其隐藏在订单编辑页面的下拉列表中,但我找不到相关的钩子.


But this will of course also remove it from the order overview list, I just want to hide it in the dropdown on the order edit page, but I cant find a hook for this.

jQuery 是我唯一的选择吗?

Is jQuery my only choice for this?

推荐答案

您可以使用以下注释,在代码中添加说明.

You can use the following, comments with explanations added in the code.

所以你得到:

// Admin order edit page: order status dropdown
function filter_wc_order_statuses( $order_statuses ) {  
    global $post, $pagenow;

    // Target edit pages
    if( $pagenow === 'post.php' && isset($_GET['post']) && $_GET['action'] == 'edit' && get_post_type($_GET['post']) === 'shop_order' ) {
        // Get ID
        $order_id = $post->ID;

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // Is a WC order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Get current order status
            $order_status = $order->get_status();
            
            // Compare
            if ( $order_status == 'pending' ) {
                unset( $order_statuses['wc-completed'] );
            } elseif ( $order_status == 'processing' ) {
                unset( $order_statuses['wc-pending'] );             
            } 
        }
    }
    
    return $order_statuses;
}
add_filter( 'wc_order_statuses', 'filter_wc_order_statuses', 10, 1 );

这篇关于根据 WooCommerce 编辑订单页面中的订单状态在下拉菜单中隐藏订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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