Dorokhov.codes

Working with orders

Primary WooCommerce Database Tables

Orders and Revenue Data:

  • wp_posts: Stores WooCommerce orders as custom post types (shop_order).
  • wp_postmeta: Contains meta information for orders, such as totals, taxes, and shipping details.

Products and Stock Data:

  • wp_posts: Stores WooCommerce products as custom post types (product and product_variation).
  • wp_postmeta: Contains additional product metadata, such as price, stock status, and SKU.

Customers and Users:

  • wp_users: Stores registered customer details.
  • wp_usermeta: Contains additional customer data, such as billing and shipping addresses.
  • wp_wc_customer_lookup: Stores customer data for faster lookups.

Orders and Analytics-Specific Tables:

  • wp_wc_order_stats: Summary of order data for reporting purposes.
  • wp_wc_order_product_lookup: Data about which products are associated with specific orders.
  • wp_wc_order_tax_lookup: Tax data for orders.
  • wp_wc_order_coupon_lookup: Coupon data for orders.

Product Analytics:

  • wp_wc_product_meta_lookup: Indexed product data for faster queries, such as prices, stock status, and visibility.

Tax, Shipping, and Coupon Analytics:

  • wp_wc_tax_rate_classes: Contains tax rate data.
  • wp_wc_shipping_zones and related tables: Stores shipping zones and methods.

Getting orders

// @since  2.2
// @return bool|WC_Order|WC_Order_Refund
$order = wc_get_order( $order_id );
$order = new WC_Order( $order_id );

Order statuses

There are 7 default order statuses in WooCommerce.

Status Descripiton
Pending payment Order received, no payment initiated. Awaiting payment (unpaid).
Failed Payment failed or was declined (unpaid).
Processing Payment received (paid) and stock has been reduced; order is awaiting fulfillment. All product orders require processing, except those that only contain products which are both virtual and downloadable.
Completed Order fulfilled and complete – requires no further action.
On hold Awaiting payment – stock is reduced, but you need to confirm payment.
Canceled Canceled by an admin or the customer – stock is increased, no further action required.
Refunded Refunded by an admin – no further action required.

Order process diagram

If we take a look at wc-order-functions.php, we will find the next slugs for statuses:

function wc_get_order_statuses() {
	$order_statuses = array(
		'wc-pending'    => _x( 'Pending payment', 'Order status', 'woocommerce' ),
		'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
		'wc-on-hold'    => _x( 'On hold', 'Order status', 'woocommerce' ),
		'wc-completed'  => _x( 'Completed', 'Order status', 'woocommerce' ),
		'wc-cancelled'  => _x( 'Cancelled', 'Order status', 'woocommerce' ),
		'wc-refunded'   => _x( 'Refunded', 'Order status', 'woocommerce' ),
		'wc-failed'     => _x( 'Failed', 'Order status', 'woocommerce' ),
	);
	return apply_filters( 'wc_order_statuses', $order_statuses );
}

Order types

parent
resubscribe
renewal
switch

Working with statuses

// No internal wc- prefix is required.
$order->update_status('on-hold');

// Check if an order has status:
$order->has_status( 'pending' );

// Getting status:
$order->get_status();

/**
 * We should call this every time after a successful payment.
 * It changes the status to 'processing' or 'completed' 
 * (depending on is virtual/downloading product or not).
 */
$order->payment_complete();

Useful methods

// Gets the order number for display (by default, order ID).
$order->get_order_number();

// Gets order grand total. incl. taxes. Used in gateways.
$order->get_total();

// Gets order currency.
$order->get_currency();

// Get billing first name.
$order->get_billing_first_name();

// Get billing last name.
$order->get_billing_last_name();

// Get billing email.
$order->get_billing_email();

// Get shipping first name.
$order->get_shipping_first_name();

// Get shipping last name.
$order->get_shipping_last_name();

// Checks if an order needs payment, based on status and order total.
$order->needs_payment();

// Add meta data:
$order->add_meta_data();

// Get meta:
$order->get_meta();

// Gets the count of order items:
$order->get_item_count();