If you use the get_id()
method, you get your item ID which is 15
in your code.
获得订单ID获取产品ID:
正确WC_Order_Item_Product方法来获取产品编号为:get_product_id()
Get the or DER ID
正确WC_Order_Item_Product方法来获取订单ID是:get_order_id()
获取WC_Product对象
正确WC_Order_Item_Product方法来获得WC_Product对象是: get_product()
获取WC_Order对象
正确的WC_Order_ Item_Product方法来获得WC_order对象是: get_order()
获取和使用WC_Data
方法取消保护数据和元数据:
get_data()
get_meta_data()
得到w^C_Product对象:
$order_item_id = 15;
$order_item = new WC_Order_Item_Product($order_item_id);
// The product ID
$product_id = $order_item->get_product_id();
// The order ID
$order_id = $order_item->get_order_id();
// The WC_Product object
$product = $order_item->get_product();
// The WC_Order object
$order = $order_item->get_order();
// The item ID
$item_id = $order_item->get_id(); // which is your $order_item_id
获取从WC_Order对象的订单项目:
$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){
//Get the product ID
$item_product->get_product_id();
//Get the WC_Product object
$item_product->get_product();
}
访问和不受保护的WC_Order_Item_Product数据:
你可以使用所有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_product){
// Get the common data in an array:
$item_product_data_array = $item_product->get_data();
// Get the special meta data in an array:
$item_product_meta_data_array = $item_product->get_meta_data();
}
参考:How to get WooCommerce order details
我不确定这是做什么。我正在调用'get_id()',我真的不知道这是产品ID还是其他对象ID。如果有的话,你可以精心研究差异吗? –
好吧,你似乎正在返回产品ID,但我相信我需要返回的对象,而不是ID ...'get_id()'只是检查它是否有一个ID,但对象被返回而不是产品ID。基本上,我不确定是否可以在此上下文中使用'get_id()'。也许这很好,我不知道,但我得到一个PHP通知,说'wc_deprecated_function' –
谢谢,这是很好的信息。非常感谢! –