I'm trying to get a list/count of products who's has a specific attribute and value. Products are managed and setup using the WooCommerce plugin.
Each product has the same variation set, the product is assigned to a category, I need to retrieve only product with a specific attribute i.e "newyork" and the stock quantity that is less than i.e "20"
Each product variation is set to Managed stock, but not the product itself hope that makes sense. My issue at the moment is the WordPress query I have is not checking the variation name or stock at all.
Any help would be appreciated.
<?php
$args= array(
'post_type' => array('product', 'product_variation'),
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_stock',
'value' => 20,
'compare' => '<'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('cat1', 'cat2', 'cat3'),
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_regions',
'field' => 'slug',
'terms' => 'newyork',
'operator' => 'IN'
),
)
);
$loop = new WP_Query($args);
$post_count = array();
while($loop->have_posts()) : $loop->the_post();
global $product; ?>
<pre style="background: #1c1c1b; color: #f7e700">
<?php $post_count[] = $product->ID ;?>
</pre>
<?php endwhile; ?>
$total = count($post_count);