php 在 WooCommerce 3 中获取订单项目和 WC_Order_Item_Product
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45706007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Order items and WC_Order_Item_Product in WooCommerce 3
提问by Solomon Closson
Ok, reading up on the changes in WooCommerce 3.0+, it seems that you can not access this class directly anymore, so I would assume that this code needs to be changed, since it is spitting out an error:
好的,阅读 WooCommerce 3.0+ 中的更改,您似乎无法再直接访问此类,因此我认为需要更改此代码,因为它正在吐出错误:
$order_item_id = 15;
$order_item = new WC_Order_Item_Product($order_item_id);
$return = $order_item->get_id() ? $order_item : false;
But, embarrassingly, I'm not sure how to change this code to use the correct new getter and setter functions in the newest version of this class, which no longer has a construct. How to do this properly? I don't see any get
function on getting the order item in the same way as the above.
https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html
但是,令人尴尬的是,我不确定如何更改此代码以在该类的最新版本中使用正确的新 getter 和 setter 函数,该版本不再具有构造。如何正确地做到这一点?我没有看到get
以与上述相同的方式获取订单项目的任何功能。
https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html
Maybe I am overlooking something here?
也许我在这里忽略了一些东西?
回答by LoicTheAztec
If you use the
get_id()
method, you get your item ID which is15
in your code.
如果您使用该
get_id()
方法,您将获得您15
的代码中的项目 ID 。
Get the product ID:
The correct WC_Order_Item_Product method to get the Product id is: get_product_id()
获取产品 ID:获取产品 ID
的正确 WC_Order_Item_Product 方法是:get_product_id()
Get the variation ID:
The correct WC_Order_Item_Product method to get the Product id is: get_variation_id()
获取变体 ID:
获取产品ID的正确 WC_Order_Item_Product 方法是:get_variation_id()
Get the order ID
The correct WC_Order_Item_Product method to get the Order id is: get_order_id()
获取订单 ID 获取订单 ID
的正确 WC_Order_Item_Product 方法是:get_order_id()
Get the WC_Product object
The correct WC_Order_Item_Product method to get WC_Product object is:
get_product()
获取 WC_Product 对象 获取 WC_Product 对象
的正确 WC_Order_Item_Product 方法是:
get_product()
Get the WC_Order object
The correct WC_Order_Item_Product method to get WC_order object is:
get_order()
获取 WC_Order 对象 获取 WC_order 对象
的正确 WC_Order_Item_Product 方法是:
get_order()
Get and unprotecting the data and meta datausing WC_Data
methods:get_data()
get_meta_data()
使用WC_Data
方法获取和取消保护数据和元数据:get_data()
get_meta_data()
Get The WC_Product
object from the order item ID:
WC_Product
从订单商品 ID 中获取对象:
$order_item_id = 15;
$item = new WC_Order_Item_Product($order_item_id);
// The product ID
$product_id = $item->get_product_id();
// The variation ID
$variation_id = $item->get_variation_id();
// The WC_Product object
$product = $item->get_product();
// The quantity
$quantity = $item->get_quantity();
// The order ID
$order_id = $item->get_order_id();
// The WC_Order object
$order = $item->get_order();
// The item ID
$item_id = $item->get_id(); // which is your $order_item_id
// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();
// Get the product SKU (using WC_Product method)
$sku = $product->get_sku();
// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
Get the order items from the WC_Order
object(and use theWC_product
Object):
从WC_Order
对象中获取订单项(并使用WC_product
对象):
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
//Get the product ID
$product_id = $item->get_product_id();
//Get the variation ID
$variation_id = $item->get_variation_id();
//Get the WC_Product object
$product = $item->get_product();
// The quantity
$quantity = $item->get_quantity();
// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();
//Get the product SKU (using WC_Product method)
$sku = $product->get_sku();
// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
}
Accessing data and custom meta data:
访问数据和自定义元数据:
1) unprotecting WC_Order_Item_Product
dataand custom meta data:
1)取消保护WC_Order_Item_Product
数据和自定义元数据:
You can use all WC_Order_Item_Product data
methods or you can unprotect the data using WC_Data
following methods:
您可以使用所有WC_Order_Item_Product data
方法,也可以使用WC_Data
以下方法取消保护数据:
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
// Get the common data in an array:
$item_product_data_array = $item->get_data();
// Get the special meta data in an array:
$item_product_meta_data_array = $item->get_meta_data();
// Get the specific meta data from a meta_key:
$meta_value = $item->get_meta( 'custom_meta_key', true );
// Get all additional meta data (formatted in an unprotected array)
$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
}
2) The Array Accessis still possible (for backwards compatibility with legacy arrays)to get the common data directly:
2) Array Access仍然可以(为了与遗留数组向后兼容)直接获取公共数据:
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
$product_id = $item['product_id']; // Get the product ID
$variation_id = $item['variation_id']; // Get the variation ID
$product_name = $item['name']; // The product name
$item_qty = $item['quantity']; // The quantity
// Get line item totals (non discounted)
$line_total = $item['subtotal']; // or $item['line_subtotal'] -- The line item non discounted total
$line_total_tax = $item['subtotal_tax']; // or $item['line_subtotal_tax'] -- The line item non discounted tax total
// Get line item totals (discounted)
$line_total2 = $item['total']; // or $item['line_total'] -- The line item non discounted total
$line_total_tax2 = $item['total_tax']; // The line item non discounted tax total
// And so on ……
}
As reference:
作为参考:
回答by ishegg
WC_Order_Item_Product inherits from WC_Order_Item, which has get_order_id(), so you can get the Order ID with
WC_Order_Item_Product 继承自 WC_Order_Item,它有 get_order_id(),所以你可以用
$order_item->get_order_id();