29

Reading up the changes in WooCommerce 3.0, it seems that is not possible to anymore properties from order items directly, so I would assume that the following code needs to be changed, since it is spitting out an error:

$order_item_id = 15;
$order_item = new WC_Order_Item_Product($order_item_id);
$return = $order_item->get_id() ? $order_item : false;

But, embarrassingly, I'm not sure how to change this code to use the correct new getter and setter functions in the newest version of this class, which no longer has a construct. How to do this properly? I don't see any get function on getting the order item in the same way as the above.
https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html

Maybe I am overlooking something here?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Solomon Closson
  • 6,111
  • 14
  • 73
  • 115

2 Answers2

82

If you use the get_id() method, you get your item ID which is 15 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 get WC_Product object is: get_product()

Get the WC_Order object
The correct WC_Order_Item_Product method to get WC_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:

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

Get the order items from the WC_Order object (and use the WC_product Object):

$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 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 using WC_Data following methods:

$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). The Array Access is still possible (for backwards compatibility with legacy arrays) to get the common data directly:

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

As reference:

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I'm not sure what this is doing. mine is calling `get_id()` which I really don't know if that is the product id or some other object id. Can you ellaborate on the difference, if any here? – Solomon Closson Aug 16 '17 at 06:22
  • Well, you seem to be returning the product id, but I believe I need to return the object, not the id... `get_id()` just checks if it has an id, but the object is being returned not the product id. And basically, I'm not sure if `get_id()` is possible to be used in this context. Maybe this is fine, I have no idea, but I am getting a PHP Notice saying `wc_deprecated_function` – Solomon Closson Aug 16 '17 at 06:33
  • Great answer. May be ideal to name the $item variable to $product instead. – Omar Shishani Dec 31 '21 at 22:05
  • 1
    @LoicTheAztec: I can only say thank you for this and your other perfect and detailed answers. Without your help I and many others here would have disappeared in the WooCommerce swamp. :-) – jMike Jul 19 '23 at 14:30
2

WC_Order_Item_Product inherits from WC_Order_Item, which has get_order_id(), so you can get the Order ID with

$order_item->get_order_id();
ishegg
  • 9,685
  • 3
  • 16
  • 31
  • Is `get_order_id()` returning the same as `get_id()`. Ohh, and thanks, I overlooked the inherited classes for some reason. `get_id()` is coming from `WC_Data` which I believe is a different value than `get_order_id()` coming from `WC_Order_Item` class.... – Solomon Closson Aug 16 '17 at 06:05
  • Right, I misread your question. Answer is almost the same though, like you saw, the get_id() method is also inherited from WC_Data. What error are you getting? – ishegg Aug 16 '17 at 06:09