Get Product id from order id in Woocommerce
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.
Solutions
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';
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'];
}
Similar questions
Batch change order information as order status for specific Woocommerce order IDs
I have a list of order IDs (~400) that are currently not in the correct order status that I need to change, I would also want to update their payment method. What is the most efficient and best way to approach this? So far my thought process is to have an array of Order Ids, run through them and then $order->update_status( 'custom-status' ) on e...
Hide order statuses in dropdown based on order status in WooCommerce edit order page
I want to hide order statuses in the WooCommerce order status dropdown under specific scenarios: I still want to display all these order statuses in the order overview list. All I can find is to unset an order status completely: But this will of course also remove it from the order overview list, I just want to hide it in the dropdown on the order ...
Woocommerce, different order emails depending on product in order
I need to send two different "new_order" emails depending on the products ordered. The case is that the products are stored at different locations and I need one email to be sent containing only the products present at storage 1, and the other mail containing the products present at storage 2. If only products located at storage 1 was ordered, emai...
Add order item image only if the product exist on Woocommerce order view
On woocommerce My account view order pages I was able to add the product image using this answer code: Add the product image to Woocommerce my account order view But I have an issue related to imported orders where the product doesn’t exist. So this make an error with a blank page, when viewing those orders. Any idea on how to avoid this problem?
Woocommerce Product Short Code Order by Order of IDs
I would like to order the product display by the ids that are entered in [products ids=""] short code. I need it to order by the order you enter it. So... [products ids="1,2,3"] [products ids="3,1,2"] [products ids="2,3,1"] ... all list differently. I found this piece of code that I think is close but doesn't quite work. I'm not real familiar with ...
Also ask