Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get post_id from order_id in woocommerce

I need to get post_id in wordpress from woocommerce order id or order number.

global $woocommerce, $post;

$order = new WC_Order($post->ID);

//to escape # from order id 

$order_id = trim(str_replace('#', '', $order->get_order_number()));

with this code i am getting order id from post id. I have to reverse this and get post id from order id.

like image 250
Anurag Kumar Avatar asked Oct 31 '25 15:10

Anurag Kumar


2 Answers

you do realize that an order may contain 1 or more post id right?

$order = new WC_Order( $order_id );
$items = $order->get_items();

foreach ( $items as $item ) {
    //$product_name = $item['name'];
    //$product_id = $item['product_id']; // post id
    //$product_variation_id = $item['variation_id'];
}
like image 114
Reigel Avatar answered Nov 03 '25 06:11

Reigel


Have you tried this?

$post_id = get_post($order_id)->ID;
like image 28
Mojtaba Rezaeian Avatar answered Nov 03 '25 06:11

Mojtaba Rezaeian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!