skip to Main Content

is it possible to do so that in the admin panel in the product view make it so that the number of available products for each variant is displayed as in the example below?:

Image

I have absolutely no idea how to do that. I will be grateful for your help

2

Answers


  1. Add the code in your active theme functions.php file.
    First create new column using the filter manage_edit-product_columns

         function add_variation_id_stock_column($columns) {
                $new_columns = array();
            
                foreach ($columns as $key => $label) {
                    $new_columns[$key] = $label;
        // Here you can set the column to be shown after name column or any other column u want. 
    //If you are not sure about the column name using inspector tool of your browser check the column ID.
                    if ($key === 'name') { 
                        $new_columns['variation_id_stock'] = 'Variation ID | Stock'; //Name it as you want
                    }
                }
            
                return $new_columns;
            }
            
            add_filter('manage_edit-product_columns', 'add_variation_id_stock_column');
    

    After that using manage_product_posts_custom_column action you can pull the data you want.

    function variation_id_stock_column_content($column, $post_id) {
        if ($column === 'variation_id_stock') {
            $product = wc_get_product($post_id);
            if ($product->is_type('variable')) {
                $variations = $product->get_children();
                foreach ($variations as $variation_id) {
                    $variation = wc_get_product($variation_id);
                    $stock_quantity = $variation->get_stock_quantity();
                    $variation_id = $variation->get_id();
                    echo '<p>Variation ID: ' . $variation_id . ' | Stock: ' . $stock_quantity . '</p>';
                }
            } else {
                echo '-'; //Do something else if its not variable product
            }
        }
    }
    
    add_action('manage_product_posts_custom_column', 'variation_id_stock_column_content', 10, 2);
    
    Login or Signup to reply.
  2. Code goes in the functions.php of your theme or child theme. Tested and works.
    enter image description here

        // Add a custom column to the products admin table
    function add_custom_variations_column($columns) {
        $columns['product_variations'] = 'Product Variations';
        return $columns;
    }
    add_filter('manage_product_posts_columns', 'add_custom_variations_column');
    
    // Display variation data in the custom column
    function display_custom_variations_column($column, $post_id) {
        if ($column === 'product_variations') {
            $product = wc_get_product($post_id);
    
            if ($product->is_type('variable')) {
                $variations = $product->get_children();
    
                if (!empty($variations)) {
                    $variation_titles = array();
    
                    foreach ($variations as $variation_id) {
                        $variation = wc_get_product($variation_id);
                        $variation_titles[] = $variation->get_title();
                    }
    
                    echo implode(', ', $variation_titles);
                } else {
                    echo 'No variations';
                }
            } else {
                echo '-';
            }
        }
    }
    add_action('manage_product_posts_custom_column', 'display_custom_variations_column', 10, 2);
    
    // Make the custom column sortable
    function make_variations_column_sortable($columns) {
        $columns['product_variations'] = 'product_variations';
        return $columns;
    }
    add_filter('manage_edit-product_sortable_columns', 'make_variations_column_sortable');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search