Get the order items ID from WooCommerce

So i had a task to get all the ids of a order in WooCommerce. This was a easy task. The first thing you need to have is the order ID. If you have the order ID then you can find very easy the product ID.

Lets see the following code:

$order_id = "numberoforder"; // here you need to declare the order ID
$order = wc_get_order( $order_id ); // here you need to have
$productsordered = $order->get_items();
foreach ( $productsordered as $product ) {
    $productreturn_name[] = $product->get_name(); /// gets the name of the products in an array
    $productreturn_id[] = $product->get_product_id();/// gets the ID of the products in an array
    $productreturn_variation_id[] = $product->get_variation_id(); /// gets the variation of the ID of the products in an array
}

If you wish after that to see the exact code you will need to just print_r($variable) and you will see all the data

Leave a Reply

Your email address will not be published. Required fields are marked *