Search code examples
phpcsswordpresswoocommerceorders

Hide a specific action button conditionally in Woocommerce admin Orders list


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?


Solution

  • 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…