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;
}
@nickelnext

Copy link
Copy Markdown

What if the product ids are more than one?

ghost commented Feb 22, 2021

Copy link
Copy Markdown

What if the product ids are more than one?

AND order_item_meta.meta_value IN ($product_ids)

@readej

readej commented Apr 7, 2021

Copy link
Copy Markdown

What if you only want one variation of a product?

@readej

readej commented Apr 7, 2021

Copy link
Copy Markdown

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

carazo commented Jan 5, 2023

Copy link
Copy Markdown

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