you are not showing $quantity
, maybe that is off?
this one here definitely works:
$args = [
'post_type' => 'product_variation',
'posts_per_page' => 100,
'meta_query' => [
[
'key' => '_stock',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC'
]
]
];
$query = new WP_Query($args);
with this query, you get all variations, that are in stock. from there you can get the parent by post_parent ID via get_post_field('post_parent')
if ($query->have_posts()) :
echo '<ul>';
while ($query->have_posts()) :
$query->the_post();
echo '<li>' . get_the_title() . ': ' . get_post_meta(get_the_ID(), '_stock') . ', ' . get_post_field('post_parent') . '</li>';
endwhile;
echo '</ul>';
wp_reset_postdata();
else :
echo '<pre>nothing found</pre>';
endif;