Skip to content

Instantly share code, notes, and snippets.

@lukecav
Created December 29, 2017 16:38
Show Gist options
  • Select an option

  • Save lukecav/05afef12feaf980c121da9afb9291ad5 to your computer and use it in GitHub Desktop.

Select an option

Save lukecav/05afef12feaf980c121da9afb9291ad5 to your computer and use it in GitHub Desktop.
Get All orders IDs for a given product ID in WooCommerce
/**
* Get All orders IDs for a given product ID.
*
* @param integer $product_id (required)
* @param array $order_status (optional) Default is 'wc-completed'
*
* @return array
*/
function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ){
global $wpdb;
$results = $wpdb->get_col("
SELECT order_items.order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_product_id'
AND order_item_meta.meta_value = '$product_id'
");
return $results;
}
@tharmann
Copy link
Copy Markdown

tharmann commented Dec 5, 2019

Perfect - exactly what I needed. Thank yoU!

@lukecav
Copy link
Copy Markdown
Author

lukecav commented Dec 5, 2019

Your welcome.

@knath632
Copy link
Copy Markdown

knath632 commented Apr 23, 2020

Thank you, Its worked for me.

@EvilBozkurt
Copy link
Copy Markdown

EvilBozkurt commented Dec 8, 2020

edit: its working for me too :D

@lukecav
Copy link
Copy Markdown
Author

lukecav commented Dec 8, 2020

K great to hear.

@nickelnext
Copy link
Copy Markdown

What if the product ids are more than one?

Copy link
Copy Markdown

ghost commented Feb 22, 2021

What if the product ids are more than one?

AND order_item_meta.meta_value IN ($product_ids)

@readej
Copy link
Copy Markdown

readej commented Apr 7, 2021

What if you only want one variation of a product?

@readej
Copy link
Copy Markdown

readej commented Apr 7, 2021

What if you only want one variation of a product?

AND order_item_meta.meta_key = '_variation_id'
AND order_item_meta.meta_value = '$variation_id'

instead of

AND order_item_meta.meta_key = '_product_id'
AND order_item_meta.meta_value = '$product_id'

Thanks for this query BTW. Appreciate it!

@carazo
Copy link
Copy Markdown

carazo commented Jan 5, 2023

Maybe this would be affected by HPOS?

The post_id in posts table will remain to assure compatibility although all the data will be out of post/postmeta so I think it will continue working without problems.

@hossein911
Copy link
Copy Markdown

Hi, I want to sort products by order count, how change this query to achieve all products order count and sort them?

@hossein911
Copy link
Copy Markdown

I found that

query =  "
        SELECT count(order_items.order_id) as order_count, order_item_meta.meta_value as product_id
        FROM {$wpdb->prefix}woocommerce_order_items as order_items
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
        LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
        WHERE posts.post_type = 'shop_order'
        AND posts.post_status = 'wc-completed'
        AND order_items.order_item_type = 'line_item'
        AND order_item_meta.meta_key = '_product_id'
        GROUP BY product_id
        ORDER BY order_count DESC";

Thankyou lukecav for the query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment