Hide a specific action button conditionally in Woocommerce admin Orders list

独自空忆成欢 提交于 2019-12-06 15:17:45

问题


I Would like to add some CSS to the order admin page to hide a custom order action button, but only if the order contains only downloadable products.

This is the function I need to load conditionally:

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>';
}

Is that possible?


回答1:


With CSS it's not be possible.

Instead you can hook in woocommerce_admin_order_actions filter hook, where you will be able to check if all order item are downloadable and then remove the action button "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;
}

Code goes in function.php file of the active child theme (or active theme).

This is untested but should work…

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



来源:https://stackoverflow.com/questions/48399729/hide-a-specific-action-button-conditionally-in-woocommerce-admin-orders-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!