• Resolved evertech

    (@evertech)


    Hi,

    I would like to know if there’s a hook to alter the product table in the admin panel. My goal is to add a specific class to the <tr> of the table when a product is out of stock, so that I can alter the style of said rows giving visual feedback regarding the stock status of the item (in case you’re wondering, Manage Stock is set to off and Hide Out Of Stock Items is on).
    Or, as an alternative, is there a way to have the out of stock labels / in stock labels visible in the admin panel without having to activate the Manage Stock option?

    Thanks
    Roberto Pravisani

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Rodrigo Primo

    (@rodrigosprimo)

    Hi @evertech,

    I haven’t tested, but I believe you could use the ‘post_class’ filter (https://developer.www.ads-software.com/reference/hooks/post_class/) to add more classes to the product post_type. This filter is called inside get_post_class() which is used by WordPress when generating the <tr>s: https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-posts-list-table.php#L1301.

    I hope this helps.

    Thread Starter evertech

    (@evertech)

    Thank Rodrigo, that did it!
    In case anyone needs to did something similar, here’s the code I used:

    add_filter('post_class', 'set_row_post_class', 10,3);
    function set_row_post_class($classes, $class, $post_id){
        
        // Make sure you're in the admin panel
        if (!is_admin()) {  
            return $classes;
        }
        
        // limit action to product table
        $screen = get_current_screen(); 
        if ('product' != $screen->post_type && 'edit' != $screen->base) {
            return $classes;
        }
        
        // Get stock status of the product 
        $stock = get_post_meta( $post_id, '_stock_status', true );
        
        // Add a class to the <tr> with the stock status
        $classes[] = "stock-status-".$stock;
     
        // Return the array with the classes
        return $classes;
    }
    

    Cheers
    Roberto

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Highlight out of stock products in admin panel’ is closed to new replies.