在WooCommerce 3中获取订单项和WC_Order_Item_Product

29

阅读 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个回答

82
如果你使用get_id()方法,你会得到你的项目ID,即代码中的15
获取产品ID: 正确的WC_Order_Item_Product方法来获取产品ID是:get_product_id()
获取变体ID: 正确的WC_Order_Item_Product方法来获取变体ID是:get_variation_id()
获取订单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()
从订单项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)
}

访问数据和自定义元数据:

1). 解除保护 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)
}

2). 数组访问仍然可以直接获取常见数据(为了向后兼容传统数组)。
$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 ……
}

作为参考:

我不确定这是在做什么。我的代码调用了 get_id(),但我真的不知道它是产品 ID 还是其他对象 ID。你能详细说明一下这里的区别吗? - Solomon Closson
嗯,你似乎返回了产品ID,但我认为我需要返回对象而不是ID... get_id() 只是检查它是否有一个ID,但是返回的是对象而不是产品ID。基本上,我不确定在这种情况下是否可以使用 get_id()。也许这没问题,我不知道,但我收到了一个PHP通知,说wc_deprecated_function - Solomon Closson
好的回答。也许将 $item 变量更名为 $product 更合适。 - Omar Shishani
1
@LoicTheAztec:我只能说谢谢你为此和其他完美而详细的回答。没有你的帮助,我和许多其他人在WooCommerce的沼泽中可能会迷失不见。 :-) - jMike

2

WC_Order_Item_Product继承自WC_Order_Item,后者有get_order_id()方法,因此您可以使用以下代码获取订单ID:

$order_item->get_order_id();

get_order_id() 返回的值和 get_id() 相同吗?哦,谢谢,我不知道为什么忽略了继承类。get_id() 来自 WC_Data,我相信这是一个不同于 WC_Order_Item 类中的 get_order_id() 的值。 - Solomon Closson
好的,我误读了你的问题。答案几乎相同,就像你看到的那样,get_id()方法也是从WC_Data继承而来的。你遇到了什么错误? - ishegg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接