Product variation WP_Query with a product category in Woocommerce

Geme

With Woocommerce, I am trying to make a WP_Query for product variations post type with a product category 'Apple'.

$args = array(
    'product_cat'    => 'Apple',
    'post_type'      => array('product', 'product_variation'),
    'post_status'    => 'publish',
    'key'            => '_visibility',
    'value'          => 'visible',
    'posts_per_page' => 100,
    'taxonomy'       => 'pa_size',
    'meta_value'     => '39',
    'meta_query'     => array(
        array(
            'key'         => '_stock',
            'value'       => 0,
            'compare'     => '>'
        )
    )
);

But I can't get it work in this query. If I remove 'product_cat' => 'Apple', the query works. Why?

LoicTheAztec

There is many mistakes in this WP_Query regarding Woocommerce products:

  • For product category and product attribute you should better use normally a tax_query instead.
  • For Product visibility, since Woocommerce 3, it's handled by product_visibility taxonomy for 'exclude-from-search' and 'exclude-from-catalog' terms.

Important notes about Product variations:

  • Product categories (or product tags) are not handled by Product variations, but by the parent Variable product
  • Product attributes for variations are handled as post meta data with a meta_key prepended by "attribute_" and a met_value that is the term slug.
  • Product visibility is not handled in product variations, as they are not displayed in archives pages.
  • They are not displayed in archive pages (as mentioned before).

So when using a WP_Query, you can NOT query at the same time "product" post type and "product_variation" post type as they are really different.

To make your query work for "product_variation" post type, you need a little utility function that will get the parent variable product for a product category (or any custom taxonomy as Product tags…):

// Utility function to get the parent variable product IDs for a any term of a taxonomy
function get_variation_parent_ids_from_term( $term, $taxonomy, $type ){
    global $wpdb;

    return $wpdb->get_col( "
        SELECT DISTINCT p.ID
        FROM {$wpdb->prefix}posts as p
        INNER JOIN {$wpdb->prefix}posts as p2 ON p2.post_parent = p.ID
        INNER JOIN {$wpdb->prefix}term_relationships as tr ON p.ID = tr.object_id
        INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
        WHERE p.post_type = 'product'
        AND p.post_status = 'publish'
        AND p2.post_status = 'publish'
        AND tt.taxonomy = '$taxonomy'
        AND t.$type = '$term'
    " );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works. Necessary for the WP_Query below


Here WP_Query code for Product variations (only) related to a specific product category and specific variation attribute values:

// Settings
$cat_name = 'Apple'; // Product category name
$attr_taxonomy = 'pa_size'; // Product attribute
$attribute_term_slugs = array('39'); // <== Need to be term SLUGs

$query = new WP_Query( array(
    'post_type'       => 'product_variation',
    'post_status'     => 'publish',
    'posts_per_page'  => 100,
    'post_parent__in' => get_variation_parent_ids_from_term( $cat_name, 'product_cat', 'name' ), // Variations
    'meta_query'      => array(
        'relation'    => 'AND',
        array(
            'key'     => '_stock',
            'value'   => 0,
            'compare' => '>'
        ),
        array( 
            'key'     => 'attribute_'.$attr_taxonomy, // Product variation attribute
            'value'   => $attribute_term_slugs, // Term slugs only
            'compare' => 'IN',
        ),
    ),
) );

// Display the queried products count
echo '<p>Product count: ' . $query->post_count . '<p>';

// Displaying raw output for posts
print_pr($query->posts);

Tested and works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Exclude a WooCommerce product category from a WP_Query

Get WooCommerce Subscription product type and specific category in a WP_Query

Query by product category, product tags and price in Woocommerce

Get all Woocommerce products from current product category term Id in a WP_Query

Check a product category for a product in Woocommerce

Woocommerce Additional Message on Product Variation

Customize function Variation product in Woocommerce

Limit posts on a WP_Query and in WooCommerce product query

Woocommerce: Product Category Widget

Woocommerce: Query total sales by product tag AND product category?

Order by custom Woocommerce product sorting in a WP_Query

Display the product price in a WP_Query loop in Woocommerce

Get sku from Woocommerce product bookings in a WP_Query

Custom decimals in WooCommerce product prices for a product category

Set a product category term in a product on Woocommerce

List main product subcategories of a product category in WooCommerce

Check if a product belongs to a specific product category in Woocommerce

Get Woocommerce Product Categories from Product variations in a WP_Query loop

Move the variation description to the Woocommerce product variable description

Custom HTML for WooCommerce product variation dropdown

Woocommerce 3.1 product variation meta data

Bulk remove product variation images in Woocommerce

Change original price in cart for WooCommerce product variation

Change WooCommerce variable product title based on variation

How to get WooCommerce product variation values

Customize the selected product variation prices in WooCommerce

Woocommerce change product variation via JS

How To check Product Have Variation in woocommerce

Remove specific product variation from cart in WooCommerce