-1

I have made a function for when the WooCommerce order payment is successful.

add_action( 'woocommerce_payment_complete', 'do_it' ); 
function do_it( $order_id ){
    // Here will run the function
    $order_id="//I want the order id here of woocommerce";
    $order = wc_get_order( $order_id ); //Then I can get order details from here
}

How Can I get $order_id here with this hook?

Brad Holmes
  • 497
  • 5
  • 22

2 Answers2

1

You already have the order ID in your function as the parameter.

add_action('woocommerce_payment_complete', 'do_it', 10, 1);
function do_it($order_id) {
    $order = new WC_Order( $order_id );
    $myuser_id = (int)$order->user_id;
    $user_info = get_userdata($myuser_id);
    $items = $order->get_items();
    foreach ($items as $item) {
        if ($item['product_id']==24) {
          // Do something clever
        }
    }
    return $order_id;
}
Stender
  • 2,446
  • 1
  • 14
  • 22
0

Try with this.

add_action( 'woocommerce_payment_complete', 'do_it' ); 
function do_it( $orderId ){
    // Get an instance of the WC_Order object (same as before)
    $order = wc_get_order( $order_id );
    $orderId = $order->get_id(); // Get the order ID
}
Krupal Panchal
  • 1,553
  • 2
  • 13
  • 26