Get Order items and WC_Order_Item_Product in WooCommerce 3

Ok, reading up on the changes in WooCommerce 3.0+, it seems that you can not access this class directly anymore, so I would assume that this 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?

Answers


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 Product 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
$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();

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
    $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();
}

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 );
}

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
    $line_subtotal = $item['line_subtotal'];  // The line subtotal
    $line_total    = $item['line_total'];  // The line subtotal

    // And so on ……
}

As reference:


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();

Need Your Help

delete version number in url

url wicket wicket-1.5

How can I delete or hide the version number in the URL introduced in Wicket 1.5?

Push changes to a webpage without refreshing

javascript jquery ajax webpage

I know that we can do this by polling for changes in regular intervals. And this can be achieved by AJAX (for example using jQuery.load() along with setInterval() ).