如何获取 WooCommerce 订单详细信息 [英] How to get WooCommerce order details

查看:45
本文介绍了如何获取 WooCommerce 订单详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中来自以下行代码:

$order = new WC_Order( $order_id );

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

解决方案

WOOCOMMERCE ORDERS IN VERSION 3.0+

自从 Woocommerce 重大更新 3.0+ 以来,情况发生了很大变化:

<块引用>

相关:
如何从 WooCommerce 的订单中获取客户详细信息?
获取订单商品和 WC_Order_Item_Product在 WooCommerce 3

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

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

//获取WC_Order 对象的一个​​实例(和之前一样)$order = wc_get_order( $order_id );$order_id = $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();//获取订单状态(参见下面的条件方法has_status())$currency = $order->get_currency();//获取使用的货币$payment_method = $order->get_payment_method();//获取支付方式ID$payment_title = $order->get_payment_method_title();//获取支付方式标题$date_created = $order->get_date_created();//获取创建日期(WC_DateTime 对象)$date_modified = $order->get_date_modified();//获取修改日期(WC_DateTime 对象)$billing_country = $order->get_billing_country();//客户账单国家//... 等等 ...

<块引用>

对于作为条件方法的订单状态(其中需要定义the_targeted_status"并替换为订单状态以针对特定订单状态):

if ( $order->has_status('completed') ) {//做点什么}

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

//获取 WC_Order 对象的实例$order = wc_get_order( $order_id );$order_data = $order->get_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'];##创建和修改WC_DateTime对象日期字符串##//使用格式化的日期(使用 php date() 函数作为方法)$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');//使用时间戳(使用 php getTimestamp() 函数作为方法)$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'];//... 等等## 账单信息:$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'];## 运输信息:$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_ProductWC_Order_Item 方法获取订单商品并访问数据:

//获取 WC_Order 对象的实例$order = wc_get_order($order_id);//遍历每个 WC_Order_Item_Product 对象foreach ($order->get_items() as $item_key => $item ):## 使用 WC_Order_Item 方法 ##//Item ID 可直接从 foreach 循环中的 $item_key 访问或$item_id = $item->get_id();## 使用 WC_Order_Item_Product 方法 ##$product = $item->get_product();//获取 WC_Product 对象$product_id = $item->get_product_id();//产品标识$variation_id = $item->get_variation_id();//变量标识$item_type = $item->get_type();//订单项的类型(line_item")$item_name = $item->get_name();//产品名称$quantity = $item->get_quantity();$tax_class = $item->get_tax_class();$line_subtotal = $item->get_subtotal();//行小计(未打折)$line_subtotal_tax = $item->get_subtotal_tax();//行小计税(未贴现)$line_total = $item->get_total();//总行(折扣)$line_total_tax = $item->get_total_tax();//行总税(已打折)## 访问订单项数据属性(在值数组中)##$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'];//使用方法从 WC_product 对象获取数据(示例)$product = $item->get_product();//获取 WC_Product 对象$product_type = $product->get_type();$product_sku = $product->get_sku();$product_price = $product->get_price();$stock_quantity = $product->get_stock_quantity();Endforeach;

<块引用>

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

In WooCommerce from the following line code:

$order = new WC_Order( $order_id );

How can I get WooCommerce order details from the order id?

解决方案

WOOCOMMERCE ORDERS IN VERSION 3.0+

Since Woocommerce mega major Update 3.0+ things have changed quite a lot:

Related:
How to get Customer details from Order in WooCommerce?
Get Order items and WC_Order_Item_Product in WooCommerce 3

So the Order items properties will not be accessible as before in a foreach loop and you will have to use these specific getter and setter methods instead.

Using some WC_Order and WC_Abstract_Order methods (example):

// 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 ...

For order status as a conditional method (where "the_targeted_status" need to be defined and replaced by an order status to target a specific order status):

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

Get and access to the order data properties (in an array of values):

// 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'];

Get the order items and access the data with WC_Order_Item_Product and WC_Order_Item methods:

// 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;

So using get_data() method allow us to access to the protected data (associative array mode) …

这篇关于如何获取 WooCommerce 订单详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆