Lately, I was modifying a WordPress website for a client and I had to find a way to display the stock status of every variation of a variable product in WooCommerce. It was the first time I messed up with WooCommerce’s actions and filters. Here is the solution, without using a plugin. All code goes in functions.php file.

Filter to remove/add columns in products page

<?php
add_filter('manage_edit-product_columns', 'rearrange_columns', 15);
function rearrange_columns($columns){

   // Unsetting = removing the column with a specific ID (e.g. 'product_tag')
   unset( $columns['product_tag'] );

   // We add a column with ID 'var_stock_status'
   $columns['var_stock_status'] = __( 'Stock Number of Product\'s Variations');

   return $columns;

}

Action to actually display our variable stock status

<?php
add_action( 'manage_product_posts_custom_column', 'variable_stock_status', 10, 2 );

function variable_stock_status( $column, $postid ) {

   // We run this function only on our custom column
   if ( $column == 'var_stock_status' ) {

      // We get the product object info
      global $product;

      // We continue only if the previous product object is a variable
      if ($product->is_type('variable') ) {

         // We get an array of all available variations
         $avail_vari = $product->get_available_variations();
         echo '<div class="var_stock_box">';

         // Start a loop to get and format array's data
         foreach ($avail_vari as $key => $value) {

            // Store variation's ID to a variable
            $vari_id = $value['variation_id'];

            // Use this if you want to display the NAME of the filter
            // Change this to your desired filter using pa_ (e.g. "pa_age", "pa_source" etc.)
            $taxonomy = 'pa_color';
            $meta = get_post_meta($value['variation_id'], 'attribute_'.$taxonomy, true);
            $vari_color = get_term_by('slug', $meta, $taxonomy);
            $vari_color = $vari_color->name; 
            sort($vari_color);

            // Use this to display the SLUG (or value if it's a number) of the filter
            $vari_size = $value['attributes']['attribute_pa_size'];
            sort($vari_size);

            $vari_obj = new WC_Product_variation($vari_id);
            $vari_stock = get_post_meta( $vari_id, '_stock',true );
            $sum+= $vari_stock;

            if ( $vari_stock > 0 ) {
               echo "<span>{$vari_color} {$vari_size}: {$vari_stock}</span>";
            }
         }
         echo "<span>Sum: {$sum} product(s)</span></div>";
      }
   }

}

Styling the column with CSS (through PHP)

Since the column is in the backend, we can’t use the style.css file to apply our changes. We must call an action to pass the CSS to WordPress’ Admin Head Area.

<?php
add_action('admin_head', 'var_stock_status_css');
function var_stock_status_css() {

   echo "
     <style>
       /* Add your CSS */
     </style>
   ";

}

We are done!