37

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.

closed as off-topic by Rarst Jan 16 '14 at 20:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – Rarst
If this question can be reworded to fit the rules in the help center, please edit the question.

66

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'];
}
  • Unless I'm missing something, this doesn't appear to work in the latest version of WooCommerce... – rnevius Nov 9 '15 at 17:34
  • Still works in WooCommerce 2.4.8 for me, but you need to have the $order_id variable defined (sometimes it's in $order->id, depending on your context). – Ewout Nov 10 '15 at 14:25
  • @mevius i added an edit for 3+ with a dispatch function for checking multiple products – Garet Claborn Oct 20 '17 at 17:40
  • see edit history for dispatcher, this is not good without it as it will eat up your server pointlessly – Garet Claborn Nov 22 '17 at 19:46
  • @GaretClaborn that depends entirely on what you're doing with this data. 'as is', this example snippet is not wasteful of memory in any way. – Ewout Nov 27 '17 at 13:44
6

I worked on it and achieved something. That I would like to share to other developers. This is not preferred way to do it, but for knowledge I am posting my answer.

global $wpdb;
            $result = $wpdb->get_results('select t1.order_item_id, t2.* FROM 
            wp_woocommerce_order_items as t1 JOIN wp_woocommerce_order_itemmeta as t2 ON t1.order_item_id = t2.order_item_id
            where t1.order_id='.$order->ID);
            echo '<pre>';
            print_r($result);
            echo '</pre>'; 

hope will help someone.

Additionally:

Better to use wordpress table prefix to avoid problems in multiple website or in migration etc.

global $wpdb;
$table_name = $wpdb->prefix . 'table_name'; 
  • 1
    @ErenorPaz Thanks, I added content in answer, in reply to your comment :) – arslaan ejaz Oct 25 '16 at 8:33
  • Thank you for the fast response, even on an old thread! I'll delete my previous comments, since it's outdated now :) – Erenor Paz Oct 25 '16 at 10:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.