Variable product and standard product quantity totals in admin
-
Hi there,
I’m trying to create a column in the admin products page that will show the total product amounts for variable and simple products.
I’d also like it to be sortable if possible.At present the product count lists stock as ‘In stock × 7’ for simple products and ‘In stock’ for variable products without an in stock amount.
Is there a way I can have the variable stock amount display a total amount for all variants?
For example if a product has colour variations such as Red x3, Blue x4 the stock amount in the admin products page would read ‘In stock × 7’.
I found Francesco Di Candia’s solution for showing the total stock quantity of a variable product “Woocommerce – Trying to get variable product quantity sum in admin”
function add_qty_admin( $column ) { if (!isset($columns['total_qty'])) $columns['total_qty'] = "Total Stock"; return $columns; } add_filter( 'manage_posts_columns', 'add_qty_admin' ); function admin_post_data_row($column_name, $post_id) { global $wpdb; switch($column_name) { case 'total_qty': $query = "SELECT sum(meta_value) FROM $wpdb->posts AS p, $wpdb->postmeta AS s WHERE p.post_parent = %d AND p.post_type = 'product_variation' AND p.post_status = 'publish' AND p.id = s.post_id AND s.meta_key = '_stock'"; $product_qty = $wpdb->get_var($wpdb->prepare($query,$post_id)); if ($product_qty) echo $product_qty; break; default: break; } } add_action( 'manage_posts_custom_column', 'admin_post_data_row', 10, 2);
However if the product isn’t a variable product the area is left blank, also this seems to cause some conflicts.
And finally if this can be done would there be a way to make the column sortable?
Thanks for your help!
Kind regards, JP
- The topic ‘Variable product and standard product quantity totals in admin’ is closed to new replies.