如何获取 WooCommerce 订单详细信息

新手上路,请多包涵

在 WooCommerce 中,来自以下行代码:

 $order = new WC_Order( $order_id );

如何从订单 ID 获取 WooCommerce 订单详细信息?

原文由 Faisal Ijaz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 710
2 个回答

3.0+ 版本中的 WOOCOMMERCE 订单

自 Woocommerce 大型主要更新 3.0+ 以来,情况发生了很大变化:

有关的:

如何从 WooCommerce 中的订单中获取客户详细信息?

在 WooCommerce 3 中获取订单商品和 WC_Order_Item_Product

因此,在 foreach 循环中将无法像以前一样访问 Order items 属性,您将不得不使用 这些特定的 getter 和 setter 方法

使用一些 WC_OrderWC_Abstract_Order 方法(示例):

 // Get an instance of the WC_Order object (same as before)
$order = wc_get_order( $order_id );

$order_id  = $order->get_id(); // Get the order ID
$parent_id = $order->get_parent_id(); // Get the parent order ID (for subscriptions…)

$user_id   = $order->get_user_id(); // Get the costumer ID
$user      = $order->get_user(); // Get the WP_User object

$order_status  = $order->get_status(); // Get the order status (see the conditional method has_status() below)
$currency      = $order->get_currency(); // Get the currency used
$payment_method = $order->get_payment_method(); // Get the payment method ID
$payment_title = $order->get_payment_method_title(); // Get the payment method title
$date_created  = $order->get_date_created(); // Get date created (WC_DateTime object)
$date_modified = $order->get_date_modified(); // Get date modified (WC_DateTime object)

$billing_country = $order->get_billing_country(); // Customer billing country

// ... and so on ...

对于作为条件方法的订单状态 _(其中“the_targetedstatus”需要定义并替换为订单状态以定位特定订单状态)

>  if ( $order->has_status('completed') ) {
>     // Do something
> }
>
> ```

获取和访问订单数据属性(在值数组中):

// Get an instance of the WC_Order object \(order = wc_get_order( \)order_id );

\(order_data = \)order->get_data(); // The Order data

\(order_id = \)order_data[‘id’]; \(order_parent_id = \)order_data[‘parent_id’]; \(order_status = \)order_data[‘status’]; \(order_currency = \)order_data[‘currency’]; \(order_version = \)order_data[‘version’]; \(order_payment_method = \)order_data[‘payment_method’]; \(order_payment_method_title = \)order_data[‘payment_method_title’]; \(order_payment_method = \)order_data[‘payment_method’]; \(order_payment_method = \)order_data[‘payment_method’];

Creation and modified WC_DateTime Object date string

// Using a formated date ( with php date() function as method) \(order_date_created = \)order_data[‘date_created’]->date(‘Y-m-d H:i:s’); \(order_date_modified = \)order_data[‘date_modified’]->date(‘Y-m-d H:i:s’);

// Using a timestamp ( with php getTimestamp() function as method) \(order_timestamp_created = \)order_data[‘date_created’]->getTimestamp(); \(order_timestamp_modified = \)order_data[‘date_modified’]->getTimestamp();

\(order_discount_total = \)order_data[‘discount_total’]; \(order_discount_tax = \)order_data[‘discount_tax’]; \(order_shipping_total = \)order_data[‘shipping_total’]; \(order_shipping_tax = \)order_data[‘shipping_tax’]; \(order_total = \)order_data[‘total’]; \(order_total_tax = \)order_data[‘total_tax’]; \(order_customer_id = \)order_data[‘customer_id’]; // … and so on

BILLING INFORMATION:

\(order_billing_first_name = \)order_data[‘billing’][‘first_name’]; \(order_billing_last_name = \)order_data[‘billing’][‘last_name’]; \(order_billing_company = \)order_data[‘billing’][‘company’]; \(order_billing_address_1 = \)order_data[‘billing’][‘address_1’]; \(order_billing_address_2 = \)order_data[‘billing’][‘address_2’]; \(order_billing_city = \)order_data[‘billing’][‘city’]; \(order_billing_state = \)order_data[‘billing’][‘state’]; \(order_billing_postcode = \)order_data[‘billing’][‘postcode’]; \(order_billing_country = \)order_data[‘billing’][‘country’]; \(order_billing_email = \)order_data[‘billing’][‘email’]; \(order_billing_phone = \)order_data[‘billing’][‘phone’];

SHIPPING INFORMATION:

\(order_shipping_first_name = \)order_data[‘shipping’][‘first_name’]; \(order_shipping_last_name = \)order_data[‘shipping’][‘last_name’]; \(order_shipping_company = \)order_data[‘shipping’][‘company’]; \(order_shipping_address_1 = \)order_data[‘shipping’][‘address_1’]; \(order_shipping_address_2 = \)order_data[‘shipping’][‘address_2’]; \(order_shipping_city = \)order_data[‘shipping’][‘city’]; \(order_shipping_state = \)order_data[‘shipping’][‘state’]; \(order_shipping_postcode = \)order_data[‘shipping’][‘postcode’]; \(order_shipping_country = \)order_data[‘shipping’][‘country’];


使用 **`WC_Order_Item_Product`** 和 **`WC_Order_Item`** 方法获取订单项目并访问数据:

// Get an instance of the WC_Order object \(order = wc_get_order(\)order_id);

// Iterating through each WC_Order_Item_Product objects foreach (\(order->get_items() as \)item_key => $item ):

## Using WC_Order_Item methods ##

// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item->get_id();

## Using WC_Order_Item_Product methods ##

$product      = $item->get_product(); // Get the WC_Product object

$product_id   = $item->get_product_id(); // the Product id
$variation_id = $item->get_variation_id(); // the Variation id

$item_type    = $item->get_type(); // Type of the order item ("line_item")

$item_name    = $item->get_name(); // Name of the product
$quantity     = $item->get_quantity();
$tax_class    = $item->get_tax_class();
$line_subtotal     = $item->get_subtotal(); // Line subtotal (non discounted)
$line_subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax (non discounted)
$line_total        = $item->get_total(); // Line total (discounted)
$line_total_tax    = $item->get_total_tax(); // Line total tax (discounted)

## Access Order Items data properties (in an array of values) ##
$item_data    = $item->get_data();

$product_name = $item_data['name'];
$product_id   = $item_data['product_id'];
$variation_id = $item_data['variation_id'];
$quantity     = $item_data['quantity'];
$tax_class    = $item_data['tax_class'];
$line_subtotal     = $item_data['subtotal'];
$line_subtotal_tax = $item_data['subtotal_tax'];
$line_total        = $item_data['total'];
$line_total_tax    = $item_data['total_tax'];

// Get data from The WC_product object using methods (examples)
$product        = $item->get_product(); // Get the WC_Product object

$product_type   = $product->get_type();
$product_sku    = $product->get_sku();
$product_price  = $product->get_price();
$stock_quantity = $product->get_stock_quantity();

endforeach;

”`

所以使用 get_data() 方法允许我们访问受保护的数据(关联数组模式)……

原文由 LoicTheAztec 发布,翻译遵循 CC BY-SA 4.0 许可协议

使用 wp/wc rest api:

         $request = new WP_REST_Request('GET', '/wc/v3/orders/<YOUR_ORDER_ID');

        $response = rest_do_request($request);
        $server = rest_get_server();
        $order = $server->response_to_data($response, false);

        print_r( $order['id'] );
        print_r( $order['date_created'] );
        print_r( $order['status'] );
        ...

来源: https ://wpscholar.com/blog/internal-wp-rest-api-calls/,https: //developer.wordpress.org/rest-api/reference/posts/#list-posts

原文由 Hamid Araghi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题