Created
December 29, 2017 16:38
-
-
Save lukecav/05afef12feaf980c121da9afb9291ad5 to your computer and use it in GitHub Desktop.
Get All orders IDs for a given product ID in WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Your welcome.
Thank you, Its worked for me.
edit: its working for me too :D
K great to hear.
What if the product ids are more than one?
What if the product ids are more than one?
AND order_item_meta.meta_value IN ($product_ids)
What if you only want one variation of a product?
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!
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.
Hi, I want to sort products by order count, how change this query to achieve all products order count and sort them?
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
Perfect - exactly what I needed. Thank yoU!