WooCommerce 中的 wc_get_orders()函数是一个强大的工具,它允许开发人员根据指定参数获取订单,为查询和操作订单数据提供了便利。我们调用该函数时,它会执行数据库查询,获取符合指定条件的订单,并返回一个订单对象数组。

wc_get_orders()的工作方式

在后台,WooCommerce 使用 WordPress 强大的数据库查询功能,特别是其WP_Query类,根据提供的参数获取订单。它利用 WordPress 的数据库结构,特别是文章类型和元数据结构,高效地检索订单信息。

在基于自定义文章类型的订单存储方式中,订单数据存储在文章数据表中,wc_get_orders 底层使用WP_Query获取订单数据;在HPOS 存储方式中,订单数据存储在 WooCommerce 自定义的数据中,wc_get_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;
}

以下是 WooCommerce 中wc_get_orders()函数可使用的一些常用参数:

  1. ‘状态’:根据订单状态(如 “已完成”、”待处理”、”正在处理”)过滤订单。
  2. customer”(客户根据客户信息检索订单。您可以指定客户的 ID 或电子邮件。
  3. 创建日期检索在指定日期范围内创建的订单。可以使用比较运算符,如”>”、”=”、”
  4. 修改日期”:与 “date_created “类似,根据修改日期筛选订单。
  5. orderby’:指定订单排序的属性。选项包括 “日期”、”id”、”标题”、”菜单订单”、”总计 “等。
  6. order”:”订单”:决定检索订单的顺序。值可以是 “ASC”(升序)或 “DESC”(降序)。
  7. limit”(限制):限制检索订单的数量。用于分页或限制结果数量。将限制设为-1 表示检索全部。
  8. page’(页码):实施分页时指定结果的页码。
  9. 分页”:如果设置为 “true,则启用结果分页。
  10. 类型指定检索订单的文章类型。默认为 “shop_order”。
$args = ['status' => 'completed', 'limit' => -1, 'type' => 'shop_order'];
$order = wc_get_orders($args);

// 获取订单 ID 和 Key
$order->get_id();
$order->get_order_key();

// 获取订单金额
$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();

// 获取订单项目
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"
}

// 其他订单信息
$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();

// 获取订单行数据
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();

// 获取订单发货信息
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();

// 获取订单日期
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();

// 获取订单用户、帐单和送货地址
$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();

// 获取订单支付信息
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();

// 获取订单 URL
$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();

// 获取订单状态
$order->get_status();

// 获取感谢页面 URL
$order->get_checkout_order_received_url();

通过使用 wc_get_orders()及其灵活的参数,开发人员可以根据自己的需要动态检索特定的订单集,无论是在前端显示订单、在后台处理订单、生成报告,还是在 WooCommerce 驱动的网站或应用程序中执行其他操作。

总之,wc_get_orders()是 WooCommerce 中的一个基础功能,它使开发人员能够高效地管理和处理订单数据,从而有助于定制和改进基于 WordPress 的在线商店。