Understanding the wc_get_orders() Function in WooCommerce

The wc_get_orders() function in WooCommerce is a powerful tool that allows developers to retrieve orders based on specified parameters, providing a convenient way to query and manipulate order data. When we call this function, it executes a database query to retrieve orders matching specific conditions and returns an array of order objects.

How wc_get_orders() Works

In the backend, WooCommerce utilizes WordPress’s robust database querying capabilities, specifically the WP_Query class, to retrieve orders based on the parameters provided. It leverages the WordPress data structure, particularly post types and metadata structures, to search for order information efficiently.

In order storage methods based on custom post types, order data is stored in the post data table, and wc_get_orders uses WP_Query under the hood to retrieve order data. In HPOS storage methods, order data is stored in WooCommerce’s custom data tables, and wc_get_orders uses a customized storage class to retrieve order data.

Below is a basic example of retrieving the total earnings from completed orders:

function woo_total_earning()
{
    $args  = ['status' => 'completed', 'limit' => -1, 'type' => 'shop_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;
}

The following are some commonly used parameters available for the wc_get_orders() function in WooCommerce:

  1. ‘status’: Filter orders based on order status (e.g., ‘completed’, ‘pending’, ‘processing’).
  2. ‘customer’: Search for orders based on customer information. You can specify a customer ID or email address.
  3. ‘date_created’: Search for orders created within a specified date range. Comparison operators such as “>”, “<“, “>=”, “<=” or specific dates in “YYYY-MM-DD” format can be used.
  4. ‘date_modified’: Similar to “date_created,” filtering orders based on the modification date.
  5. ‘orderby’: Specify the attribute for sorting orders. Options include “date,” “id,” “title,” “menu_order,” “total,” etc.
  6. ‘order’: Determine the order of searching for orders. Values can be “ASC” (ascending) or “DESC” (descending).
  7. ‘limit’: Limit the number of orders retrieved. Used for pagination or limiting the number of results. Setting limit to -1 indicates retrieving all.
  8. ‘page’: Specify the page number of results when implementing pagination.
  9. ‘paginate’: If set to “true,” enables result pagination.
  10. ‘type’: Specify the post type of orders to search for. The default is “shop_order.”
$args = ['status' => 'completed', 'limit' => -1, 'type' => 'shop_order'];
$order = wc_get_orders($args);

// Get Order ID and Key
$order->get_id();
$order->get_order_key();

// Get Order Totals
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_total_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$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_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_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 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 Order Line Data
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();

// Get Order Shipping Information
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();

// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();

// Get Order Customer, Billing, and Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$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_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_email();
$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 Order Payment Information
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();

// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_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 Order Status
$order->get_status();

// Get Thank You Page URL
$order->get_checkout_order_received_url();

By using wc_get_orders() and its flexible parameters, developers can dynamically search for specific sets of orders according to their needs, whether for displaying orders on the frontend, processing orders in the backend, generating reports, or performing other operations in a WooCommerce-driven website or application.

In conclusion, wc_get_orders() is a fundamental feature in WooCommerce that enables developers to manage and process order data efficiently, thereby helping to customize and improve online stores based on WordPress.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *