WooCommerce: Get purchased items for a product category using a SQL query

John Smith

I am trying to list all of the products bought that belong to a certain product category, for instance all T-shirt orders under the "clothes" product category.

I find the Woocommerce database structure quite confusing and you have to write long queries to get simple information.

Could anyone provide an example query to get all orders by a certain category?

I know this will probably involve wp_woocommerce_order_items and wp_terms tables, but get lost from there.

LoicTheAztec

The following SQL query will give you a list of product IDs purchased at least once that belongs to "clothes" product category slug:

SELECT DISTINCT tr.object_id
FROM wp_term_relationships tr
INNER JOIN wp_term_taxonomy tt
    ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms t
    ON tt.term_id = t.term_id
INNER JOIN wp_woocommerce_order_itemmeta oim
    ON tr.object_id = oim.meta_value
INNER JOIN wp_woocommerce_order_items oi
    ON oim.order_item_id = oi.order_item_id
INNER JOIN wp_posts as o
    ON oi.order_id = o.ID
WHERE tt.taxonomy = 'product_cat'
    AND t.slug = 'clothes'
    AND oim.meta_key = '_product_id'
    AND o.post_type = 'shop_order'
    AND o.post_status IN ('wc-completed','wc-processing')

Tested and works

Or in Wordpress using WPDB class this can be done using:

global $wpdb;

$product_category = 'clothes'; // Term slug

$products_ids = $wpdb->get_col( "
    SELECT DISTINCT tr.object_id
    FROM wp_term_relationships tr
    INNER JOIN wp_term_taxonomy tt
        ON tr.term_taxonomy_id = tt.term_taxonomy_id
    INNER JOIN wp_terms t
        ON tt.term_id = t.term_id
    INNER JOIN wp_woocommerce_order_itemmeta oim
        ON tr.object_id = oim.meta_value
    INNER JOIN wp_woocommerce_order_items oi
        ON oim.order_item_id = oi.order_item_id
    INNER JOIN wp_posts as o
        ON oi.order_id = o.ID
    WHERE tt.taxonomy = 'product_cat'
    AND t.slug = '$product_category'
    AND oim.meta_key = '_product_id'
    AND o.post_type = 'shop_order'
    AND o.post_status IN ('wc-completed','wc-processing')
");

// Pre-formatted raw display (an array of products ids)
echo '<pre>'; print_r($products_ids); echo '</pre>';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get also WooCommerce product category thumbnail Id using a SQL query

How to get product category ids from order items in WooCommerce

Get WooCommerce cart items count except for a product Category

Get the product category terms from cart items in WooCommerce

SQL query to get Most Frequently purchased and Most Recently purchased quantity for a customer-product pair

Make a product to be purchased only once or not if any item from a specific product category has been purchased in Woocommerce

Get WooCommerce Subscription product type and specific category in a WP_Query

Get on sale products for Woocommerce product category archives using a Get request

Query by product category, product tags and price in Woocommerce

woocommerce get product category id

Get the catalog visibility for a product in a SQL query on Woocommerce

Get total product purchased count with specific product attributes in Woocommerce

Get the category name from a WooCommerce product using the product id or the category id

Checking cart items for a product category in Woocommerce

Check for Product Category in cart items with WooCommerce

Woocommerce mySQL Query - List All Orders, Users and Purchased Items

Sync custom product price by product category on cart items in Woocommerce

Get the subcategories of the current product category in Woocommerce archives

Woocommerce - get product in category with specific attribute

Get "primary" category image from WooCommerce product

Get Woocommerce product category image url

Get and display the product category featured image in Woocommerce

Is there a way to get all the category slug of a woocommerce product

Use the product ID instead of product name in a custom SQL query on WooCommerce order items

Using Woocommerce product category as condition in an if statement

Woocommerce FIlter Product using color and Category?

Product variation WP_Query with a product category in Woocommerce

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

WooCommerce: Get the total sales for product variations in a SQL query