Sol*_*son 18 php wordpress product orders woocommerce
好了,阅读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;
Run Code Online (Sandbox Code Playgroud)
但是,令人尴尬的是,我不确定如何更改此代码以在此类的最新版本中使用正确的新getter和setter函数,该类不再具有构造.怎么做得好?我没有看到任何get关于获得订单项的功能与上述相同.
https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html
也许我在这里忽视一些事情?
Loi*_*tec 40
如果您使用该
get_id()方法,则会获得15代码中的商品ID .
获取产品ID:获取产品ID
的正确WC_Order_Item_Product方法是:get_product_id()
获取订单ID获取订单ID
的正确WC_Order_Item_Product方法是:get_variation_id()
获取WC_Product对象获取WC_Product对象
的正确WC_Order_Item_Product方法是:
get_order_id()
获取WC_Order对象获取WC_order对象
的正确WC_Order_Item_Product方法是:
get_product()
使用以下get_order()方法获取和取消保护数据和元数据:
WC_Data
get_data()
get_meta_data()从订单商品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
$product_id = $item->get_variation_id();
// The WC_Product object
$product = $item->get_product();
// The quantity
$order_id = $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();
Run Code Online (Sandbox Code Playgroud)
从WC_Product对象获取订单商品 (并使用 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 ){
//Get the product ID
$product_id = $item->get_product_id();
//Get the variation ID
$product_id = $item->get_variation_id();
//Get the WC_Product object
$product = $item->get_product();
// The quantity
$product_name = $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();
}
Run Code Online (Sandbox Code Playgroud)
访问和取消保护WC_product数据:
您可以使用所有WC_Order_Item_Product方法,也可以使用WC_Order_Item_Product 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 );
}
Run Code Online (Sandbox Code Playgroud)
作为参考:
| 归档时间: |
|
| 查看次数: |
31905 次 |
| 最近记录: |