I want to display a report based on the order status if it's completed then display some data
but it works with all order status for example if i put on-hold it gets the data but when I put completed I get this error
Fatal error: Call to undefined method WC_Order_Refund::get_order_number()
here is my code
//Get Report for Orders that have status of delivered
foreach( $orders as $order ){
if ( $order->get_status() === 'completed'){
$order_data = $order->get_data(); // The Order data
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
}
$orders_completed .= '<tr><td>' . $order->get_order_number() . '</td>' .
'<td>' . $order->get_date_created()->date('Y-m-d H:i:s') . '</td>' .
'<td>' . $order->get_status() . '</td>' .
'<td>' . $order->get_total() . '</td>' .
'<td>' . $product_id . '</td>' .
'<td>' . $product_name . '</td>' .
'<td>' . $order->get_item_count() . '</td>' .
'<td>' . $order->get_billing_first_name() . '</td>' .
'<td>' . $order->get_billing_email() . '</td>' .
'<td>' . $order->get_billing_phone() . '</td>' .
'<td>' . $order_payment_method = $order_data['payment_method_title'] . '</td></tr>';
}
}
if i changed 'completed' to 'on-hold' or any other status it will work and get the data
but how to make it work with completed orders?
Thank you very much
The problem is related to the order type that should be "shop order"… There is 2 ways:
1) Check the order type, which need to be "shop_order" (but not "shop_order_refund"). So you can use the method get_type() that also works for WC_Order_Refund
class like:
foreach( $orders as $order ){
if ( $order->get_type() === 'shop_order' && $order->get_status() === 'completed'){
$order_data = $order->get_data(); // The Order data
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
}
$orders_completed .= '<tr><td>' . $order->get_order_number() . '</td>' .
'<td>' . $order->get_date_created()->date('Y-m-d H:i:s') . '</td>' .
'<td>' . $order->get_status() . '</td>' .
'<td>' . $order->get_total() . '</td>' .
'<td>' . $product_id . '</td>' .
'<td>' . $product_name . '</td>' .
'<td>' . $order->get_item_count() . '</td>' .
'<td>' . $order->get_billing_first_name() . '</td>' .
'<td>' . $order->get_billing_email() . '</td>' .
'<td>' . $order->get_billing_phone() . '</td>' .
'<td>' . $order_payment_method = $order_data['payment_method_title'] . '</td></tr>';
}
};
2) change the WC_Order_Query, targeting only "shop_order" post type:
$orders = wc_get_orders( array('limit' => -1, 'type' => 'shop_order') );
Both ways will avoid to get this fatal error