【问题标题】:Hide a specific action button conditionally in Woocommerce admin Orders list在 Woocommerce 管理订单列表中有条件地隐藏特定操作按钮
【发布时间】:2018-07-02 03:43:46
【问题描述】:

我想在订单管理页面添加一些 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>';
}

这可能吗?

【问题讨论】:

    标签: php css wordpress woocommerce orders


    【解决方案1】:

    使用 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 文件中。

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

    您必须检查 'dispatch' 是否是此操作按钮的正确 slug...

    【讨论】:

    • 感谢您在这里帮助我,它运行良好。
    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2021-09-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多