In WooCommerce, I have this function that loops through all the orders and get the orders that have coupon discounts:
$orders = wc_get_orders( array('numberposts' => -1) );
// Loop through each WC_Order Object
foreach( $orders as $order ){
if ( sizeof($order->get_used_coupons()) > 0 ) {
$order_data = $order->get_data(); // The Order data
$data = 'Order Number: #' . $order->id . '<br>' .'Order Status: '. $order->status . '<br>' . 'Order Creation Date: ' . $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->total . '<br>' . 'Customer Username: ' . $order_billing_first_name = $order_data['billing']['first_name'] . '<br>' . 'Customer E-Mail: '. $order_billing_email = $order_data['billing']['email'] . '<br>' . 'Customer Phone: ' . $order_billing_phone = $order_data['billing']['phone'] . '<br>';
}
}
Instead of coupons, Is it possible to get all orders that have products on sale? How I can do that?
Updated: The following will let you get WooCommerce orders that have items "on sale".
A function with a custom SQL query that get Orders Ids with "on sale" items:
Code goes in function.php file of your active child theme (or active theme). Tested and works.
USAGE in your code: