2

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); 

2 Answers 2

7

You should use wc_get_products and a custom filter for adding your specific query.

Example

I want to find products containing a specific attribute value "table-filter".

  $args = array(
            'table-filter' => array(1,2,3)
        );
  $products = wc_get_products($args);

Than I have a filter for this:

add_filter('woocommerce_product_data_store_cpt_get_products_query', 'my_handle_custom_query_var', 10, 2);

function my_handle_custom_query_var($query, $query_vars) {
    if (!empty($query_vars['table-filter'])) {
        $query['tax_query'][] = array(
            'taxonomy' => 'pa_table-filter',
            'field'    => 'term_id', //default
            'terms'    => $query_vars['table-filter'],
            'operator' => 'IN',
        );
    }
    return $query;
}

Hint: The generated attribute taxonomy from WooCommerce is always prefixed with "pa_", so if you attributes slug is "table-filter", the taxonomy will be "pa_table-filter".

-1

I think you has a proble on 'tax_query'. If you want you query array on 'tax_query', you need flown on format wordpress

And short explain for you: in 'tax_query' need 'relation':

 'tax_query'           => array(
   'relation'=>'AND', // 'AND' 'OR' ...
    array(
      'taxonomy'        => 'product_cat',
      'field'           => 'slug',
      'terms'           => array('cat1', 'cat2', 'cat3'),
      'operator'        => 'IN',
    ),
    array(
      'taxonomy'        => 'pa_regions',
      'field'           => 'slug',
      'terms'           => 'newyork',
      'operator'        => 'IN'
    )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.