All my woocommerce products are variable products. Each of them have the same variation 'male' and 'female'.
I'm trying to add a column for each of them to my products overview in wp-admin both showing the stock quantity for that variation.
I've included following code which displays the sum of 'male' and 'female'. Is there a way of only querying one of them?
function add_qty_admin( $column ) {
if (!isset($columns['total_qty']))
$columns['total_qty'] = "Totaal in voorraad";
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);