# How to Get a List of WooCommerce Orders Containing a Specific Product
Finding all WooCommerce orders that include a particular product is a common task for store owners. Maybe you’re running a promotion, tracking sales of a specific item, or investigating a customer issue. This guide will walk you through how to do this, even if you’re new to coding. We’ll cover several methods, from simple to more advanced, ensuring you find the best solution for your needs.
Why You Need This: Real-World Scenarios
Before diving into the technical stuff, let’s look at why this is a useful skill:
- Promotional Campaigns: You ran a sale on “Super Widget Pro” and want to see which customers purchased it to send a follow-up email.
- Inventory Management: You’ve noticed low stock of “Awesome T-shirt” and need to identify orders containing it to understand demand.
- Customer Service: A customer claims they ordered “Deluxe Coffee Maker” but haven’t received it. You need to quickly locate their order.
- Reporting & Analytics: You want Explore this article on How To Set Up Optional Subscription Products In Woocommerce to generate a custom report showing sales figures for a specific product over a period.
Method 1: Using WooCommerce’s Built-in Order Management (The Easiest Way)
The simplest approach is to use WooCommerce’s built-in order search functionality. While not perfect for large-scale analysis, it’s great for quick checks.
1. Go to your WooCommerce Orders page: This is usually accessible through your WordPress admin dashboard.
2. Use the search bar: Type in the product name you’re looking for. WooCommerce will filter orders containing that product in their items list.
Limitations: This Read more about Alidropship For Woocommerce How To Process Orders method relies on the product name being correctly entered and only shows orders containing the specific name typed. It’s not ideal Read more about How To Redirect Woocommerce After Checkout for complex queries or large datasets.
Method 2: Using a WooCommerce Plugin (The Convenient Way)
Several plugins enhance WooCommerce’s order management capabilities. Search the WordPress plugin directory for terms like “WooCommerce order search,” “advanced WooCommerce reports,” or “WooCommerce order management.” Many offer improved search functionality, often allowing you to filter orders by product, date range, customer, and more.
Advantages: Plugins provide a user-friendly interface and often offer more advanced filtering options than the default WooCommerce system.
Disadvantages: You need to install and configure a plugin, which can sometimes introduce conflicts or slow down your website. Choose a reputable plugin with good reviews.
Method 3: Custom Code (The Powerful Way – For Developers)
For advanced users comfortable with PHP, this method provides the Check out this post: My Account Endpoints How To Reset Woocommerce most control and flexibility. We’ll use a custom function to query the database directly. This requires access to your WordPress theme’s `functions.php` file or a custom plugin. Always back up your files before making any code changes!
This code retrieves all orders containing a product with the ID `123` (replace with your product ID):
function get_orders_containing_product( $product_id ) { global $wpdb;
$orders = $wpdb->get_results( $wpdb->prepare( “
SELECT order_item_id, order_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta
WHERE meta_key = ‘_product_id’ AND meta_value = %d
“, $product_id ) );
return $orders;
}
// Example usage: Get orders containing product with ID 123
$orders = get_orders_containing_product( 123 );
// Loop through the results and display order information
foreach ( $orders as $order ) {
$order_id = $order->order_id;
echo “Order ID: ” . $order_id . “
“;
// You can further query the order details using wc_get_order( $order_id )
}
Explanation:
- This code queries the `woocommerce_order_itemmeta` table, which stores the relationship between orders and their items.
- `meta_key = ‘_product_id’` targets the product ID.
- `meta_value = %d` safely inserts the product ID, preventing SQL injection vulnerabilities.
- The function returns an array of `order_item_id` and `order_id`. You can then use `wc_get_order()` to retrieve complete order details.
Remember to replace Explore this article on How To Get All Products In Woocommerce `123` with the actual ID of your product. You can find the product ID in the product’s edit screen in your WordPress admin.
Conclusion
Finding orders containing a specific product in WooCommerce can be achieved through various methods. Choose the approach that best suits your technical skills and needs. For simple tasks, the built-in search is sufficient. For more complex analyses or large datasets, a plugin or custom code might be necessary. Remember to always back up your data before making any changes.