在 Woocommerce 管理订单列表中有条件地隐藏特定操作按钮

Hide a specific action button conditionally in Woocommerce admin Orders list

我想向订单管理页面添加一些 CSS 以隐藏自定义订单操作按钮,但前提是订单仅包含可下载的产品。

这是我需要有条件加载的函数:

add_action( 'admin_head', 'hide_custom_order_status_dispatch_icon' );
function hide_custom_order_status_dispatch_icon() {
    echo '<style>.widefat .column-order_actions a.dispatch { display: none; }</style>';
}

这可能吗?

CSS 不可能

相反,您可以挂钩 woocommerce_admin_order_actions 过滤器挂钩,您可以在其中检查是否所有订单项都可以下载,然后删除操作按钮 "dispatch":

add_filter( 'woocommerce_admin_order_actions', 'custom_admin_order_actions', 900, 2 );
function custom_admin_order_actions( $actions, $the_order ){
    // If button action "dispatch" doesn't exist we exit
    if( ! $actions['dispatch'] ) return $actions;

    // Loop through order items
    foreach( $the_order->get_items() as $item ){
        $product = $item->get_product();
        // Check if any product is not downloadable
        if( ! $product->is_downloadable() )
            return $actions; // Product "not downloadable" Found ==> WE EXIT
    }
    // If there is only downloadable products, We remove "dispatch" action button
    unset($actions['dispatch']);

    return $actions;
}

代码进入活动子主题(或活动主题)的 function.php 文件。

这是未经测试的,但应该可以工作……

You will have to check that 'dispatch' is the correct slug for this action button…