wc_get_orders() function in WooCommerce
The wc_get_orders() function in WooCommerce is a powerful tool for querying and manipulating order data by allowing developers to get orders based on specified parameters. When we call this function, it performs a database query, fetches the orders that match the specified criteria, and returns an array of order objects.
wc_get_orders()The way it works
On the backend, WooCommerce uses WordPress' powerful database query capabilities, specifically itsWP_Queryclass that fetches orders based on the parameters provided. It utilizes the WordPress database structure, specifically the post type and metadata structure, to efficiently retrieve order information.
In an order storage method based on custom article types, the order data is stored in the article data table, thewc_get_orders Bottom UseWP_QueryGet order data; in theHPOS Storage MethodsIn WooCommerce, the order data is stored in WooCommerce's customized datawc_get_orders Get order data using a customized storage class.
Below is a basic example of getting the total amount of a completed order:
function woo_total_earning()
{
$args = ['status' => 'completed', 'limit' => -1, 'type' => 'store_order'];
$order = wc_get_orders($args);
$total_amount = 0;
foreach ($order as $o) {
$total_amount += $o->get_total() - $o->get_total_refunded();
}
return $total_amount;
}Below are some of the features that are available in WooCommercewc_get_orders()Some common parameters that can be used by functions:
- ‘Status’:Filter orders based on order status (e.g. “Completed”, ”Pending”, ”Processing”).
- customerRetrieve orders based on customer information. You can specify the customer's ID or email.
- Date of creationRetrieves orders created within a specified date range. This can be done using comparison operators such as ”>”, ”=”, ”<=”, or the format “YYYY-MM-DD“ for a specific date.
- Revision Date”:Similar to “date_created“, filter orders based on modification date.
- orderby’:Specifies attributes for order sorting. Options include “date”, ”id”, ”title”, ”menu order”, ”total“, etc.
- order”: ”order”:Determines the order in which the orders are retrieved. The value can be “ASC” (ascending) or “DESC” (descending).
- limit”:Limits the number of orders retrieved. Used for paging or limiting the number of results. Setting the limit to -1 means retrieve all.
- page’:Specifies the page number of the result when implementing paging.
- Pagination”:If set to true“, then result paging is enabled.
- typologySpecifies the type of article to retrieve orders from. The default is “shop_order”.
$args = ['status' => 'complete', 'limit' => -1, 'type' => 'shop_order'];
$order = wc_get_orders($args);
// Get the Order ID and Key
$order->get_id();
$order->get_order_key();
// Get the order amount
$order->get_formatted_order_total(); // Get the order amount.
$order->get_cart_tax(); // Get the order amount.
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total(); $order->get_discount_total().
$order->get_total_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax(); $order->get_shipping_tax().
$order->get_shipping_total(); $order->get_shipping_total().
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax(); $order->get_total_tax().
$order->get_total_refunded();
$order->get_total_tax_refunded(); $order->get_total_tax_refunded().
$order->get_total_shipping_refunded(); $order->get_total_shipping_refunded();
$order->get_item_count_refunded(); $order->get_total_tax_refunded()
$order->get_total_qty_refunded().
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
// Get the order items
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product = $item->get_product();
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$subtotal = $item->get_subtotal();
$total = $item->get_total();
$tax = $item->get_subtotal_tax();
$tax_class = $item->get_tax_class();
$tax_status = $item->get_tax_status();
$allmeta = $item->get_meta_data();
$somemeta = $item->get_meta( 'anything', true );
$item_type = $item->get_type(); // e.g. "line_item", "fee"
}
// Other order information
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item_count();
$order->get_item_total();
$order->get_downloadable_items();
$order->get_coupon_codes();
// Get the order line data
$order->get_line_subtotal();
$order->get_line_tax(); // Get the order line data.
$order->get_line_total();
// Get the order shipment information
$order->get_shipping_method();
$order->get_shipping_methods(); // Get order shipping information.
$order->get_shipping_to_display();
// Get the date of the order
$order->get_date_created();
$order->get_date_modified(); // Get the order date.
$order->get_date_completed(); $order->get_date_modified(); // Get the date of the order.
$order->get_date_paid().
// Get the order user, billing and shipping address.
$order->get_customer_id();
$order->get_user_id(); // Get the order user, billing and delivery address.
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop(); $order->get_address_prop().
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city().
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country(); $order->get_billing_country().
$order->get_billing_email(); $order->get_billing_email().
$order->get_billing_phone(); $order->get_billing_phone().
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company().
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city().
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
// Get the order payment information
$order->get_payment_method();
$order->get_payment_method_title();; // Get order payment information.
$order->get_transaction_id();
// Get the order URL
$order->get_checkout_payment_url(); // Get the order URL.
$order->get_checkout_order_received_url(); // Get the order URL.
$order->get_cancel_order_url().
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint().
$order->get_view_order_url().
$order->get_edit_order_url();
// Get the status of the order
$order->get_status();
// Get the thank you page URL
$order->get_checkout_order_received_url(); // Get the checkout page URL.;
By using the wc_get_orders()With its flexible parameters, developers can dynamically retrieve specific sets of orders according to their needs, whether it's displaying orders on the front-end, processing orders in the back-end, generating reports, or performing other actions in a WooCommerce-powered website or application.
In conclusion.wc_get_orders()is a foundational feature in WooCommerce that enables developers to efficiently manage and process order data to help customize and improve WordPress-based online stores.