阅读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;
但是,令人尴尬的是,我不知道如何修改这段代码,以便在这个类的最新版本中使用正确的新getter和setter函数,它不再有构造。如何正确地做到这一点?我没有看到任何get
函数以与上面相同的方式获取订单项。
https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html
或许我忽略了什么?
2条答案
按热度按时间58wvjzkj1#
If you use the
get_id()
method, you get your item ID which is15
in your code.Get the product ID:
The correct
WC_Order_Item_Product
method to get the Product Id is:get_product_id()
Get the variation ID:
The correct
WC_Order_Item_Product
method to get the variation Id is:get_variation_id()
Get the order ID
The correct
WC_Order_Item_Product
method to get the Order Id is:get_order_id()
Get the WC_Product object
The correct
WC_Order_Item_Product
method to getWC_Product
object is:get_product()
Get the WC_Order object
The correct
WC_Order_Item_Product
method to getWC_order
object is:get_order()
Get and unprotecting the data and meta data using
WC_Data
methods:get_data()
get_meta_data()
Get The
WC_Product
object from the order item ID:Get the order items from the
WC_Order
object*(and use the*WC_product
Object):Accessing data and custom meta data:
1). Unprotecting
WC_Order_Item_Product
data and custom meta data:You can use all
WC_Order_Item_Product data
methods or you can unprotect the data usingWC_Data
following methods:2). The Array Access is still possible (for backwards compatibility with legacy arrays) to get the common data directly:
As reference:
qojgxg4l2#
WC_Order_Item_Product继承自WC_Order_Item,后者具有get_order_id(),因此您可以使用以下命令获取订单ID