Get Product id from order id in Woocommerce [closed]

I am having trouble with Woocommerce product details and order details relationship. I’m not able to find the product ID of a related order ID on the View Orders page of the Woocommerce theme. I simply want to get the product content and permalink etc on View Orders page.

I tried searching in wp_postmeta but had no luck.


WooCommerce 3.0+

you can get the order items of an order by

$order = wc_get_order( $order_id );
$items = $order->get_items();

then if you loop through the items, you can get all the relevant data:

foreach ( $items as $item ) {
    $product_name = $item->get_name();
    $product_id = $item->get_product_id();
    $product_variation_id = $item->get_variation_id();

a good tip is to check how the admin order pages get the data, you’ll find many answers there!

Pre-WooCommerce 3.0

$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
    $product_name = $item['name'];
    $product_id = $item['product_id'];
    $product_variation_id = $item['variation_id'];

Leave a Comment