在WooCommerce 3中获取订单项目和WC_Order_Item_Product

2022-08-30 15:53:14

阅读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 函数,该类不再具有构造。如何正确执行此操作?我没有看到以与上述相同的方式获取订单项的任何功能。
https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.htmlget

也许我在这里忽略了一些东西?


答案 1

如果您使用 get_id() 方法,则您的商品 ID 在代码中为 15

获取产品 ID:
获取产品 ID 的正确方法是:get_product_id()WC_Order_Item_Product

获取变体 ID
获取变体 ID 的正确方法是:get_variation_id()WC_Order_Item_Product

获取订单 ID
获取订单 ID 的正确方法是:get_order_id()WC_Order_Item_Product

获取WC_Product对象
获取对象的正确方法是:get_product()WC_Order_Item_ProductWC_Product

获取WC_Order对象
获取对象的正确方法是:get_order()WC_Order_Item_ProductWC_order

使用WC_Data方法获取和取消保护数据和元数据

  • get_data()
  • get_meta_data()

从订单项 ID 中获取WC_Product对象:

$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)

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数据和自定义元数据:

取消保护WC_Order_Item_Product数据和自定义元数据:

您可以使用所有WC_Order_Item_Product数据方法,也可以使用以下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)
}

阵列访问仍然可以(为了与旧阵列向后兼容)直接获取公共数据:

$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 ……
}

作为参考:


答案 2

WC_Order_Item_Product继承自具有get_order_id())的WC_Order_Item,因此您可以通过以下方式获取订单 ID:

$order_item->get_order_id();

推荐