1

Is there a way to get product_variations in WP_Query that filter / only get the product variations that have the right woocommerce-product-category, color (attribute woocommerce), size (attribute woocommerce), price (custom post meta)?

I can't figure out how to make the WP_Query.

$all_product_variation_query = new WP_Query( array(
'post_type'         => 'product_variation',
'post_status'       => 'publish',
'posts_per_page'    => '-1',
) );

This guy is so close with what i need https://wordpress.stackexchange.com/questions/246516/woocommerce-filter-by-parent-products-taxonomy-and-product-variations-meta-da

BUT i need a way to combine Product_Variation with Product and then filter to get only the product_variation with certain color (attribute), size (attribute), and taxonomy id (category)

Community
  • 1
  • 1
Raymond Seger
  • 1,080
  • 5
  • 17
  • 34
  • what you want to achieve, your question is not very clear. Do you want to get list of all the product which has variations and belongs to a specific product category ? – Raunak Gupta Feb 24 '17 at 14:24
  • I have ordinary WC_Product_Variable, and each of it have category, custom attribute color and size. What i need is showing the WC_Product_Variations (not the variable product) with the correct category, color, size. So basically showing the product "children" / Variations instead of the Product_Variable – Raymond Seger Feb 26 '17 at 05:42
  • respuesta aqui https://stackoverflow.com/questions/49342704/product-variation-wp-query-with-a-product-category-in-woocommerce – willy cotes Jun 08 '22 at 11:54

1 Answers1

1

Please try with below code.

    $product_args = array(
        'numberposts' => 1000,
        'post_status' => array('publish', 'pending', 'private', 'draft'),
        'post_type' => array('product', 'product_variation'),
        'order' => 'ASC',
            );

    $product_args['tax_query'] = array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => array(13), //vategory IDs
            'operator' => 'IN',
    ));
    $all_product_n_variation_query = new WP_Query($product_args)
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • does not work. It only works if you use it with 'post_type' =>array('product'), which is not what i need, what i need is product_variations. I need a way to combine product (that have the category data) with product_variation (which does NOT have category data, but it does have "foreign key" that links with the product_variable / parent) – Raymond Seger Feb 27 '17 at 03:11