1

I would like to List all orders that contain products that had discounts on them, so I can generate a report based on only the orders that have discounts on them and get the [ order number, order date, order status, order total, user name, email and phone ]

of every order that have product with discount

something like

if (order_had_product_with_discount) {
    get the [ order number, order date, order status, order total, user name, email and phone ] of this order
}  

It's not valid code but I need to know where to start.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
mohamed adel
  • 695
  • 1
  • 15
  • 36

1 Answers1

2

You will use the WC_Order method get_used_coupons() on the $order variable (the WC_Order Object) like:

if ( sizeof($order->get_used_coupons()) > 0 ) {
    // Your code goes here
}

Now to get the order number, the order date, the order status, the order total, the user name, the email and phone, you will find everything on the following threads:


Note: You can get the WC_Order object from $order_id variable (order ID) with:

$order = wc_get_order( $order_id );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Good answer! – Mr. Jo Apr 07 '19 at 13:14
  • As Always Who else to give the perfect answers to my none sense questions except LoicTheAztec i will try it and tell you many thanks – mohamed adel Apr 07 '19 at 13:43
  • Hello i made this based on your code `$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 echo 'Order Number: #' . $order->id . '
    ' .'Order Status: '. $order->status . '
    ' . 'Order Creation Date: ' ; } }` but there is a problem it gets the discounts made by discount coupon i want it to get discounts that is made through products in wordpress not coupons
    – mohamed adel Apr 07 '19 at 16:15
  • the code loops through all the orders it should only return the orders that have discounts (SALE) on them sorry i mean SALE not discounts – mohamed adel Apr 07 '19 at 16:16
  • 1
    @mohamedadel This is a bit too late. You should try to be more explicit when asking questions ,as nobody can't guess things by magic. So may be you mean orders that have products on sale may be? If it's the case, you should ask a new question and then notify me here. Thank you. – LoicTheAztec Apr 07 '19 at 16:51
  • Yes Exactly about products on sale okay i will make a new question and notify you thank you very much you save me every time – mohamed adel Apr 07 '19 at 16:53
  • 1
    Here it is : https://stackoverflow.com/questions/55562444/woocommerce-access-orders-that-have-products-on-sale – mohamed adel Apr 07 '19 at 18:47