-- 创造无限可能

WooCommerce开发教程:获取订单详细信息

2022-06-14 10:32:35
566 人浏览 1 人点赞
有用,点赞支持一下

相关函数

WC_Order:根据id获取订单
wc_get_order:

基础用法

  1. 访问普通数据
    $order_id = 1;
    $order = new WC_Order( $order_id );
    $orderid  = $order->get_id(); // 获取订单 ID
    $parent_id = $order->get_parent_id(); // 获取父级订单 ID
    $user_id   = $order->get_user_id(); // 获取用户 ID
    $user      = $order->get_user(); // 获取 WP_User 对象
    // 获取订单状态
    $order_status  = $order->get_status(); // 获取订单状态
    // Get Order Dates
    $date_created  = $order->get_date_created(); // 获取创建日期
    $date_modified = $order->get_date_modified(); // 获取修改日期
    $date_completed = $order->get_date_completed(); // 获取订单完成日期
    $date_paid = $order->get_date_paid(); // 获取订单付款日期
    //获取订单账单详情
    $billing_first_name = $order->get_billing_first_name();
    $billing_last_name = $order->get_billing_last_name();
    $billing_company = $order->get_billing_company();
    $billing_address1 = $order->get_billing_address_1();
    $billing_address2 = $order->get_billing_address_2();
    $billing_city = $order->get_billing_city();
    $billing_state = $order->get_billing_state();
    $billing_postcode = $order->get_billing_postcode();
    $billing_country = $order->get_billing_country();
    $billing_email = $order->get_billing_email();
    $billing_phone = $order->get_billing_phone();
    $billing_formatted_name = $order->get_formatted_billing_full_name();
    $billing_formatted_address = $order->get_formatted_billing_address();
    //获取订单物流详情
    $shipping_first_name = $order->get_shipping_first_name();
    $shipping_last_name = $order->get_shipping_last_name();
    $shipping_company = $order->get_shipping_company();
    $shipping_address1 = $order->get_shipping_address_1();
    $shipping_address2 = $order->get_shipping_address_2();
    $shipping_city = $order->get_shipping_city();
    $shipping_state = $order->get_shipping_state();
    $shipping_postcode = $order->get_shipping_postcode();
    $shipping_country = $order->get_shipping_country();
    $shipping_formatted_name = $order->get_formatted_shipping_full_name();
    $shipping_formatted_address = $order->get_formatted_shipping_address();
    //获取订单付款详情
    $currency      = $order->get_currency(); // 获取所用的货币 
    $payment_title = $order->get_payment_method_title(); // 获取付款方式名称
    $payment_method = $order->get_payment_method(); // 获取付款方式 ID
    $fees = $order->get_fees(); // 获取订单手续费
    $subtotal = $order->get_subtotal(); // 获取订单费用小计
    $total_tax = $order->get_total_tax(); // 获取订单总税费
    $total = $order->get_total(); // 获取订单总费用
    
  2. 访问一些有权限的数据
    $order_id = 1;
    $order = wc_get_order( $order_id );
    $order_data = $order->get_data();
    
  3. 如果订单有多个项目
    foreach ( $order->get_items() as $item_id => $item ) {
    $product = $item->get_product();
    $product_id = $item->get_product_id();
    $variation_id = $item->get_variation_id();
    $name = $item->get_name();
    $quantity = $item->get_quantity();
    $subtotal = $item->get_subtotal();
    $total = $item->get_total();
    $tax = $item->get_subtotal_tax();
    $all_meta = $item->get_meta_data();
    $single_meta = $item->get_meta( '_meta_key', true );
    $type = $item->get_type();
    // 等等...
    }